Swift 3 - Access item key values by array index - swift

Hi im trying to access the items in the legs array inside my plist
I want to be able to get the values for key = "title" from the legs array from this plist
I already have the method to get the values from the dictionary called abs which is the following code
let path = Bundle.main.path(forResource:"Exercise", ofType: "plist")
let dict:NSDictionary = NSDictionary(contentsOfFile: path!)!
if (dict.object(forKey: "levels") != nil) {
if let levelDict:[String : Any] = dict.object(forKey: "levels") as? [String : Any] {
for (key, value) in levelDict {
if ( key == "something") {
print(value)
}
}
}
}
However i have no idea how to access the legs array to get item titles

While reaching to the legs array which is of type [[String:String]] in your case with three elements and each index having two key pair values. You can loop over that index and get your values then iterate over to others.
let yourLegsArray:AnyObject = dict.object(forKey: "legs")! as AnyObject
then iterate over yourLegsArray and get the inside values of the Dict.

So I managed to find an answer to my original question
let path = Bundle.main.path(forResource: "Exercise", ofType: "plist")
let dict = NSDictionary(contentsOfFile: path!)!
let levelArray:AnyObject = dict.object(forKey: "legs")! as AnyObject
if let nsArray:NSArray = levelArray as? NSArray{
var levelDict:AnyObject = nsArray[1] as AnyObject //currentLevel is an Int
//then finally I could get data from a dictionary in the array
let Title = levelDict["title"] as AnyObject? as! String
print(Title)
}
So now this will print the second item in legs array which is "leg 2"

Related

How can i fetch specific data from firebase and store it in a dictionary?

I'm trying to fetch data from my firebase database, such that i can store it in a form of dictionary which is a type [String: [Any]] where key is the unique id and the value is a type of array which has the data stored under uniqueID->Question.
func getData(currUser: String, completion: #escaping (([String : [Any]]) -> ())) {
var newArray = [String : [Any]]()
let ref = Database.database().reference(fromURL: "MYURL").child("users/\(currUser)/Questions").observeSingleEvent(of: .value, with: { (snap) in
let enumerator = snap.children
while let rest = enumerator.nextObject() as? DataSnapshot, let value = rest.value{
newArray.updateValue([value], forKey: rest.key)
}
completion(newArray)
})
}
this completion block gives me:
["-LlpbizBpQTXOQ6zv0zd": [{
Qusetion = (
Hello,
test,
kkkkkkkkkkk
);
}]]]
Instead how can i get
["-LlpbizBpQTXOQ6zv0zd": [Hello,test,kkkkkkkkkkk]]
You're converting the value to a string, while it's actually a JSON object. That's why the value in your dictionary is a JSON object.
To only get the question text under Qusetion (typo?), you'll need to loop over that child snapshot and collect the individual values. Something like:
var newArray = [String : [Any]]()
let ref = Database.database().reference(fromURL: "MYURL").child("users/\(currUser)/Questions").observeSingleEvent(of: .value, with: { (snap) in
let enumerator = snap.children
while let rest = enumerator.nextObject() as? DataSnapshot {
var values = [String]
let valueEnumerator = rest.childSnapshot(atPath: "Qusetion").children
while let valueRest = valueEnumerator.nextObject() as? DataSnapshot, let value = rest.value {
values.append(value)
}
newArray.updateValue([values], forKey: rest.key)
}
completion(newArray)
})

Firebase Add New Key/Value to Pre-existing database without crashing xcode

