Yogesh Chauhan's Blog

Solution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’

in Swift on 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!"
}

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Learn how to add Scroll Indicator using CSS and JavaScript?CSSHow to create a full screen loader using CSS and JavaScript?CSSFULL OUTER JOIN in PostgresPostgresWhat are components in Angular?AngularHow to create Flickering Texts using CSS?CSSSQL ANY OperatorSQL/MySQLAdd animated hamburgers menu using Hamburgers collection on GitHubCSSHow to convert an object from API to JSON array in Angular 9?AngularThe Lending Club Analysis using Logistic Regression and Random Forest in RStudioMiscellaneousWhat is HTMLUnknownElement?HTMLFull and Partial CUBE in Postgres with ExamplesPostgresAccessing and Setting features of JavaScript ObjectsJavaScript