Could not cast value of type 'NSTaggedPointerString' to 'NSNumber'
The reason of the error is jsonDict["totalfup"]
is a String (NSTaggedPointerString
is a subclass of NSString
) , so you should convert String to Double.
Please make sure, catch exception and check type before force-unwrap !
totalData = (jsonDict["totalfup"] as! NSString).doubleValue
For safety, using if let
:
// check dict["totalfup"] is a String?
if let totalfup = (dict["totalfup"] as? NSString)?.doubleValue {
// totalfup is a Double here
}
else {
// dict["totalfup"] isn't a String
// you can try to 'as? Double' here
}
Can not cast value of type NSTaggedPointerString to NSDictionary
.childAdded
gives FIRDataSnapshot
at a time ... so no need to loop for this .. you just need to pass the current child in your structure.
self.productsArray.removeAll()
var newPostsArray = [Product]()
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
ref.observe(FIRDataEventType.childAdded, with: { (posts) in
let newPost = Product(snapshot: posts as! FIRDataSnapshot)
newPostsArray.insert(newPost, at: 0)
self.productsArray = newPostsArray
self.tableView.reloadData()
}) { (error) in
print(error.localizedDescription)
}
handling Could not cast value of type 'NSTaggedPointerString' to 'NSNumber'
It seems that not all of the entries in your JSON have a numeric value for LivingArea.
One option is to keep the variable as an optional and unwrap it when required. Don't force unwrap or you will get a crash when the value is missing or no an integer.
You should code defensively, especiallly when dealing with data from an external source.
Another option is to set a default of 0 using
let livingArea = userDict?["LivingArea"] as? Int ?? 0
Could not cast value of type 'NSTaggedPointerString' to 'NSNumber' - from PHP to Swift
I suggest you to use optionals, then your Int value seems to be a String in your Json, try:
for (k, v) in array {
print(k, "-", v)
if let stringV = v as? String {
convertInsideData[k as! String] = Int(stringV)
} else if let numberV = v as? NSNumber {
convertInsideData[k as! String] = v.intValue
}
}
Could not cast value of type 'NSTaggedPointerString' (0x10146ecd8) to 'NSNumber' (0x102675600)
id = Int((content[0]["id"] as! NSString).floatValue)
This works fine for me. Content is json array
Could not cast value of type 'NSTaggedPointerString' to an array
The error message says that NSTaggedPointerString
(expName
) can not be cast to NSArray
([Task]
)
Your goal is to add all Task
s to the task array if the name
property is not nil
but you're trying to add the name which causes the error.
Some suggestions:
fetch(context:
returns always an array of the NSManagedObject
subclass so cast it immediately.
Since you are using NSManagedObject
subclass get the name
property directly rather than with valueForKey
.
The check for > 0 is not needed because the loop will be skipped in case of an empty array.
let results = try context.fetch(request) as! [Task]
// check data existance
print(results.count)
for task in results {
if let expName = task.name {
print("expence name is :", expName)
tasks += task
print("my array is : \(tasks)")
}
}
or shorter
let results = try context.fetch(request) as! [Task]
tasks.filter{ $0.name != nil }
The most efficient way is to filter the tasks before the fetch via an appropriate predicate.
Related Topics
Swift:Difference in '!' and '' in Swift
Swift 3 Warning: Non-Optional Expression of Type 'String' Used in a Check for Optionals
Which Value Types in Swift Supports Copy-On-Write
Pattern Matching in a Swift for Loop
Swift 5 Coredata Predicate Using Uuid
Http Request Swift Providing Parameters
How to Move Platform with Velocity
Expression Pattern of Type 'String' Cannot Match Values of Type 'Nsstoryboardsegue.Identifier
Set Insets to the Collectionview Programmatically in Swift
Comparing Non-Optional Any to Nil Is Always False
Using Codable to Encode/Decode from Strings to Ints with a Function in Between
Load Image from Url on Watchkit
Testing If a Decimal Is a Whole Number in Swift
Cannot Convert Value of Type 'Binding<String>' to Expected Argument Type 'Binding<String>'
Diffrence Between Function and Generic Function in Swift
How to Fix Memory Leaks in iOS Applications