Strings from NSArray into annotation, mapkit
I am trying to populate the Title, and Subtitle using mapkit with the following lines of code. The textItems array holds two strings.
NSArray *textItems = [searchString componentsSeparatedByString:@","];
addAnnotation =
[[AddressAnnotation alloc] initWithCoordinate:location
mTitle:[[textItems objectAtIndex:0] stringValue]
mSubTitle:[[textItems objectAtIndex:1] stringValue]];
The app stops when 开发者_JS百科it reaches the 'addAnnotation'.
If I change mTitle:[[textItems objectAtIndex:0] stringValue]
to mTitle:@"test"
i.e. it works fine. When I debug I can see that the data in the textItems array is present.
Any ideas?
Thanks.
The componentsSeparatedByString
method returns an array of NSString
objects.
You are calling stringValue
on those objects but stringValue
applies to NSNumber
objects--not NSString
so you must be getting an "unrecognized selector" error.
Remove the calls to stringValue
:
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location
mTitle:[textItems objectAtIndex:0]
mSubTitle:[textItems objectAtIndex:1]];
However, it would still be a good idea to check the count before accessing those indexes in the array and use a default value if the array returns only 0 or 1 objects.
I would set a breakpoint between the first and second line you posted. When you get there, go down to the console and type "po textItems" and "po [textItems count]". They will print the array and the number of objects in the array respectively. At the very least it's a check to make sure that you're getting the number of objects in the array you expect.
精彩评论