Voyager
Voyager

Reputation: 399

Add array object not init

I have a method that init my class object from JSON. The method like this:

func getList (dic : Array<[String:AnyObject]>)  -> Array<Dog> {
    let dogs : NSMutableArray = NSMutableArray()
    for myDic in dic {
        let dog = Dog.init(dog: myDic)
        dogs.addObject(dog)
    }
    return NSArray(dogs) as! Array<Dog>
}

And I can present it on tableview without issue. But right now I want to make pagination for the list. If I run the method getList again it will be init new object and replacing my old one. How can I to add a new object to exisiting. I don't want to create separate object with the same property.

Upvotes: 0

Views: 46

Answers (1)

ganzogo
ganzogo

Reputation: 2614

You need to add a member variable which you will append to. So:

var dogs: [Dog] = []

And then call getList like this:

dogs += getList(dic: myDic)

By the way, you can make your getList method much simpler, like this:

func getList(dic: [[String: AnyObject]]) -> [Dog] {
  return dic.map(Dog.init)
}

Upvotes: 1

Related Questions