Please help! Error with the location of objects in the game.

Good afternoon!

Please help. He did play on Xcode 7.3, everything was fine! But the transition to a new Xcode 8 and Swift 3, an error occurs. I can not understand!


It has been SWIFT 2


let levelPlist = NSBundle.mainBundle().pathForResource("Level01", ofType: "plist")

let levelData = NSDictionary(contentsOfFile: levelPlist!)!

endLevelY = levelData["EndY"]!.integerValue!

let platforms = levelData["Platforms"] as! NSDictionary

let platformPatterns = platforms["Patterns"] as! NSDictionary

let platformPositions = platforms["Positions"] as! [NSDictionary]

for platformPosition in platformPositions {

let patternX = platformPosition["x"]?.floatValue

let patternY = platformPosition["y"]?.floatValue

let pattern = platformPosition["pattern"] as! NSString

let platformPattern = platformPatterns[pattern] as! [NSDictionary]

for platformPoint in platformPattern {

let x = platformPoint["x"]?.floatValue

let y = platformPoint["y"]?.floatValue

let type = PlatformType(rawValue: platformPoint["type"]!.integerValue)

let positionX = CGFloat(x! + patternX!)

let positionY = CGFloat(y! + patternY!)

let platformNode = createPlatformAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)

foregroundNode.addChild(platformNode)

}

}

let stars = levelData["Stars"] as! NSDictionary

let starPatterns = stars["Patterns"] as! NSDictionary

let starPositions = stars["Positions"] as! [NSDictionary]

for starPosition in starPositions {

let patternX = starPosition["x"]?.floatValue

let patternY = starPosition["y"]?.floatValue

let pattern = starPosition["pattern"] as! NSString

let starPattern = starPatterns[pattern] as! [NSDictionary]

for starPoint in starPattern {

let x = starPoint["x"]?.floatValue

let y = starPoint["y"]?.floatValue

let type = StarType(rawValue: starPoint["type"]!.integerValue)

let positionX = CGFloat(x! + patternX!)

let positionY = CGFloat(y! + patternY!)

let starNode = createStarAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)

foregroundNode.addChild(starNode)

}

}


It became SWIFT 3

if GameState.sharedInstance.nlevel == 1 {

let levelPlist = Bundle.main.path(forResource: "Level01", ofType: "plist")

let levelData = NSDictionary(contentsOfFile: levelPlist!)!

endLevelY = (levelData["EndY"]! as AnyObject).intValue!

let platforms = levelData["Platforms"] as! NSDictionary

let platformPatterns = platforms["Patterns"] as! NSDictionary

let platformPositions = platforms["Positions"] as! [NSDictionary]

for platformPosition in platformPositions {

let patternX = (platformPosition["x"] as AnyObject).floatValue

let patternY = (platformPosition["y"] as AnyObject).floatValue

let pattern = platformPosition["pattern"] as! NSString

let platformPattern = platformPatterns[pattern] as! [NSDictionary]

for platformPoint in platformPattern {

let x = (platformPoint["x"] as AnyObject).floatValue

let y = (platformPoint["y"] as AnyObject).floatValue

let type = PlatformType(rawValue: (platformPoint["type"]! as AnyObject).intValue)

let positionX = CGFloat(x! + patternX!)

let positionY = CGFloat(y! + patternY!)

let platformNode = createPlatformAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)

foregroundNode.addChild(platformNode)

}

}

let stars = levelData["Stars"] as! NSDictionary

let starPatterns = stars["Patterns"] as! NSDictionary

let starPositions = stars["Positions"] as! [NSDictionary]

for starPosition in starPositions {

let patternX = (starPosition["x"] as AnyObject).floatValue

let patternY = (starPosition["y"] as AnyObject).floatValue

let pattern = starPosition["pattern"] as! NSString

let starPattern = starPatterns[pattern] as! [NSDictionary]

for starPoint in starPattern {

let x = (starPoint["x"] as AnyObject).floatValue

let y = (starPoint["y"] as AnyObject).floatValue

let type = StarType(rawValue: (starPoint["type"]! as AnyObject).intValue)

let positionX = CGFloat(x! + patternX!)

let positionY = CGFloat(y! + patternY!)

let starNode = createStarAtPosition(CGPoint(x: positionX, y: positionY), ofType: type!)

foregroundNode.addChild(starNode)

}

}


