1

I asked another question similar to this one but I will try rewording it in order to make it clear exactly what it is I am trying to do. I currently have a JSON file that breaks down like so: There is a top level object, from that object there is an array of objects, each containing strings and ints. I want to get certain string values from these objects into an array that I can use to display in a UITableView and the only methods I see posted everywhere use this .append function, which is great when you are only checking the array to append one time maybe a few times a week but I would like the array to display the most current version every time the func is called.

So far I have tried many different methods in order to cast the Name string that is contained within each of the objects into an array by .appending an array in swift that I called NameList. Doing it this way causes the program to stall while it appends each of the names into the array. My last question got the recommendation of doing a plist instead but I think because I wasn't clear on exactly what problem I have been having was. So to sum it up again I just want to cast all the string values into an array that can be called by the tableview. Is there anyway to do this without appending the array each time and just have the strings from the json to go directly to the tableview? My current code is:

func updateArr() {        
    let path = NSBundle.mainBundle().pathForResource("CardList", ofType: "json")
    let jsonData = NSData(contentsOfFile: path!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: nil)
    let json = JSON(data: jsonData!)
    for (key, subJson) in json["array"] {
        if let Name = subJson["Name"].string {
            if let Class = subJson["Class"].number {
                if Class == Int(1) {
            cardNameList.append(Name)
            println(cardNameList)
                }
            }
        }
    }
}

2 Answers 2

1

Casting values from json object to defined values can be like that

if let Name:String = subJson["Name"] as! String {
       if let Class:Number = subJson["Class"] as! Number {

       }
}
Sign up to request clarification or add additional context in comments.

1 Comment

And doing it this way will allow it to go easily into an array without having to append array each time? Basically just looking to get ["Name"] in a tableview.
0

sorry for not continuing to answer the comments in the other post. Again I recommend to use a property list file.

It seems that you have an array of dictionaries with two keys name and Class, so the plist structure could look like

<dict>
    <key>array</key>
    <array>
        <dict>
            <key>name</key>
            <string>Alpha</string>
            <key>Class</key>
            <integer>1</integer>
        </dict>
        <dict>
            <key>name</key>
            <string>Beta</string>
            <key>Class</key>
            <integer>2</integer>
        </dict>
    <array>
</dict>

then you can easily filter and map the data with

if let cardArray = plistDict["array"] {
  let names = cardArray.filter {$0["Class"] == 1} .map {$0["name"]! }
}

plistDict is the deserialized content of the plist file

21 Comments

My only concern with converting to a plist is that I had also planned on using this database in android as well and update both simultaneously in the same language. From my understanding json is much easier to work with in android as far as simple databases are concerned.
I was under the impression, that the highest priority is speed. I guess this is one of the fastest ways to filter and map a large amount of data. And with ObjC or Swift it's very easy to convert JSON to plist and vice versa.
I was trying my hardest to avoid having to redo my whole database but you pretty much convinced me that I should. With a plist am I still going to have to parse the data into an array for displaying on a Tableview or will I be able to do it with only the mapped strings?
what information do you want to display (how many properties)? So far we have only name and Class
Class wont be displayed it is just a sort tool to determine which names might be stored. There are also 3 other ints and a description string which will be displayed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.