展开 SCNVector3/SCNVector4 值进行打印? [英] Unwrapping SCNVector3/SCNVector4 values to print?

查看:66
本文介绍了展开 SCNVector3/SCNVector4 值进行打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个简单的问题

This should be an easy question

   import SceneKit
    import Accelerate

    var str:SCNVector3 = SCNVector3Make(1, 2, -8)
    println("vector \(str)" 

回答

vector C.SCNVector3

如何展开和显示像 [ 1, 2, -8] 这样的向量?

How to unwrap and display the vector like [ 1, 2, -8]?

推荐答案

Swift 2 的更新: 在 Swift 2 中,默认打印结构具有所有属性:

Update for Swift 2: In Swift 2, structures are by default printed with all properties:

let str = SCNVector3Make(1, 2, -8)
print("vector \(str)")
// Output:
// vector SCNVector3(x: 1.0, y: 2.0, z: -8.0)

您可以通过采用 CustomStringConvertible 来自定义输出协议:

You can customize the output by adopting the CustomStringConvertible protocol:

extension SCNVector3 : CustomStringConvertible {
    public var description: String {
        return "[\(x), \(y), \(z)]"
    }
}

let str = SCNVector3Make(1, 2, -8)
print("vector \(str)")
// Output:
// vector [1.0, 2.0, -8.0]

<小时>

上一个答案:

正如 Eric 已经解释过的,println() 检查对象是否符合到 Printable 协议.您可以为 SCNVector3 添加一致性使用自定义扩展:

As Eric already explained, println() checks if the object conforms to the Printable protocol. You can add conformance for SCNVector3 with a custom extension:

extension SCNVector3 : Printable {
    public var description: String {
        return "[\(self.x), \(self.y), \(self.z)]"
    }
}

var str = SCNVector3Make(1, 2, -8)
println("vector \(str)")
// Output:
// vector [1.0, 2.0, -8.0]

这篇关于展开 SCNVector3/SCNVector4 值进行打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