Tuesday, 15 June 2010

ios - Swift: SKNode should have optional types, Optional member does not have member -



ios - Swift: SKNode should have optional types, Optional member does not have member -

i had working , compiling swift game before updated new xcode , lot of errors has appeared. here 2 haven't been able fix.

func centeronnode(node: sknode ) { allow camerapositioninscene = node.scene?.convertpoint(node.position, fromnode: node.parent!) var y = cgfloat(node.parent?.position.y - camerapositioninscene?.y!) // error: // operand of postfix ‘!’ should have optional types; type cgfloat allow x = cgfloat(node.parent.position.x - camerapositioninscene.x) // error: // sknode? not have fellow member position // force photographic camera bit y -= 75 node.parent.position = cgpointmake(x, y)

i guess don't ! , ? business. wrapping, unwrapping, optional types. in java or c# terms?

this work, assuming node.scene never nil, , node.parent never nil:

let camerapositioninscene = node.scene!.convertpoint(node.position, fromnode: node.parent!) var y = cgfloat(node.parent!.position.y - camerapositioninscene.y) allow x = cgfloat(node.parent!.position.x - camerapositioninscene.x)

if possible nil, utilize code instead:

if allow camerapositioninscene = node.scene?.convertpoint(node.position, fromnode: node.parent!) { if allow parent = node.parent { var y = cgfloat(parent.position.y - camerapositioninscene.y) allow x = cgfloat(parent.position.x - camerapositioninscene.x) // force photographic camera bit y -= 75 parent.position = cgpointmake(x, y) } }

this test ensure none of values nil, , nil if (instead of crashing, first illustration would).

for explanation of how optionals work, read apple's article.

ios iphone xcode swift ios8

No comments:

Post a Comment