这是我的Json数据。
{
"products":[
{
"capacity":4,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-uberx.png",
"display_name":"uberX",
"product_id":"a1111c8c-c720-46c3-8534-2fcdd730040d",
"description":"The low-cost Uber"
},
{
"capacity":6,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-uberxl2.png",
"display_name":"uberXL",
"product_id":"821415d8-3bd5-4e27-9604-194e4359a449",
"description":"low-cost rides for large groups"
},
{
"capacity":4,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-black.png",
"display_name":"UberBLACK",
"product_id":"d4abaae7-f4d6-4152-91cc-77523e8165a4",
"description":"The original Uber"
},
{
"capacity":6,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-suv.png",
"display_name":"UberSUV",
"product_id":"8920cb5e-51a4-4fa4-acdf-dd86c5e18ae0",
"description":"Room for everyone"
},
{
"capacity":4,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-taxi.png",
"display_name":"uberTAXI",
"product_id":"3ab64887-4842-4c8e-9780-ccecd3a0391d",
"description":"Taxi without the hassle"
}
]
}我要用键i.e capacity, image, display_name,product_id, description表示的所有值,并将其保存在给定的列表中
var array_capacity = Array<Int>();
var array_image = Array<String>();
var array_display_name = Array<String>();
var array_product_id = Array<String>();
var array_description = Array<String>(); 但它坠毁了。我想这是因为一些演员的问题。
这是我的解析代码。
var responseDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary
if(!(responseDict==nil)){
var products: NSArray = responseDict["products"] as NSArray
for item in products { // loop through data items
let obj = item as NSDictionary
for (key, value) in obj {
// print(key as String);
array_product_id.append(value as String);
array_display_name.append(value as String);
array_image.append(value as String);
array_description.append(value as String);
array_capacity.append(value as Int);
}
}
}日志
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x109c55f30: pushq %rbp
0x109c55f31: movq %rsp, %rbp
0x109c55f34: pushq %rbx
0x109c55f35: pushq %rax
0x109c55f36: movq %rsi, %rcx
0x109c55f39: movq %rdi, %rbx
0x109c55f3c: xorl %eax, %eax
0x109c55f3e: testq %rbx, %rbx
0x109c55f41: je 0x109c55f5d ; swift_dynamicCastObjCClassUnconditional + 45
0x109c55f43: movq 0x7858e(%rip), %rsi ; "isKindOfClass:"
0x109c55f4a: movq %rbx, %rdi
0x109c55f4d: movq %rcx, %rdx
0x109c55f50: callq *0x63112(%rip) ; (void *)0x000000010be04000: objc_msgSend
0x109c55f56: testb %al, %al
0x109c55f58: movq %rbx, %rax
0x109c55f5b: je 0x109c55f64 ; swift_dynamicCastObjCClassUnconditional + 52
0x109c55f5d: addq $0x8, %rsp
0x109c55f61: popq %rbx
0x109c55f62: popq %rbp
0x109c55f63: retq
0x109c55f64: leaq 0xecc7(%rip), %rax ; "Swift dynamic cast failed"
0x109c55f6b: movq %rax, 0x7fbe6(%rip) ; gCRAnnotations + 8
0x109c55f72: int3
0x109c55f73: nopw %cs:(%rax,%rax)发布于 2014-09-09 08:54:02
这里的问题是,您正在以快速的方式迭代NSDictionary。NSDictionary似乎不符合快速序列,所以您使用键值在NSDictionary上的迭代会崩溃。使用allKeys方法进行NSDictionary,并像对目标C那样定期提取值,如下所示,
if let jsonData = jsonString?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false){
if let jsonObject = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments, error: nil) as? [String: AnyObject]{
if let products: AnyObject = jsonObject["products"]{
for aProduct in products as NSArray{
for aKey in (aProduct as NSDictionary).allKeys{
println(aKey)
}
}
}
}
}您所做的错误之一是将字典中的值附加到并使用动态强制转换将其转换为string或Int。这不是这样做的,而且代码再次崩溃,您可以选择使用if let并将值追加到数组中来检查它是否可以转换为类型。
发布于 2014-09-09 09:47:44
可能这部分发生了错误:
array_product_id.append(value as String);
array_display_name.append(value as String);
array_image.append(value as String);
array_description.append(value as String);您可以检查是否可以将值强制转换为如下所示的特定对象:
if let specificObject = dictionary["key"] as? String
{
// specificObject has a value of string
}
else
{
// dictionary["key"] can't be cast-ed to string
} 上面的示例检查dictionary["key"]的值是否可以转换为String。
更新:更改这些行。您正在遍历字典,value的内容可以是字符串,也可以是Int,反之亦然,从而导致错误。
for (key, value) in obj {
array_product_id.append(value as String);
array_display_name.append(value as String);
array_image.append(value as String);
array_description.append(value as String);
array_capacity.append(value as Int);
}相反:
// Remove the for-loop
array_product_id.append(obj["your_key_here"] as String);
array_display_name.append(obj["your_key_here"] as String);
array_image.append(obj["your_key_here"] as String);
.
.
.发布于 2015-03-18 07:21:07
对我来说,转换错误是因为这些值是整数的:
"capacity":4,将所有值设置为字符串:
"capacity":"4",移除铸造错误:
for item in products { // loop through data items
let obj = item as NSDictionary
for (key, value) in obj {
println("\"\(key as String)\" : \"\(value as String)\"")
}
}除非你需要在Int中的值。这些对我有用。
https://stackoverflow.com/questions/25739894
复制相似问题