` let query = ref?.child("Reviews").queryOrdered(byChild: "UserID").queryEqual(toValue: myUser.userId)
query?.observeSingleEvent(of: .value) { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let dict = snap.value as! [String: Any]
let uid = dict["UserID"] as! String
let review = dict["Body"] as! String
let rating = dict["Rating"] as! String
let titleID = dict["TitleID"] as! String
let reviewID = dict["ReviewID"] as! String
let ratingID = dict["RatingID"] as! String
`
THE ERROR OCCURS AT THE ratingID call to the database. It unwraps nil.
I am trying to adapt a pre existing Firebase database with a new key/value.
I then try to display entries in my tableview and I get a crash with unwrap returning nil. I know why this is happening and it's because the previous data does not have the new key/value I want to include in the node going forward. I have tried many different things such as if let and guard let without much fortune. How do I add new key/Values and still have the tableview read entries that don't have the new value?
I include an image of the current node and I want to add a 'RatingsID' to the node. When I do, I get the unwrap nil error.
Database node prior to new key/value
Your code is super close, just need to protect the code in case the ratingID key doesn't exist.
So change
let ratingID = dict["RatingID"] as! String
to
let ratingID = dict["RatingID"] as! String ?? ""
So if the RatingID node does not exist, you'll set ratingID to an empty string (or whatever string you want)
You could also code it to only operate on the value of that key if the node exists (not nil)
if let someVal = dict["xxx"] {
//do something with someVal
} else {
print("xxx node wasn't found")
}
Here's a complete example: We are reading some messages from the messages node and some of them have a test_key node and some dont. For those that don't, default string is assigned to test
let postsRef = self.ref.child("messages")
postsRef.observe(.childAdded) { snapshot in
let dict = snapshot.value as! [String: Any]
let msg = dict["msg"] as! String
let test = dict["test_key"] ?? "default string"
print(msg, test)
}

How to get some data out of an array in swift?

Here is my problem: I have an plist file, which have a simple strut:
root object is an array, and there are 2 sections in the array, each of them are dictionary including 2 pair key-value data:
and I'm going to create a tableView to show the datas, but I can't get the content out of the array :
here is how i declared my dataArray:
var plistArray = [AnyObject]()
can some one help me?
You need to properly cast at each level:
if let innerArray = plistArray[0] as? [AnyObject] {
if let dataDic = innerArray[indexPath.row] as? [String:String] {
if let imageName = dataDic["Pic"] {
cell?.imageView?.image = UIImage(named: imageName)
}
}
}
But why are you using AnyObject when you know what the plist contains? Use proper types. You know it's an array of arrays of dictionaries with String keys and String values.
var plistArray = [[[String:String]]]()
Then all you need is:
if let imageName = plistArray[0][indexPath.row]["Pic"] {
cell?.imageView?.image = UIImage(named: imageName)
}

Retrieve all values from child i collectionView

I´m trying to retrieve all the values from one child into a collectionView
I keep getting this error
Could not cast value of type '__NSDictionaryM' in this line
let follower = snapshotValue!["Followers"] as! String
here is my code
followRef.child("users").child((FIRAuth.auth()?.currentUser?.displayName)!).observe(FIRDataEventType.value, with: {
snapshot in
let snapshotValue = snapshot.value as? Dictionary<String,AnyObject>
let follower = snapshotValue!["Followers"] as! String
self.postsFollow.insert(postFollowStruct(follower: follower) , at: 0)
self.followersView.reloadData()
})
By what i understand you have many json objects in your snapshot.value? Then you want to cast to an object each one of them and put them into an array?
if let dict = snapshot.value as? [String: AnyObject]{
let followers = dict
for follower in followers {
if let restDict = follower.value as? [String:AnyObject] {
let followerObj = Follower()
followerObj.setValuesForKeys(restDict)
//add to array
}
}
}

Realm write / add with dictionaries

I have the following code:
var rawTypeArray = [[String: String]]()
rawTypeArray = NSArray(contentsOfFile: finalPathString) as! [[String: String]]
for eachType: [String: String] in rawTypeArray {
let setType = SetTypes(value: eachType)
try! realm.write {
realm.add(setType)
}
let all = realm.objects(SetTypes.self)
print (all)
}
the values of the dictionary are not passed to the realm object.
All the object are initialized with the default values. The dictionary values are ignored, what's wrong?