Unable to set value of type NSTaggedPointerString 'NSNumber'

I have such a Swift structure.

struct Usage { var totalData: Double var remainingTotalData: Double init(jsonData: NSData) { var jsonDict = [String: AnyObject]() do { jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! [String: AnyObject] } catch { print("Error occurred parsing data: \(error)") } totalData = jsonDict["totalfup"] as! Double remainingTotalData = jsonDict["totalrem"] as! Double } } 

From the API, I get the following JSON response. This is the println of the jsonDict variable.

 [ "totalfup": 96.340899, "totalrem": 3548710948 ] 

When I try to assign the totalfup value to the totalfup property, I get this error.

Failed to assign value of type NSTaggedPointerString 'NSNumber'

Does anyone know why? I tried changing the type of the property to float , and then the whole structure to a class, but still a problem arises.

+60
double ios swift swift2 nsjsonserialization
Oct 23 '15 at 9:37
source share
5 answers

The reason for the error is that jsonDict["totalfup"] is a string, so you should convert the string to double.

Please make sure to catch the exception and check the type before force deployment

 totalData = (jsonDict["totalfup"] as! NSString).doubleValue 

For security reasons, use if let :

 if let totalfup = (dict["totalfup"] as? NSString)?.doubleValue { } 
+91
Oct 23 '15 at 9:43 on
source share

I think it can help you.

 totalData = Double(jsonDict["totalfup"] as! String)! 
+15
Mar 10 '16 at 12:39
source share

The reason for the failure is that JSON returns String values, not numbers.

If the returned JSON data contains only these two key pairs, declare the type as [String:String] , which avoids casting types.

In any case, you need to put the code for updating the variables in the "good" branch of the do - catch expression.

 struct Usage { var totalData = 0.0 var remainingTotalData = 0.0 init(jsonData: NSData) { // Swift 3: Data do { let jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! [String: String] // Swift 3: let jsonDict = try NSJSONSerialization.jsonObject(with: jsonData) as! [String: String] totalData = Double(jsonDict["totalfup"]!) remainingTotalData = Double(jsonDict["totalrem"]!) } catch { print("Error occurred parsing data: \(error)") } } } 
+4
Oct 23 '15 at 9:56
source share

why not use direct Swift types directly?

 import Foundation struct Usage { var totalData: Double = 0 var remainingTotalData: Double = 0 init(jsonData: NSData) { do { if let jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as? [String:Double] { totalData = jsonDict["totalfup"] ?? 0 remainingTotalData = jsonDict["totalrem"] ?? 0 } } catch { print("Error occurred parsing data: \(error)") } } } if let data = "{\"totalfup\":96.340899,\"totalrem\":3548710948}".dataUsingEncoding(NSUTF8StringEncoding) { let usage = Usage(jsonData: data) dump(usage) /* โ–ฟ Usage - totalData: 96.340899 - remainingTotalData: 3548710948.0 */ } 
-one
Mar 10 '16 at 14:29
source share

Swift 4

  let strStatus:String = dictProperty.value(forKey: "StatusType") as! String let myStatus = Double.init(strStatus) 

Refresh

 extension String { func toDouble() -> Double? { let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "en_US_POSIX") return numberFormatter.number(from: self)?.doubleValue } func toInt() -> Int? { let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "en_US_POSIX") return numberFormatter.number(from: self)?.intValue } func toFloat() -> Float? { let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "en_US_POSIX") return numberFormatter.number(from: self)?.floatValue } func toBool() -> Bool? { let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "en_US_POSIX") return numberFormatter.number(from: self)?.boolValue } } 
-2
Aug 07 '18 at 9:14
source share



All Articles