i get the below error when trying to retrieve a key name from a query. and then cast it as an int of use by viewcontroller.
Could not cast value of type '__NSArrayM' (0x1073ebdb0) to 'NSNumber' (0x1069f3488).
this is the line in the query:
tempVarLDS = object.value(forKey: keyName)
when printed this returns
(
7
)
so it is an array. but i can't seem to access it via
tempvarLDS[0]
or any of the usual suspects. any idea how to get this int?
If it's an NSArray of NSNumbers, you should be able to cast it as an array of integers.
// Cast it
let tempvarLDS:[Int] = object.value(forKey: keyName) as! [Int]
// Loop
for num in tempvarLDS {
print(num)
}
I don't see why that wouldn't work
Related
I am trying to load some values from my firebase database directly into CoreData, the value in question is an integer but the issue is CoreData wants a Int64, Int32, or Int16, and it's not possible to downcast Ints in swift, So I am getting an error on the line of code where I assign the CoreData attribute to the database value. The database returns 0 (value will only be 0 or 1). Casting the database value as an Int64 is giving me an error. The bilateral_unilat attribute is of type Int64. How can I save an Int value into CoreData?
cdExercise.bilat_unilat = (exercise.childSnapshot(forPath: "bilat_unilat").value as! Int64)
this is the error:
Could not cast value of type 'NSTaggedPointerString' (0x7fff87a91958) to 'NSNumber' (0x7fff87b50b80).
in the debugger window I can print out a value of 0, which is the expected value, with this line of code:
po (exercise.childSnapshot(forPath: "bilat_unilat").value as! NSNumber)
Not sure what the issue is here
If you're attempting to get to a NSNumber as the end result, here's a couple of options
let aInt = snapshot.childSnapshot(forPath: "bilat_unilat").value as? Int ?? 0
let aNum = NSNumber(integerLiteral: aInt)
or just do it directly
let aNum = snapshot.childSnapshot(forPath: "bilat_unilat").value as! NSNumber
*note I omitted error handling for brevity and assumes the value of the bilat_unilat node is in fact an Int
The error
Could not cast value of type 'NSTaggedPointerString' (0x7fff87a91958) to 'NSNumber' (0x7fff87b50b80)
is misleading.
value is apparently NSNumber so cast it to NSNumber and then to Int64
let bilatUnilat = exercise.childSnapshot(forPath: "bilat_unilat").value as! NSNumber
cdExercise.bilat_unilat = bilatUnilat as! Int64
If the value is only 0 or 1 I recommend to declare the attribute as Bool
let bilatUnilat = exercise.childSnapshot(forPath: "bilat_unilat").value as! NSNumber
cdExercise.bilat_unilat = bilatUnilat == 1
And please name the Core Data attributes according to the naming convention lowerCamelCased
No idea why this won't work. The value of this dictionary is a key, yet whenever I try to cast to an array, I get a multitude of errors. Here's what I've tried, with the error I get following each example (in all examples, parameters is type [String : Any]:
let paramsArray = parameters["inputVO"] as AnyObject
if let array = paramsArray as? Array {
}
Error: Generic parameter 'Element' could not be inferred in cast to 'Array<_>'
if let array = parameters["inputVO"] as? Array {
}
Error: Ambiguous reference to member 'subscript'
I'm not sure what else to do to cast the result to an array? I'm sure I've done this before, I have no idea why this is failing. Any help is greatly appreciated.
Edit: Here's the output when I print out params. As expected, it is populated with an Array of Dictionary's.
Optional([["stmtDate": cmd, "transId": identifier, "isSupplementDataAvailable": true]])
Either it's an array
if let array = parameters["inputVO"] as? [[String:Any]] { ... }
or a dictionary
if let dictionary = parameters["inputVO"] as? [String:Any] { ... }
Both types are generics and need specific type information
[[String:Any]] is the short form of Array<Dictionary<String,Any>>
[String:Any] is the short form of Dictionary<String,Any>
I have a JSON response received from server which contains dictionary elements and one of the elements might be array which will contain values such as ["abc","def","ghi"] and I'm assigning this array to the variable called array as shown in the code below:
self.array = infoDictionary["element1"] as! [String]
The problem I'm facing is that incase there's null against key "element1" then I get the error:
Could not cast value of type '__NSCFConstantString' to 'NSArray'
This results in my application getting crashed. How can I save my application from crashing in case if there's "" against "element1"?
The error message says that the value for key element1 is a String rather than an Array.
If that value could be both String and [String] you have to check the type:
if let item = infoDictionary["element1"] {
if item is String {
print("I'm String")
} else if item is [String] {
print("I'm Array")
}
}
I'm trying to convert a anyObject to an Int. Here's my code:
println(currentObject[0])
println(_stdlib_getDemangledTypeName(currentObject[0]))
2
Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>
I'm trying to convert currentObject[0] to an Int. Here's what I tried:
let num: Int = currentObjec[0] as! Int
When I run the app, I get the following error:
Could not cast value of type '__NSCFString' (0x103c93c50) to 'NSNumber' (0x103535b88)
When I try this: currentObject[0].toInt()
I get an error saying
'AnyObject' does not have a member named 'toInt()'
The only way I'm able to make it work, is to do the following:
let numStr = currentObject[0] as! String
let num: Int = numStr.toInt()!
Is there a simpler way to do it? (Without having to first convert it to a String.)
From the error message
Could not cast value of type '__NSCFString' (0x103c93c50) to 'NSNumber' (0x103535b88)
you can conclude that currentObject[0] is actually an NSString.
In that case you can simply convert it with
let numStr = currentObject[0].integerValue
But note that this will crash if the array element is not an NSString.
My problem is:
I am querying user["key"] that I am attempting to assign to a new variable, as on the documentation.
The column in Parse is an array of Strings, in my query, if I println(user["key"]) it returns the correct objects.
However, when I do:
let myVariable = user["key"] as String
or
let myVariable = user["key"] as! String
I have the error:
Could not cast a value of type _NSArrayM to NSString
My end goal is to retrieve objects from Parse, and submit an "if" condition and then delete the results. For this I need to convert the objects into PFObject and this is where I struggle.
To add, I can only downcast to AnyObject! from let myVariable = user["key"]
and when I try to delete this object, I have the error
NSArrayM delete: unrecognised selector sent to instance.
Any help would be greatly appreciated.
Parse.com normally return an array, as you can see in your error message:
Could not cast a value of type _NSArrayM to NSString
__NSArrayM is a code-word for a mutable array, so you are trying to cast an array to a string.
If you are sure your query is return just one result you can retrieve just the last (and only) element in the array and cast to string.
if let myVariable = user["key"].last as? String{
println("myVariable key is \(myVariable)"
} else {
println("Could not retrieve key")
}