繁体   English   中英

Swift IO无法从字典转换为数组

[英]Swift IOs not working converting from Dictionary to array

我将字典“类型”转换为字符串数组,并且运行良好

当我尝试以“坏”执行错误的相同方式词典“费用”时

    import Foundation

/* Path for JSON files bundled with the Playground */
var pathForHearthstoneJSON = NSBundle.mainBundle().pathForResource("hearthstone_basic_set", ofType: "json")

/* Raw JSON data (...simliar to the format you might receive from the network) */
var rawHearthstoneJSON = NSData(contentsOfFile: pathForHearthstoneJSON!)

/* Error object */
var parsingHearthstoneError: NSError? = nil

/* Parse the data into usable form */
var parsedHearthstoneJSON = NSJSONSerialization.JSONObjectWithData(rawHearthstoneJSON!, options: .AllowFragments, error: &parsingHearthstoneError) as! NSDictionary

/* Start playing with JSON here... */
var origin = parsedHearthstoneJSON["Basic"]
var type = origin?.valueForKeyPath("type") as! [String]
var cost = origin?.valueForKeyPath("cost") as! [Int]

var counter = 0;

type[0]
cost[0]

它像那样工作

    /* Path for JSON files bundled with the Playground */
var pathForHearthstoneJSON = NSBundle.mainBundle().pathForResource("hearthstone_basic_set", ofType: "json")

/* Raw JSON data (...simliar to the format you might receive from the network) */
var rawHearthstoneJSON = NSData(contentsOfFile: pathForHearthstoneJSON!)

/* Error object */
var parsingHearthstoneError: NSError? = nil

/* Parse the data into usable form */
var parsedHearthstoneJSON = NSJSONSerialization.JSONObjectWithData(rawHearthstoneJSON!, options: .AllowFragments, error: &parsingHearthstoneError) as! NSDictionary

/* Start playing with JSON here... */
var origin = parsedHearthstoneJSON["Basic"]
var type = origin?.valueForKeyPath("type") as! [String]
//var cost = origin!.valueForKeyPath("cost")
let cost = origin?.valueForKeyPath("cost") as! [NSObject];
//println(cost.class)
cost.dynamicType
var counter = 0;

type[0]
cost[3]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM