guard
let url = Bundle.main.url(forResource: "default", withExtension: "metallib"),
let data = try? Data(contentsOf: url) else {
fatalError("Unable to get metallib")
}
guard let generalKernel = try? CIKernel(functionName: "myKernel", fromMetalLibraryData: data) else {
fatalError("Unable to create CIKernel from myKernel")
}
// .xyzw または .rgba で各要素にアクセスできる。
int4 test = int4(0, 1, 2, 3);
int a = test.x; // a = 0
int b = test.y; // b = 1
int c = test.z; // c = 2
int d = test.w; // d = 3
int e = test.r; // e = 0
int f = test.g; // f = 1
int g = test.b; // g = 2
int h = test.a; // h = 3
//xyzw の並びで 複数の要素にアクセスできる
float4 c;
c.xyzw = float4(1.0f, 2.0f, 3.0f, 4.0f);
c.z = 1.0f;
c.xy = float2(3.0f, 4.0f);
c.xyz = float3(3.0f, 4.0f, 5.0f);
// 順列
float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
float4 swiz = pos.wzyx; // swiz = (4.0f, 3.0f, 2.0f, 1.0f)
float4 dup = pos.xxyy; // dup = (1.0f, 1.0f, 2.0f, 2.0f)
// 代入
float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
// pos = (5.0, 2.0, 3.0, 6.0)
pos.xw = float2(5.0f, 6.0f);
// pos = (8.0, 2.0, 3.0, 7.0)
pos.wx = float2(7.0f, 8.0f);
// pos = (3.0, 5.0, 9.0, 7.0)
pos.xyz = float3(3.0f, 5.0f, 9.0f);
MSL Matrix 型
halfnxm // n x m 16bit floating point floatnxm // n x m 32bit floating point
float4x4 m;
// This sets the 2nd column to all 2.0.
m[1] = float4(2.0f);
// This sets the 1st element of the 1st column to 1.0.
m[0][0] = 1.0f;
// This sets the 4th element of the 3rd column to 3.0.
m[2][3] = 3.0f;
import UIKit
class ContainerViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet weak var label1: UILabel!
@IBAction func button1Pressed(_ sender: Any) {
label1.text = "Pressed"
}
}
実行すると、次のエラーが発生する。
Thread 1: Exception: “-[UIViewController button1Pressed:]: unrecognized selector sent to instance 0x7f9e9b307890”
状況
スタックの詳細は次のとおり。
2020-07-17 10:05:13.714066+0900 TestScrollView1[10517:781374] [Storyboard] Unknown class ContainerViewController1 in Interface Builder file.
2020-07-17 10:05:18.843040+0900 TestScrollView1[10517:781374] -[UIViewController button1Pressed:]: unrecognized selector sent to instance 0x7f9e9b307890
2020-07-17 10:05:18.906229+0900 TestScrollView1[10517:781374] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[UIViewController button1Pressed:]: unrecognized selector sent to instance 0x7f9e9b307890’
*** First throw call stack:
原因
[Storyboard] Unknown class ContainerViewController1 in Interface Builder file. のメッセージにあるように、追加したファイルが認識されていない。
let cgimage:CGImage
let data = cgimage.dataProvider?.data
let bytes = CFDataGetBytePtr(data)!
let bytesPerPixel = cgimage.bitsPerPixel / cgimage.bitsPerComponent
for y in 0 ..< cgimage.height {
for x in 0 ..< cgimage.width {
let offset = (y * cgimage.bytesPerRow) + (x * bytesPerPixel)
let b = bytes[offset]
let g = bytes[offset + 1]
let r = bytes[offset + 2]
}
}
let numbers = [1, 2, 3, 4, 5]
let sum = numbers.withUnsafeBufferPointer { buffer -> Int in
var result = 0
for i in stride(from: buffer.startIndex, to: buffer.endIndex, by: 2) {
result += buffer[i]
}
return result
}
// 'sum' == 9