无法将类型'__NSCFBoolean'(0x100b123b8)的值转换为'NSString'(0x100f0ab48)

时间:2022-03-25 16:26:11

I'm trying to parse a .json file, but as I thought I was near the end, an error occurred:

我正在尝试解析.json文件,但是当我以为我快要结束时,发生了一个错误:

Could not cast value of type '__NSCFBoolean' (0x10c8ba3b8) to 'NSString' (0x10ccb2b48).

无法将类型'__NSCFBoolean'(0x10c8ba3b8)的值转换为'NSString'(0x10ccb2b48)。

I have done my research and I found to (surprise) that i have some Boolean values messing around in stade of String in my .json ...

我做了我的研究,我发现(惊讶)我有一些布尔值在我的.json的字符串中乱搞...

So how can I avoid this? I've looked up for some method which could give me the type of the bar["tags"] before I try to put it in a String but I can't find..

那我怎么能避免这个呢?在我尝试将它放入String之前,我已经查找了一些可以给我类型的条[[tags]]的方法,但我找不到..

Here is an extract of the .json and my (much too long) code:

这是.json和我(太长)代码的摘录:

...{
    "id": 3906,
    "address": "L\u2019Hotel le 123 Sebastopol 23, boulevard S\u00e9bastopol, Paris, 75002, Le Louvre, Paris",
    "name": "123 Sebastopol",
    "url": "/bar/paris/le-louvre/123-sebastopol",
    "image_url": "https://prh-wbb-prod.s3.amazonaws.com/generic_640_480/WBB%20holding%20image3_54abcf7d218d1_55b8d2128eabb.jpg",
    "tags": "Hotel, Cocktail, Terrace",
    "latitude": 48.867598851356,
    "longitude": 2.3533117460327
}, {
    "id": 1523,
    "address": "73 Rue d\u0027Aboukir, 75002 Paris, France, Le Louvre, Paris",
    "name": "Lockwood",
    "url": "/bar/paris/le-louvre/lockwood",
    "image_url": "https://prh-wbb-prod.s3.amazonaws.com/bar_640_480/lockwood_001_553f5b2c99f4b.jpg",
    "tags": false,
    "latitude": 48.867716087563,
    "longitude": 2.3469846706722
}...

my code:

id = (bar["id"] as? Int)!
name = (bar["name"] as? String)!
adress = (bar["address"] as? String)!
url = (bar["url"] as? String)!
image_url = (bar["image_url"] as? String)!

tags = (bar["tags"] as? String)!

latitude = (bar["latitude"] as? Double)!
longitude = (bar["longitude"] as? Double)!

3 个解决方案

#1


2  

hum, seem's like I didn't do enough research!

哼,好像我没有做足够的研究!

here is the solution:

这是解决方案:

if let test = bar["tags"] as? String {
    tags = (bar["tags"] as? String)!
}else{
    tags=" "
}

#2


0  

I had a similar issue once I resolved to parse JSON under Swift3 and did not expect the result to be a Bool.

一旦我决定在Swift3下解析JSON并且没想到结果是Bool,我就遇到了类似的问题。

Here is my code in the hope it helps folk like me who are not fans of Swift 3.

这是我的代码,希望它能帮助像我这样的人不是Swift 3的粉丝。

In my case the entry in the JSON output was

在我的例子中,JSON输出中的条目是

success = 0;

note it did not have the "" around success or 0 like this

注意它没有成功的“”或者像这样的0

"success" = "0"

So here is the code I used to obtain the Bool value from the JSON output

所以这里是我用来从JSON输出中获取Bool值的代码

success = 0

Noting no " "

注意到没有“”

let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if (jsonResult?["success"] as? Bool)!
 {
      okToLogIn = true
      print(okToLogIn)
 }
 else
 {
       okToLogIn = false
       print(okToLogIn)
 }

So I just kept it as Bool and checked against true or false and my code now works as it should.

所以我只是把它保存为Bool并检查是对还是错,我的代码现在可以正常工作了。

#3


-1  

You are trying to parse treat Bool value - false - as instance of String.

您正在尝试解析处理Bool值 - false - 作为String的实例。

Change your code to:

将您的代码更改为:

id = (bar["id"] as? Int)!
name = (bar["name"] as? String)!
adress = (bar["address"] as? String)!
url = (bar["url"] as? String)!
image_url = (bar["image_url"] as? String)!

tags = (bar["tags"] as? Bool)!

latitude = (bar["latitude"] as? Double)!
longitude = (bar["longitude"] as? Double)!

#1


2  

hum, seem's like I didn't do enough research!

哼,好像我没有做足够的研究!

here is the solution:

这是解决方案:

if let test = bar["tags"] as? String {
    tags = (bar["tags"] as? String)!
}else{
    tags=" "
}

#2


0  

I had a similar issue once I resolved to parse JSON under Swift3 and did not expect the result to be a Bool.

一旦我决定在Swift3下解析JSON并且没想到结果是Bool,我就遇到了类似的问题。

Here is my code in the hope it helps folk like me who are not fans of Swift 3.

这是我的代码,希望它能帮助像我这样的人不是Swift 3的粉丝。

In my case the entry in the JSON output was

在我的例子中,JSON输出中的条目是

success = 0;

note it did not have the "" around success or 0 like this

注意它没有成功的“”或者像这样的0

"success" = "0"

So here is the code I used to obtain the Bool value from the JSON output

所以这里是我用来从JSON输出中获取Bool值的代码

success = 0

Noting no " "

注意到没有“”

let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if (jsonResult?["success"] as? Bool)!
 {
      okToLogIn = true
      print(okToLogIn)
 }
 else
 {
       okToLogIn = false
       print(okToLogIn)
 }

So I just kept it as Bool and checked against true or false and my code now works as it should.

所以我只是把它保存为Bool并检查是对还是错,我的代码现在可以正常工作了。

#3


-1  

You are trying to parse treat Bool value - false - as instance of String.

您正在尝试解析处理Bool值 - false - 作为String的实例。

Change your code to:

将您的代码更改为:

id = (bar["id"] as? Int)!
name = (bar["name"] as? String)!
adress = (bar["address"] as? String)!
url = (bar["url"] as? String)!
image_url = (bar["image_url"] as? String)!

tags = (bar["tags"] as? Bool)!

latitude = (bar["latitude"] as? Double)!
longitude = (bar["longitude"] as? Double)!