Gives an error message

2016-09-17 09:18:18.211268 Little story[1940:567262] [DYMTLInitPlatform] platform initialization successful

2016-09-17 09:19:14.530467 Little story[1940:567195] Metal GPU Frame Capture Enabled

2016-09-17 09:19:14.531981 Little story[1940:567195] Metal API Validation Enabled

fatal error: unexpectedly found nil while unwrapping an Optional value

2016-09-17 09:19:15.012275 Little story[1940:567195] fatal error: unexpectedly found nil while unwrapping an Optional value

(lldb)

Replies

When you get the “fatal error: unexpectedly found nil” error, which line is highlighted?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you to pay attention!

allocates a string: let patternX = (platformPosition [ "x"] as AnyObject) .floatValue

I made substituted let patternX = (platformPosition [ "x"] as? AnyObject?) ??. FloatValue

Values pulled! But now stop with the same error on: let positionX = CGFloat (x + patternX!!)


In the game should be called the coordinates of the platforms and the stars of the plist. He takes the coordinates of the maximum point in the game, but to build the platform and the star refuses. The Swift 2 built perfectly.


Picture:

The best way of dealing with property lists has changed a lot in Swift 3 because of the Foundation value types SE-0069. You could probably apply a small number of fixes to your code to get it up’n’limping on Swift 3 but I think you’d be better off changing to the new approach. Here’s an example of that.

Consider this property list.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>myNumber</key>
    <real>3.141</real>
    <key>myString</key>
    <string>Hello Cruel World!</string>
    <key>myArray</key>
    <array>
        <string>Roger</string>
        <string>Dave</string>
        <string>Richard</string>
        <string>Nick</string>
    </array>
    <key>myDictionary</key>
    <dict>
        <key>nestedNumber</key>
        <integer>42</integer>
        <key>nestedString</key>
        <string>Goodbye Cruel World!</string>
    </dict>
</dict>
</plist>

This code extracts and prints each of the items in the property list.

let url = Bundle.main.url(forResource: "Example", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let propertyList = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String:AnyObject]
// print(propertyList)
let myNumber = propertyList["myNumber"]! as! Float
print(myNumber)
let myString = propertyList["myString"]! as! String
print(myString)
let myArray = propertyList["myArray"]! as! [String]
print(myArray)
let myDictionary = propertyList["myDictionary"]! as! [String:AnyObject]
let nestedNumber = myDictionary["nestedNumber"]! as! Int
print(nestedNumber)
let nestedString = myDictionary["nestedString"]! as! String
print(nestedString)

It prints:

3.141
Hello Cruel World!
["Roger", "Dave", "Richard", "Nick"]
42
Goodbye Cruel World!

If you have any questions about why I’ve written this code the way I have, please do post back.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi

Is this a bug in xcode. Following Rays Uberjump tutorials no longer work in Swift 3. Also the variable in let platformPattern = platformPatterns[pattern] as! [NSDictionary] returns whats below. Is there a work around for this b/c I cannot find one


Printing description of platformPattern:

expression produced error: error: /var/folders/2g/45wwty990flfjvjv_ljm5g2h0000gn/T/./lldb/4064/expr7.swift:1:77: error: use of undeclared type 'Foundation'

Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<Swift.Array<Foundation.NSDictionary>>(bitPattern: 0x1088c8800)!.pointee)

Is this a bug in xcode.

Is what a bug in Xcode? The behaviour of the

platformPattern
snippet you quoted? Or the problem with the debugger?

As I mentioned earlier, the code originally posted by Pharlequino could be made to work (probably :-) but it’s not how you’d write new Swift 3 code. If you got that code from a third-party tutorial, it’d be best to follow up with the author to get a Swift 3 equivalent.

OTOH, if the debugger is giving mysterious errors when you try to debug, that’s definitely worth a bug report.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Quinn “The Eskimo!”. I figured it out. just changing a few !! to ?? made me Swift III worthy. Thanks again