YogeshChauhan.com
Solution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’
May 5, 2020

Whenever you try to get responses from API, you get the JSON response in most cases and this error occurs all the time while getting the value from JSON array or dictionary as Int or as NSNumber.

In many cases (NOT ALL), the reason of the error is the JSON data we get is a String (Note: NSTaggedPointerString is a subclass of NSString).

So what we can do is to convert String to Double.

Like this:


// check dict["dummy"] is a String first
if let receivedData = (dict["dummy"]).doubleValue {
  // add more code in case the condition is true
}
else {
 // add more code in case the condition is false
}

OR


// check dict["dummy"] is a String first
if let receivedData = (dict["dummy"] as? NSString)?.doubleValue {
  // add more code in case the condition is true
}
else {
 // add more code in case the condition is false
}

Both of those solutions work.

It is really helpful when you want to compare the received data or just check the value like the following example.


let receivedData = (results["data"]!).doubleValue

if (receivedData == 0){
      self.label.text = "Nothing seem to be added yet!"
}
dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

The Differences Between HAVING Clause and WHERE Clause in SQLSQL/MySQLOrder By and Group By in PostgresPostgresIntroduction to components and templates Part 4: Pipes and DirectivesAngularWHERE Clause in PostgresPostgresOOP, Class and Objects Strategies For Beginners (PHP)PHPCan we execute conditions in SQL?SQL/MySQL2 Ways we can create an Array in JavaScriptJavaScriptHow to get current timestamp in Swift 4 and 5?SwiftSQL Right JoinSQL/MySQLWhat is Hadoop and Hadoop Ecosystem?MiscHow to Hide HTML elements automatically after few seconds using JavaScript?HTMLEssential SQL Commands We Need to KnowSQL/MySQL