我有一辆车和一个司机.他们互相引用.在汽车的init()中,我创建了一个驱动程序并将其分配给驱动程序成员.驱动程序成员有一个didSet方法,该方法应该设置驱动程序的汽车,从而将它们相互链接.
class GmDriver {
var car: GmCar! = nil
}
class GmCar {
var driver: GmDriver {
didSet {
driver.car = self
}
}
init() {
driver = GmDriver()
}
}
let myCar = GmCar()
println(myCar.driver.car) // nil
Run Code Online (Sandbox Code Playgroud)
但是,didSet永远不会触发.为什么?
使用Swift和SpriteKit,我有一个问题,SKSpriteNode在作为子项添加到另一个SKSpriteNode时没有显示.相比之下,放置在完全相同位置的SKLabelNode会显示出来.
// does not show up, expected a white square
override func renderBody(parentNode: SKNode, position: CGPoint) {
let node = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 48, height: 48))
node.position = position
parentNode.addChild(node)
}
// does show up as white text in the correct position
override func renderBody(parentNode: SKNode, position: CGPoint) {
let node = SKLabelNode(text: "hello")
node.position = position
parentNode.addChild(node)
}
Run Code Online (Sandbox Code Playgroud)
'parentNode'和'position'在两种情况下都是相同的.
'parentNode'是一个更大的SKSpriteNode,使用深色的纹理创建,覆盖屏幕作为背景.
'position'是CGPoint(x:50,y:50).
看起来矩形精灵低于背景,而标签精灵位于顶部.我通过删除背景并将矩形精灵直接添加到场景节点进行实验,然后它就会显示出来.
有没有人知道什么是错的?
编辑:手动设置zPosition可解决问题.但是,我仍然认为某些地方出了问题.您不需要在子节点上手动设置它,以避免其父节点隐藏子节点.我从来没有在Xcode 5中做到这一点.为什么labelnode和sprite节点之间有区别?
这是一个有希望更好地说明它的例子.在Xcode中创建一个默认的SpriteKit,Swift,Iphone项目6.将下面的代码粘贴到touchesBegan中的for循环中.删除在didMoveToView中创建的"Hello World"标签.运行它并单击屏幕中间的某个位置.
let location = touch.locationInNode(self)
// LabelNode without manually setting zposition
// …Run Code Online (Sandbox Code Playgroud) 在 JSON 数据文件中,我有一个像这样的 unicode 字符:
{
”myUnicodeCharacter”: ”\\u{25a1}”
}
Run Code Online (Sandbox Code Playgroud)
我知道如何从 JSON 文件中读取数据。当它包含如上表示的字符时会出现问题。
我将它读入一个字符串变量 myUnicodeCharacterString,它的值是“\u{25a1}”。顺便说一下,我不能在 JSON 数据文件中使用单个斜杠,因为在这种情况下,它无法将文件中的数据识别为正确的 JSON 对象,返回 nil。
然而,当该值被分配给用于显示它的东西时,它不会被编码到它的图形表示中,例如一个 SKLabelNode:
mySKLabelNode.Text = myUnicodeCharacterString // displays ”\u{25a1}” and not a hollow square
Run Code Online (Sandbox Code Playgroud)
问题归结为:
// A: direct approach, does works
let unicodeValueByValue = UnicodeScalar("\u{25a1}") // ”9633”
let c1 = Character(unicodeValueByValue) // ”a hollow square”
// B: indirect approach, this does not work
let myUnicodeString = "\u{25a1}"
let unicodeValueByVariable = UnicodeScalar(myUnicodeString) // Error: cannot find an initialiser
let c2 = Character(unicodeValueByVariable)
Run Code Online (Sandbox Code Playgroud)
那么,如果代码中没有直接给出格式为 …