Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift addObject

Tags:

json

arrays

swift

So, I'm learning how to get data from DB with JSON and then put the data on some array. Problem accours on last line, citiesArray.addObject(City()), when I need to put all data from object city (id, name, state,...) into array. I was looking line by line with compiler, and basically everything is fine except that at the end, my array is still empty (its value is nil)?

for (var i=0;i<jsonArray.count;i++){
    //Create city objec
    var cID: AnyObject? = jsonArray.objectAtIndex(i).objectForKey("id") as NSString
    var cName: AnyObject? = jsonArray.objectAtIndex(i).objectForKey("cityName") as NSString
    var cState: AnyObject? = jsonArray.objectAtIndex(i).objectForKey("cityState") as NSString
    var cPopulation: AnyObject? = jsonArray.objectAtIndex(i).objectForKey("cityPopulation") as NSString
    var cCountry: AnyObject? = jsonArray.objectAtIndex(i).objectForKey("country") as NSString

    //add city obj (i have City class) to city array
    var city = City()

    city.cityID = cID as NSString
    city.cityName = cName as NSString
    city.cityState = cState as NSString
    city.cityPopulation = cPopulation as NSString
    city.cityCountry = cCountry as NSString

    citiesArray.addObject(City())
}
like image 774
Jaco2201 Avatar asked Nov 05 '14 20:11

Jaco2201


2 Answers

A couple of issues:

  1. You suggested that you were trying to add the city with the following line of code:

    citiesArray.addObject(City())
    

    The City() construct will instantiate a new, blank City object. So that line of code would, best case scenario, add a blank City object to your array, which is not what you intended.

    When you add the city to your citiesArray, you should simply:

    citiesArray.addObject(city)
    
  2. You say you've defined your citiesArray like so:

    var citiesArray: NSMutableArray!
    

    You also need to instantiate an object for this variable (i.e. create an object to which this variable will now point), e.g.:

    citiesArray = NSMutableArray()
    
  3. You are reporting, though, that at the end of this loop, that citiesArray is nil. Really?!? But if you tried to call the addObject method and citiesArray was nil, you could have received a fatal error: "unexpectedly found nil while unwrapping an Optional value".

    So, if citiesArray was nil, then jsonArray must have been empty, too. Or for some reason you didn't even get to this loop. I would suggest (a) logging jsonArray; and (b) log or put breakpoint inside this loop and confirm you're even getting in here like you think you are.

    Also, check the timing of this (i.e. make sure your statement logging citiesArray is actually taking place after this routine that populates it). I know that sounds crazy, but if you are retrieving the data from some network resource asynchronously, you could have some timing related issues.

  4. Since you're writing Swift code, you might consider using Swift arrays. For example, define your array variable as

    var citiesArray: [City]!
    

    And instantiate it with:

    citiesArray = [City]()
    

    And add objects to it with:

    citiesArray.append(city)
    
like image 200
Rob Avatar answered Sep 29 '22 13:09

Rob


I am pretty sure you need to use the append function:

 citiesArray.append(city)

or

if you want to append at the start of the array

 citiesArray.insert(city, atIndex: 0)

instead of

citiesArray.addObject(City())

here is a little example: Syntax might not be 100% not on comp with xcode right now.

 var strA:String = "apple"
 var strB:String = "pineapple"

 var strArr = ["kiwi", "mango", "lime"]

 strArr.append(strA)
 println(strArr.count) //4 ["kiwi", "mango", "lime", "apple"]


 citiesArray.insert(strB, atIndex: 0)
 println(strArr.count) //5 ["pineapple", "kiwi", "mango", "lime", "apple"]
like image 39
the_pantless_coder Avatar answered Sep 29 '22 11:09

the_pantless_coder