如何打印 NSValue 字节
How to print NSValue bytes
如何打印 NSValue 字节?使用 print 时,它不会打印所有字节并放入“...”。例如。 {长度 = 128,字节 = 0x00000000 0000f03f 00000000 00000000 ... 00000000 0000f03f }
import Cocoa
import SceneKit
var identityMatrix = SCNMatrix4()
identityMatrix.m11 = 1
identityMatrix.m12 = 0
identityMatrix.m13 = 0
identityMatrix.m14 = 0
identityMatrix.m21 = 0
identityMatrix.m22 = 1
identityMatrix.m23 = 0
identityMatrix.m24 = 0
identityMatrix.m31 = 0
identityMatrix.m32 = 0
identityMatrix.m33 = 1
identityMatrix.m34 = 0
identityMatrix.m41 = 0
identityMatrix.m42 = 0
identityMatrix.m43 = 0
identityMatrix.m44 = 1
var a = [NSValue]()
a.append(NSValue(scnMatrix4:identityMatrix))
identityMatrix.m11 = 1
identityMatrix.m12 = 0
identityMatrix.m13 = 0
identityMatrix.m14 = 0
identityMatrix.m21 = 0
identityMatrix.m22 = 1
identityMatrix.m23 = 0
identityMatrix.m24 = 0
identityMatrix.m31 = 0
identityMatrix.m32 = 0
identityMatrix.m33 = 1
identityMatrix.m34 = 0
identityMatrix.m41 = 0
identityMatrix.m42 = -1.0
identityMatrix.m43 = 0
identityMatrix.m44 = 1
a.append(NSValue(scnMatrix4:identityMatrix))
print(a[0])
首先,我们可以通过使用数组字面量和 SCNMatrix4
初始化程序来消除所有可变性:
import Cocoa
import SceneKit
let a = [
SCNMatrix4(
m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 0, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: 0, m43: 0, m44: 1
),
SCNMatrix4(
m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 1, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: -1, m43: 0, m44: 1
),
].map(NSValue.init(scnMatrix4:))
print(a[0])
这使代码更加合理。我们还可以从文档中注意到,已经有一个常量可以用来获取单位矩阵:
import Cocoa
import SceneKit
let a = [
SCNMatrix4Identity,
SCNMatrix4(
m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 1, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: -1, m43: 0, m44: 1
),
].map(NSValue.init(scnMatrix4:))
print(a[0])
尚不清楚为什么这些矩阵包含在 NSValue
中,但要显示有关它们的详细信息(并调用它们的其他方法),您需要将矩阵从 NSValue
框使用 scnMatrix4Value
API:
let sampleMatrix = a[0].scnMatrix4Value
print(sampleMatrix)
如果你想打印这些的字节布局,(尽管仍然不清楚你为什么想要这个,因为 AFAICT,NSValue
和 SCNMatrix4
没有对它们的内存布局做出任何承诺), 您可以使用通常的指针 APIs,结合一些十六进制打印逻辑,例如 this:
withUnsafeBytes(of: &sampleMatrix) { sampleMatrixPointer in
print(sampleMatrixPointer.map { String(format: "%02x", [=13=]) }.joined(separator: ""))
}
打印:
000000000000f03f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f03f0000000000000000000000000000000000000000000000000000000000000000000000000000f03f
如何打印 NSValue 字节?使用 print 时,它不会打印所有字节并放入“...”。例如。 {长度 = 128,字节 = 0x00000000 0000f03f 00000000 00000000 ... 00000000 0000f03f }
import Cocoa
import SceneKit
var identityMatrix = SCNMatrix4()
identityMatrix.m11 = 1
identityMatrix.m12 = 0
identityMatrix.m13 = 0
identityMatrix.m14 = 0
identityMatrix.m21 = 0
identityMatrix.m22 = 1
identityMatrix.m23 = 0
identityMatrix.m24 = 0
identityMatrix.m31 = 0
identityMatrix.m32 = 0
identityMatrix.m33 = 1
identityMatrix.m34 = 0
identityMatrix.m41 = 0
identityMatrix.m42 = 0
identityMatrix.m43 = 0
identityMatrix.m44 = 1
var a = [NSValue]()
a.append(NSValue(scnMatrix4:identityMatrix))
identityMatrix.m11 = 1
identityMatrix.m12 = 0
identityMatrix.m13 = 0
identityMatrix.m14 = 0
identityMatrix.m21 = 0
identityMatrix.m22 = 1
identityMatrix.m23 = 0
identityMatrix.m24 = 0
identityMatrix.m31 = 0
identityMatrix.m32 = 0
identityMatrix.m33 = 1
identityMatrix.m34 = 0
identityMatrix.m41 = 0
identityMatrix.m42 = -1.0
identityMatrix.m43 = 0
identityMatrix.m44 = 1
a.append(NSValue(scnMatrix4:identityMatrix))
print(a[0])
首先,我们可以通过使用数组字面量和 SCNMatrix4
初始化程序来消除所有可变性:
import Cocoa
import SceneKit
let a = [
SCNMatrix4(
m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 0, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: 0, m43: 0, m44: 1
),
SCNMatrix4(
m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 1, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: -1, m43: 0, m44: 1
),
].map(NSValue.init(scnMatrix4:))
print(a[0])
这使代码更加合理。我们还可以从文档中注意到,已经有一个常量可以用来获取单位矩阵:
import Cocoa
import SceneKit
let a = [
SCNMatrix4Identity,
SCNMatrix4(
m11: 1, m12: 0, m13: 0, m14: 0,
m21: 0, m22: 1, m23: 0, m24: 0,
m31: 0, m32: 0, m33: 1, m34: 0,
m41: 0, m42: -1, m43: 0, m44: 1
),
].map(NSValue.init(scnMatrix4:))
print(a[0])
尚不清楚为什么这些矩阵包含在 NSValue
中,但要显示有关它们的详细信息(并调用它们的其他方法),您需要将矩阵从 NSValue
框使用 scnMatrix4Value
API:
let sampleMatrix = a[0].scnMatrix4Value
print(sampleMatrix)
如果你想打印这些的字节布局,(尽管仍然不清楚你为什么想要这个,因为 AFAICT,NSValue
和 SCNMatrix4
没有对它们的内存布局做出任何承诺), 您可以使用通常的指针 APIs,结合一些十六进制打印逻辑,例如 this:
withUnsafeBytes(of: &sampleMatrix) { sampleMatrixPointer in
print(sampleMatrixPointer.map { String(format: "%02x", [=13=]) }.joined(separator: ""))
}
打印:
000000000000f03f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f03f0000000000000000000000000000000000000000000000000000000000000000000000000000f03f