Unable to convert anyObject to String - swift

I am trying to run the following code, but am unable to convert the rest.value without the preceeding "Optional". The following is the code I get when attempting to solve the issue:
//for Withdrawl children
transRef.child("Withdraw").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//creates enumerator of the snapshot children for
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
let curString = (rest.value) as! String
self.anArray.append(curString)
//printing the rest.value to make sure there are children to iterate through
print(rest.value)
}
self.tableView.reloadData() //<<<<<<<<<<<< RELOAD TABLEVIEW
})
this is the error I am receiving: Could not cast value of type '__NSCFNumber' (0x10d068368) to 'NSString'

According to the Firebase documentation the value property: "Returns the contents of this data snapshot as native types. Data types returned: + NSDictionary + NSArray + NSNumber (also includes booleans) + NSString".
The var returns an Any? in order to handle this versatility.
In this case it looks like it is returning an NSNumber, so the String cast fails. I'm not sure exactly the context of the value in this scenario, but if you are sure that it is going to return a number every time you could cast it to a numerical type (Swift/Objective-C bridging allows NSNumber to be casted to any of the primitive number types in Swift), and then pass it into a String initializer to get a String out of it. e.g.:
let curNumber = rest.value as! Double
let curString = String(curNumber)
If you aren't sure what type will be returned, but are expecting to put it into an Array of Strings then you should probably check before you cast e.g.:
if let curNumber = rest.value as? Double {
let curString = String(curNumber)
self.anArray.append(curString)
}

Instead of (rest.value) as! String, say [Swift 2] String(rest.value) or [Swift 3] String(describing:rest.value). [If rest.value is an Optional, then say rest.value! in those formulations.]

Related

Cannot call value of non-function type 'Any'

i want to fix this problem Cannot call value of non-function type 'Any'
this is my code
databaseRef.child("ServiceA").queryOrderedByKey().observe(.childAdded, with: {
DataSnapshot in
let title = DataSnapshot.value!("title") as! String
self.posts.insert(PostStruct.init(title: title), at: 0)
})
Is there a way to fix it?
Thanks
DataSnapshot.value is Any and according to the documentation you could cast it in different types.
Data types returned: + NSDictionary + NSArray + NSNumber (also includes booleans) + NSString
If I understood your snipped well, in you case, you need to cast "DataSnapshot" into a dictionary in order to access with a subscript.
You can try this:
databaseRef.child("ServiceA").queryOrderedByKey().observe(.childAdded, with: {
DataSnapshot in
let title = (snapshot.value as? [String: Any])?["title"] as? String
self.posts.insert(PostStruct.init(title: title), at: 0)
})
Please also note that, in order to prevent crashes, I removed the force-wrap so "title" would be an optional String.

Switft Dictionary problem 'String?' is not convertible to 'String'; did you mean to use 'as!' to force downcast?

