斯威夫特ADDOBJECT [英] Swift addObject

查看:171
本文介绍了斯威夫特ADDOBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我学习如何从数据库使用JSON获取数据,然后把一些阵列上的数据。问题accours上最后一行, citiesArray.addObject(城()),当我需要把从对象中的所有数据城市(ID,名称,状态,...)到数组。
我一直在寻找一行行与编译器,基本上一切都很好,只是到了最后,我的数组仍然是空的(其值)?

 为(VAR I = 0; I< jsonArray.count;我++){
    //创建城市objec
    VAR CID:AnyObject? = jsonArray.objectAtIndex(I).objectForKey(ID)作为的NSString
    VAR CNAME:AnyObject? = jsonArray.objectAtIndex(ⅰ).objectForKey(的cityName)作为的NSString
    VAR cState:AnyObject? = jsonArray.objectAtIndex(ⅰ).objectForKey(cityState)作为的NSString
    VAR cPopulation:AnyObject? = jsonArray.objectAtIndex(ⅰ).objectForKey(cityPopulation)作为的NSString
    VAR cCountry:AnyObject? = jsonArray.objectAtIndex(I).objectForKey(国家)作为的NSString    //添加城市OBJ(我有市级)城市阵列
    VAR城市=城()    city​​.cityID = CID作为的NSString
    city​​.cityName = CNAME作为的NSString
    city​​.cityState = cState作为的NSString
    city​​.cityPopulation = cPopulation作为的NSString
    city​​.cityCountry = cCountry作为的NSString    citiesArray.addObject(城())
}


解决方案

几个问题:


  1. 您认为您试图与下面的行code的添加城市:

      citiesArray.addObject(城())

    城()构造将实例化一个新的空白城市对象。所以,code的该行会,最好的情况下,添加一个空白城市对象阵列,这是不是你的本意。

    当您添加城市你的 citiesArray ,您应该简单:

      citiesArray.addObject(市)


  2. 您说您已经定义了 citiesArray 像这样:

      VAR citiesArray:NSMutableArray里!

    您还需要实例化一个对象,此变量(即创建一个对象来此变量现在点),例如:

      citiesArray = NSMutableArray的()


  3. 您的报告,虽然,在这个循环结束,即 citiesArray 。真?!?但是,如果你试着拨打 ADDOBJECT 方法和 citiesArray ,你可以收到一个致命的错误:意外地发现零而展开可选的值

    所以,如果 citiesArray ,那么 jsonArray 必须已经空了。或因某些原因,你甚至没有得到这个循环。我建议(一)日志记录 jsonArray ;和(b)日志或将断点这个循环中,并确认你甚至在这里就像你以为你是得到。

    此外,检查这个时间(即确保您的语句记录 citiesArray 实际发生的之后的这个例程,填充它)。我知道这听起​​来很疯狂,但如果你是从一些网络资源异步检索数据,你可以有一些时间相关的问题。


  4. 既然你写斯威夫特code,你可能会考虑使用雨燕的阵列。例如,定义数组变量为

      VAR citiesArray:[城市]!

    和与实例吧:

      citiesArray = [城市]()

    和对象与添加到它:

      citiesArray.append(市)


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())
}

解决方案

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)
    

这篇关于斯威夫特ADDOBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