2015-11-24 11 views
11

w Javie, możemy utworzyć ArrayList obiektu tak:Jak utworzyć ArrayList obiektu w szybkim

ArrayList<Country> countryList = new ArrayList<Country>(); 

     Country aBucket = new Country(); 
     aBucket.setName("Canada"); 
     aBucket.setCity("Ottawa"); 
     countryList.add(aBucket); 

lub jak w ten sposób:

ArrayList<Matrices> list = new ArrayList<Matrices>(); 
list.add(new Matrices(1,1,10)); 
list.add(new Matrices(1,2,20)); 

ale jak mogę dostać te same rzeczy/alternatywa w SWIFT

Odpowiedz

19

Możesz to zrobić za pomocą tablicy. Aby uzyskać więcej informacji o tablicach, spójrz na here. Można dodawać obiekty za pomocą funkcji append(...).

var array = [Country]() //alternatively (does the same): var array = Array<Country>() 
array.append(Country()) 
array.append(Country()) 
3

Próbując uczynić kod tak blisko do kodu przykładu, to moja odpowiedź (wymaga zadeklarowały gdzieś klasa Kraj:

var countryList : Array<Country> = Array() 
var aBucket  : Country  = Country() 
.... 
countryList.append(aBucket) 

Nadzieja to pomaga

Powiązane problemy