Help with a loop to return UIImage from possible matches
Posted
by Canada Dev
on Stack Overflow
See other posts from Stack Overflow
or by Canada Dev
Published on 2010-05-18T11:18:16Z
Indexed on
2010/05/18
11:20 UTC
Read the original article
Hit count: 210
I am parsing a list of locations and would like to return a UIImage with a flag based on these locations.
I have a string with the location. This can be many different locations and I would like to search this string for possible matches in an NSArray, and when there's a match, it should find the appropriate filename in an NSDictionary.
Here's an example of the NSDictionary and NSArray:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"franceFlag", @"france",
@"greeceFlag", @"greece",
@"spainFlag", @"spain",
@"norwayFlag", @"norway",
nil];
NSArray *array = [NSArray arrayWithObjects:
@"france"
@"greece"
@"spain"
@"portugal"
@"ireland"
@"norway",
nil];
Obviously I'll have a lot more countries and flags in both.
Here's what I have got to so far:
-(UIImage *)flagFromOrigin:(NSString *)locationString {
NSRange range;
for (NSString *arrayString in countryArray) {
range = [locationString rangeOfString:arrayString];
if (range.location != NSNotFound) {
return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[dictionary objectForKey: arrayString] ofType:@"png"]];
}
}
return nil;
}
Now, the above doesn't actually work. I am missing something (and perhaps not even doing it right in the first place) The issue is, the locationString could have several locations in the same country, described something like this "Barcelona, Spain", "Madrid, Spain", "North Spain", etc., but I just want to retrieve "Spain" in this case. (Also, notice caps for each country).
Basically, I want to search the locationString I pass into the method for a possible match with one of the countries listed in the NSArray. If/When one is found, it should continue into the NSDictionary and grab the appropriate flag based on the correct matched string from the array. I believe the best way would then to take the string from the array, as this would be a stripped-out version of the location.
Any help to point me in the right direction for the last bit is greatly appreciated.
© Stack Overflow or respective owner