开发者

Cocoa Referencing NSArray Values

I have a string of data "latitude,longitude,location" stored as an NSString.

I want to reference the da开发者_如何学Gota in different ways so I stored it into an NSArray.

I then store the data in easy to read NSString values and want to reuse them a couple times.

- (IBAction)convertButtonPressed:(id)sender
{
NSString *returnData = @"49.1002,-102.1253,12-03-22-W3";

explode = [returnData componentsSeparatedByString:@","];

Latitude    = [explode objectAtIndex: 0];
Longitude   = [explode objectAtIndex: 1];
Location    = [explode objectAtIndex: 2];
Query.text  = [NSString stringWithFormat:@"%@", Location];
Result.text = [NSString stringWithFormat:@"%@, %@", Latitude, Longitude];
}

I then set these values into UILabel values. I have no issues at this point. The next thing I want the user to be able to do is plot this data onto a MKMapView. So I have a UIButton setup to plot the data. When it is clicked, a new ViewController slides into view and I try to pass the data, however I am not having any luck. Instead of passing data, my application crashes.

- (IBAction)plotButtonPressed:(id)sender;
{
ShowMapViewController *showMap = [[ShowMapViewController alloc] initWithNibName:nil bundle:nil];

showMap.currentLocation = Location;

showMap.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:showMap animated:YES];
}

I guess my simple question for this is... How do I properly store my variables so that I can pass them onto my ShowMapViewController.


It looks like you're using the ivar Longitude in two places. In convertButtonPressed: you are extracting it from an array using objectAtIndex: and in plotButtonPressed: you are reusing the value.

Note that when you get the value for Location you are not using a method that starts with new, alloc, or copy, which means that, if you want the value to live beyond the current autorelease pool, you must manually retain or copy it. You aren't doing that here, so some time between convertButtonPressed: and plotButtonPressed:, the autorelease pool is being popped, and your object deallocated, leaving you with a dangerous dangling pointer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