Swift IO无法从Dictionary转换为数组

时间:2015-08-11 11:47:55

标签: ios swift

我转换了字典"输入"到字符串数组,它运作良好

当我尝试字典"成本"与执行错误执行错误的方式相同

    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]

1 个答案:

答案 0 :(得分: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]