clifgriffin
clifgriffin

Reputation: 2053

How should I have done this? (initiating a large NSMutableArray)

File this under "just for fun", how would you have done this:

currencyLabels = [[NSMutableArray alloc] initWithObjects:@"Canadian Dollars", @"Euros", @"British Pounds", @"U.S. Dollars", @"United Arab Emirates Dirhams", @"Afghanistan Afghanis", @"Albania Leke", @"Armenia Drams", @"Netherlands Antilles Guilders", @"Angola Kwanza", @"Argentina Pesos", @"Aruba Guilders/Florins", @"Azerbaijan Manats", @"Bosnia/Herzegovina Convertible Marka", @"Barbados Dollars", @"Bangladesh Taka", @"Bulgaria Leva", @"Bahrain Dinars", @"Burundi Francs", @"Bermuda Dollars", @"Brunei Darussalam Dollars", @"Bolivia Bolivianos", @"Brazil Brazil Real", @"Bahamas Dollars", @"Bhutan Ngultrum", @"Botswana Pulas", @"Belarus Rubles", @"Belize Dollars", @"Congo/Kinshasa Congolese Francs", @"Switzerland Francs", @"Chile Pesos", @"China Yuan Renminbi", @"Colombia Pesos", @"Costa Rica Colones", @"Cuba Pesos", @"Cape Verde Escudos", @"Cyprus Pounds", @"Czech Republic Koruny", @"Djibouti Francs", @"Denmark Kroner", @"Dominican Republic Pesos", @"Algeria Algeria Dinars", @"Estonia Krooni", @"Egypt Pounds", @"Eritrea Nakfa", @"Ethiopia Birr", @"Fiji Dollars", @"Falkland Islands Pounds", @"Georgia Lari", @"Guernsey Pounds", @"Ghana Cedis", @"Gibraltar Pounds", @"Gambia Dalasi", @"Guinea Francs", @"Guatemala Quetzales", @"Guyana Dollars", @"Hong Kong Dollars", @"Honduras Lempiras", @"Croatia Kuna", @"Haiti Gourdes", @"Hungary Forint", @"Indonesia Rupiahs", @"Israel New Shekels", @"Isle of Man Pounds", @"India Rupees", @"Iraq Dinars", @"Iran Rials", @"Iceland Kronur", @"Jersey Pounds", @"Jamaica Dollars", @"Jordan Dinars", @"Japan Yen", @"Kenya Shillings", @"Kyrgyzstan Soms", @"Cambodia Riels", @"Comoros Francs", @"North Korea Won", @"South Korea Won", @"Kuwait Dinars", @"Cayman Islands Dollars", @"Kazakhstan Tenge", @"Laos Kips", @"Lebanon Pounds", @"Sri Lanka Rupees", @"Liberia Dollars", @"Lesotho Maloti", @"Lithuania Litai", @"Latvia Lati", @"Libya Dinars", @"Morocco Dirhams", @"Moldova Lei", @"Madagascar Ariary", @"Macedonia Denars", @"Myanmar (Burma) Kyats", @"Mongolia Tugriks", @"Macau Patacas", @"Mauritania Ouguiyas", @"Malta Liri", @"Mauritius Rupees", @"Maldives Rufiyaa", @"Malawi Kwachas", @"Mexico Pesos", @"Malaysia Ringgits", @"Mozambique Meticais", @"Namibia Dollars", @"Nigeria Naira", @"Nicaragua Cordobas", @"Norway Krone", @"Nepal Nepal Rupees", @"New Zealand Dollars", @"Oman Rials", @"Panama Balboa", @"Peru Nuevos Soles", @"Papua New Guinea Kina", @"Philippines Pesos", @"Pakistan Rupees", @"Poland Zlotych", @"Paraguay Guarani", @"Qatar Rials", @"Romania New Lei", @"Serbia Dinars", @"Russia Rubles", @"Rwanda Rwanda Francs", @"Saudi Arabia Riyals", @"Solomon Islands Dollars", @"Seychelles Rupees", @"Sudan Dinars", @"Sweden Kronor", @"Singapore Dollars", @"Saint Helena Pounds", @"Slovenia Tolars", @"Slovakia Koruny", @"Sierra Leone Leones", @"Somalia Shillings", @"Seborga Luigini", @"Suriname Dollars", @"El Salvador Colones", @"Syria Pounds", @"Swaziland Emalangeni", @"Thailand Baht", @"Tajikistan Somoni", @"Turkmenistan Manats", @"Tunisia Dinars", @"Tonga Pa'anga", @"Turkey New Lira", @"Trinidad and Tobago Dollars", @"Tuvalu Dollars", @"Taiwan New Dollars", @"Tanzania Shillings", @"Ukraine Hryvnia", @"Uganda Shillings", @"Uruguay Pesos", @"Uzbekistan Sums", @"Venezuela Bolivares", @"Viet Nam Dong", @"Vanuatu Vatu", @"Samoa Tala", @"Silver Ounces", @"Gold Ounces", @"East Caribbean Dollars", @"Palladium Ounces", @"Platinum Ounces", @"Yemen Rials", @"South Africa Rand", @"Zambia Kwacha", @"Zimbabwe Dollars",nil];

It's a static list..never changes. Using it for a "look up" style detail view.

I look forward to your ideas. :)

Upvotes: 3

Views: 174

Answers (3)

Jonah
Jonah

Reputation: 17958

currencyLabels = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"currencyLabels" ofType:@"plist"]] retain];

Data driven because there's no need to hard code configuration like this and it should be possible to change this sort of data without a code change. Loaded via NSBundle so you can localize the plist if necessary. Retained to match the object ownership shown in your example.

Upvotes: 2

Mike Bretz
Mike Bretz

Reputation: 1986

I do not yet exactly understand what kind of answer you expect... if it is for code shortness then this approach will help:

NSString *list = @"Canadian Dollars|Euros|British Pounds...";
NSArray *listItems = [list componentsSeparatedByString:@"|"];

You could also save your list items into a text file each entry into on line. Load that file as string and separate it by newline to get your items

Upvotes: 1

James J
James J

Reputation: 6458

Even though it "never changes" (way to jinx it, by the way), I would have thrown it into a property list and used arrayWithContentsOfFile: to read it in to a static variable. This makes it more readable, and would potentially let you reuse it for other things.

Upvotes: 6

Related Questions