我想从plist文件中读取整数值.我有以下代码: let path = NSBundle.mainBundle().pathForResource("savedState", ofType: "plist")let dict = NSDictionary(contentsOfFile: path!)let players: AnyObject = String(dict.valueForKey("play
let path = NSBundle.mainBundle().pathForResource("savedState", ofType: "plist") let dict = NSDictionary(contentsOfFile: path!) let players: AnyObject = String(dict.valueForKey("players") as NSString) let level: AnyObject = String(dict.valueForKey("level") as NSString) let numPlayers = Int(players as NSNumber) let playLevel = Int(level as NSNumber)
让玩家:让级别:崩溃我的应用程序.我知道这应该很简单 – 我只是想不出怎么做.
你可能正在寻找这样的东西:let path = NSBundle.mainBundle().pathForResource("savedState", ofType: "plist") let dict = NSDictionary(contentsOfFile: path!) let players = dict.valueForKey("players") as? String let level = dict.valueForKey("level") as? String let numPlayers = players != nil ? players!.toInt() : 0 let playLevel = level != nil ? level!.toInt() : 0
它试图从plist中读取播放器和级别作为可选字符串,然后如果它们不是nil,则将numPlayers和playLevel设置为它们的Int值.如果它们是nil numPlayers并且playLevel设置为0.虽然如果你的plist值是整数,为什么不把它们作为Ints读取?
let players = dict.valueForKey("players") as? Int let level = dict.valueForKey("level") as? Int