I'm new to Swift and ObjC and can't get this simple bit of code to work. I get the error 'String?' is not convertible to 'String'; did you mean to use 'as!' to force downcast?
guard let data = context as? Dictionary<String,String> else {return}
var str : String
str = data["Score"] as String //<<<I get the error here
When I change it to as! I know get this warning: Forced cast from 'String?' to 'String' only unwraps optionals; did you mean to use '!'?
Any ideas how to extract the string from the dictionary so I can use it?
It might be possible, that the key "Score" is not set in your dictionary and may return nil. Use this code to unwrap the optional:
if let score = data["Score"] {
str = score
}
Since you already unwrapped context to Dictionary<String, String>, Swift will automatically infer the type of score as String.
You don't need to cast because data dictionary would always return optional String type and you can use guard statement
guard let str = data[Score] else { return }
// Your code here
The type returned by subscripting a Dictionary is an optional, since it is valid to subscript it with non-existinent keys. So, as per the error message, in a dictionary with String values, the return type is String?.
If you know for sure that the key always exist in the dictionary, you can force-unwrap it as data["Score"]! to make the type String (without casting, but crashing if it doesn't exist). Otherwise handle the nil in some way, e.g., if let str = data["Score"] or str = data["Score"] ?? "0".
Use like this:
if let dataDictionary = context as? [String: Any] {
if let someString = dataDictionary["score"] as? String {
print(someString)
}
}
We don't know what kind of data you're working with but you're likely either getting back a dictionary actually of type [String: String], in which case you would:
func getData(context: [String: String]) {
guard let str = context["Score"] else {
return
}
print(str)
}
...or you're really getting back a dictionary of type [String: Any] (more common with database dictionaries), in which case you would:
func getData(context: [String: Any]) {
guard let str = context["Score"] as? String else {
return
}
print(str)
}

Swift Array to Set : Showing Recent Messages Command failed due to signal: Segmentation fault: 11

I am using firebase to retrieve a list of data then convert it to an NSDictonary array. I want to parse the data by a property e.g name
func getAllMyModels() {
if let e = email {
_ = ref.child("childName").queryOrdered(byChild: "email").queryEqual(toValue: e).observe(.value) { snapshot in
var dictionary = [NSDictionary]()
let children = snapshot.children
while let rest = children.nextObject() as? DataSnapshot, let value = rest.value {
dictionary.append(NSDictionary(dictionary: value as! [String: Any]))
}
let names = dictionary.flatMap {$0["name"]} // names print correct values
let set = Set(names)
print(set)
}
}
}
This code can't be complied the error is:
Showing Recent Messages
Command failed due to signal: Segmentation fault: 11
If i removed this line:
let set = Set(Array(names))
all works fine.
I also tested by replace it by this block
let ar = ["name1","name2"].flatMap { return $0 }
Set(ar)
No errors.
Not sure why? Who can tell, thanks!
EDIT: Even though the element in the array is String type but the names array is [Any], so the solution is
let names = dictionary.flatMap {$0["name"]} as! [String]
I think this errors occurs because the Array you generate from the dictionary with flatMap is an Array of Any and not a String Array, try to cast to a String like this:
...
let names = dictionary.flatMap {$0["name"] as? String}
let set = Set(Array(names))
...
Hope this help you
Cast names to [String]:
let names = dictionary.flatMap {$0["name"]} as! [String]

<= Operand asking for expected type in Swift?

I'm trying to look for items equal or less than zero in my query like so.....
for zeroItems in snapshot.value as! NSDictionary where zeroItems.value["Value"] as! Int <= 0
I'm getting an Expected type error. Please explain and show me how to correct.
You may want to consider applying a filter to simplify the logic to only include elements less than zero. I've created a working example in the IBM Swift Sandbox.
import Foundation
// Construct the data structure with demo data
struct Snapshot {
let value: NSDictionary
}
let dict: NSDictionary = [ "a" : -1, "b" : 0, "c" : 1]
let snapshot = Snapshot(value: dict)
//Apply a filter
let zeroItems = snapshot.value.filter{Int($0.1 as! NSNumber) <= 0}
//View the result
print(zeroItems)
Almost certainly you've overused Any if you have to use this many as! casts in a single line. Create a custom struct for this type and convert the data to that. If you're passing around NSDictionary and using as! very much, you're fighting the system.
That said, to make this work you probably just need more parentheses. Something like:
for zeroItems in (snapshot.value as! NSDictionary) where (zeroItems.value["Value"] as! Int) <= 0
But this is horrible Swift, so avoid this if possible.
You interpreting snapshot.value as Dictionary, so zeroItems is a tuple of key and value.
As I understand you having array of dictionaries, and you want to filter them by "Value" key, right?
If so then you may use following code:
// dictionary with Value key, and Int value
let dict: [String: Any] = ["Value":-1]
// array of dictionaries
let value: [[String: Any]] = [dict, <**add here as many as you want**>]
// get only array of integeres
let onlyIntegers = value.flatMap { $0["Value"] as? Int }.filter {$0 <= 0 }
print(onlyIntegers)
// get array of dictionaries which passes <=0 check
let onlyLessOrEqualTo0 = value.filter { dict in
if let value = dict["Value"] as? Int {
return value <= 0
}
return false
}
print(onlyLessOrEqualTo0)
First of all, adding some parentheses will help in seeing where the problem is:
for zeroItems in myDict as! NSDictionary where (zeroItems.value["Value"] as! Int) <= 0
Now, we get "Type 'Any' has no subscript members". So the problem is that you haven't told Swift what the type is of zeroItems's value, which I think is a dictionary of , judging from your code:
This compiles for me:
for zeroItems in myDict as! Dictionary<String, Dictionary<String, Int>> where zeroItems.value["Value"]! <= 0
However, this is not pretty. You could probably get more readable code using filter as was suggested in another answer.

Access data in Dictionary from NSCountedSet objects

I have an NSCountedSet consisting of String objects, if I iterate over the set and print it out, I see something like this:
for item in countedSet {
print(item)
}
Optional(One)
Optional(Two)
The countedSet is created from an Array of String objects:
let countedSet = NSCountedSet(array: countedArray)
If I print the array I see something like this:
["Optional(One)", "Optional(One)", "Optional(One)", "Optional(Two)", "Optional(Two)"]
Now, I want to use those counted strings to access data in a Dictionary, where the keys are String type and the values have the type [String : AnyObject]. If I print the dictionary, I see all the data.
However, if I use of the objects from the countedSet to access the dictionary, I always get a nil value back:
for item in countedSet {
let key = item as? String
let data = dictionary[key!] // Xcode forced me to unwrap key
print(data)
}
nil
nil
But
let key = "One"
let data = dictionary[key]
gives me the expected data.
What am I missing here, how can I access my data from the countedSet objects?
UPDATE: solved thanks to the comment from Martin R. The String objects were originally NSString objects (obtained from NSScanner), and I was casting them wrongly:
let string = String(originalString)
after I changed it to:
let string = (originalString as! String)
All works fine.