-5

I have an NSString with the value of:

NSString *combined = "10014, 40.734, -74.0053";

I would like to break this string where there are commas into three NSStrings that called bZip, bLat, and bLon so that the values would be like below:

bZip = 10014 
bLat = 40.734 
bLon = -74.0053

I would then like to use the bLat and bLon coordinates to update the center location on a MapView. The MapView pulls data from a JSON feed and plots the markers out, but I need to re-focus the map on the coordinates that were saved in the values for bLat and bLon.

I am assuming that I need to make this change after the line:

[self.mapView1 addAnnotations:newAnnotations];

Does anyone know how to help me make this happen? Thank you all!

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                 options:0
                                                   error:&error];

CLLocationCoordinate2D location;                        
NSMutableArray *newAnnotations = [NSMutableArray array]; 
MapViewAnnotation *newAnnotation;                        

for (NSDictionary *dictionary in array)
{   
    location.latitude = [dictionary[@"placeLatitude"] doubleValue];
    location.longitude = [dictionary[@"placeLongitude"] doubleValue];

    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"name"]
                                               andCoordinate:location];

    [newAnnotations addObject:newAnnotation];


}

[self.mapView1 addAnnotations:newAnnotations];
Brandon
  • 2,163
  • 6
  • 40
  • 64
  • 2
    1. This is not related to Xcode. 2. Have you googled this? There are **tons** of dupes on Stack Overflow and at other places as well. 3. If not, have you at least **tried** to do something yourself? This is **trivial,** I could enumerate 3 or 4 ways to do this off the top of my head. –  Jun 11 '13 at 07:36
  • possible duplicate of [Objective-C Split()?](http://stackoverflow.com/questions/3558888/objective-c-split) and [this](http://stackoverflow.com/questions/259956/nsstring-tokenize-in-objective-c) and [this one too](http://stackoverflow.com/questions/5095349/how-to-split-strings-in-objective-c) and [this question as well.](http://stackoverflow.com/questions/3470756/split-one-string-into-different-strings) –  Jun 11 '13 at 07:36
  • @H2CO3, thanks for letting me know not ton include Xcode in the tag for iOS development questions. In answer to question 2 - yes, I have googled it. Not quite yet trivial for me, but one day hope to be able to rattle off 3 or 4 ways to do this. Thanks much :) – Brandon Jun 11 '13 at 07:37
  • @H2CO3, recommend closing this? or should I leave it open for others to learn from. Don't want to clutter SO... thanks for the guidance. – Brandon Jun 11 '13 at 07:40
  • Since there are already four dupes, I'd recommend that you please delete it. In my opinion, we don't need more of this question. (You'll also get your lost 14 reputation back.) –  Jun 11 '13 at 07:41
  • It is too late to delete it on your own now, because it has answers. – borrrden Jun 11 '13 at 07:41
  • will do. thanks much! wow that hurt my points. I'll recover. – Brandon Jun 11 '13 at 07:41
  • Im closing it now. sorry for the distraction. – Brandon Jun 11 '13 at 07:42
  • @Brandon No problem, it'll then most likely be community-deleted. If you take a piece of advice, next time it'll be worth using the search feature of Stack Overflow itself too (with some relevant keywords, you are very likely to find an already existing answer to basic questions.) –  Jun 11 '13 at 07:44
  • thanks @H2CO3. Any idea how to unlock an account. Reputation was destroyed and can't ask questions (wasn't planning to ask another question, but have heard of this happening). Will this unlock itself over time? – Brandon Jun 11 '13 at 07:45
  • thanks @H2CO3. I'll learn better. Have a good one. – Brandon Jun 11 '13 at 07:53
  • Thank you all for keeping SO free of poor questions. I apologize mine. In an attempt to make it more appropriate for SO, I have updated my question to target a more problematic issue that I am facing. Please feel free to help me out. If you think this question is more in line with the guidelines of SO, please also feel free to up vote, or un-downvote, this question. Thank you all! – Brandon Jun 11 '13 at 09:41
  • @Brandon: Don't do that. you'll get more down votes. You should ask different question for different problem. This is not the good way to get up-votes. – Bhavin Jun 11 '13 at 09:52
  • Oh. Ok Vin. Got blasted by this question and lost ability to ask questions. Trying to regain some composure. Thanks for message. – Brandon Jun 11 '13 at 10:01

3 Answers3

3

Sample Code :

NSArray *stringArray = [combined componentsSeparatedByString: @","];

NSString *bZip = [stringArray objectAtIndex:0];
NSString *bLat = [stringArray objectAtIndex:1];
NSString *bLon = [stringArray objectAtIndex:2];
Bhavin
  • 27,155
  • 11
  • 55
  • 94
2
 NSString *combined = @"10014, 40.734, -74.0053";
 NSArray *array = [combined componentsSeparatedByString:@","];
the1pawan
  • 1,194
  • 9
  • 26
1

Try to use this one

    NSString *combined = "10014, 40.734, -74.0053";
    NSArray *data = [combined componentsSeparatedByString:@", "];

NSLog(@"bZip = %@",[data objectAtIndex:0]);
NSLog(@"bLat = %@",[data objectAtIndex:1]);
NSLog(@"bLon = %@",[data objectAtIndex:2]);
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66