Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法强制转换类型为'__NSArrayM';(0x34df0900)至';NSDictionary';敏捷的_Ios_Json_Web Services_Swift_Nsdictionary - Fatal编程技术网

Ios 无法强制转换类型为'__NSArrayM';(0x34df0900)至';NSDictionary';敏捷的

Ios 无法强制转换类型为'__NSArrayM';(0x34df0900)至';NSDictionary';敏捷的,ios,json,web-services,swift,nsdictionary,Ios,Json,Web Services,Swift,Nsdictionary,在解码来自webservice的JSON响应时,我收到一个错误,错误是: Could not cast value of type '__NSArrayM' (0x34df0900) to 'NSDictionary' 我也尝试了在StackOverflow中找到的许多解决方案,但都不起作用 我的代码: let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONRead

在解码来自webservice的JSON响应时,我收到一个错误,错误是:

Could not cast value of type '__NSArrayM' (0x34df0900) to 'NSDictionary'
我也尝试了在StackOverflow中找到的许多解决方案,但都不起作用

我的代码:

let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary)!

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger
来自Web服务的响应:

[
    {
        "id": "1",
        "title": "bmw",
        "price": "500.00",
        "description": "330",
        "addedDate": "2015-05-18 00:00:00",
        "user_id": "1",
        "user_name": "CANOVAS",
        "user_zipCode": "32767",
        "category_id": "1",
        "category_label": "VEHICULES",
        "subcategory_id": "2",
        "subcategory_label": "Motos",
        "bdd": {}
    }
]

感谢您的帮助

尝试替换以下行:

let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary)!
包括:

let jsonData:NSArray = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSArray)!
我希望这对你有帮助

干杯

使用迅捷JSON:

如果成功就在阵列中

if let success = json[0]["success"].int {
     //Now you got your value
}
if let success = json["success"].int {
     //Now you got your value
}
或者如果成功不在阵列中

if let success = json[0]["success"].int {
     //Now you got your value
}
if let success = json["success"].int {
     //Now you got your value
}
您还可以检查成功值

if let success = json["success"].int where success == 1 {
     // Now you can do stuff
}

如果您错过了读取日志的“级别”,则会发生这种情况。我遇到了这个错误,尝试使用NSArray代替NSMutableDictionary

对象的实际内容位于该数组索引0处的NSDictionary中。尝试使用此代码(包括一些要说明的日志行)


今晚之前我不会在我的mac面前,我今晚会告诉你的!谢谢你的留言!现在我有了:“二进制运算符'!='不能应用于'NSArray?'类型的操作数”和'nil'的“if jsonData[“message”]as?NSArray!=nil{”使用类似于
NSArray.count>0的东西检查它是否为空或为空,如何?我已经尝试过此解决方案,但我得到了“count不是NSArray类型的成员”:(实际上,这些评论与您创建的问答线程无关!无论如何,您最好看看这两个答案!
answer1:http://stackoverflow.com/questions/27141784/swift-nsarray-does-not-have-member-named-count
和回答2:http://stackoverflow.com/questions/25540208/optional-in-swift-return-count-of-array
在这方面我只能帮你了,对不起!特别是我对Swift了解不多!