Swift / iphone开发新手......
如果我在json中有一个objs数组的字符串,它可以以括号开始['。根据json规范,afaik,这没关系。但是,以下内容在swift / xcode中作为单元测试爆发:
var json = "[{\"class\":\"ProductDesign\"},{\"class\":\"ProductDesign\"}]"
let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(json.dataUsingEncoding(NSUTF8StringEncoding), options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
我已经看到人们正在做的博客写作,但它对我不起作用(xcode做了一些有趣的事情,弹出:
0x287784: je 0x28778d ; swift_dynamicCastObjCClassUnconditional + 61
0x287786: addl $0x10, %esp
0x287789: popl %esi
0x28778a: popl %edi
0x28778b: popl %ebp
0x28778c: retl
0x28778d: leal 0xea35(%esi), %eax
0x287793: movl 0x468ef(%esi), %ecx
0x287799: movl %eax, 0x8(%ecx)
0x28779c: movl $0x0, 0xc(%ecx)
0x2877a3: int3
0x2877a4: nopw %cs:(%eax,%eax)
有什么想法吗?这个版本中的错误??程序员愚蠢?
答案 0 :(得分:3)
您的JSON是一个数组,但您将NSJSONSerialization.JSONObjectWithData
的结果转换为NSDictionary
;将其转换为NSArray
代替:
let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(json.dataUsingEncoding(NSUTF8StringEncoding), options: NSJSONReadingOptions.MutableContainers, error: nil) as NSArray
注意: 您也可以排除AnyObject!
类型声明,只需:
let jsonObject = NSJSONSerialization.JSONObjectWithData(json.dataUsingEncoding(NSUTF8StringEncoding), options: NSJSONReadingOptions.MutableContainers, error: nil) as NSArray