我遇到問題。我想更改塊中的局部變量。我嘗試了一切。我把變量__block的開始,但沒有什麼改變。我不能改變塊中的myString值。你可以幫幫我嗎 ?這裏是我的代碼:在目標C中更改塊中的變量
- (IBAction)setButtonPressed:(id)sender {
placeName = [[[NSMutableString alloc] init] retain];
__block NSMutableString *myString = [[NSMutableString alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MM.yyyy hh:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:today];
[previousParkingLon addObject:[NSNumber numberWithFloat:self.longitude]];
[previousParkingLat addObject:[NSNumber numberWithFloat:self.latitude]];
[previousParkingsDate addObject:dateString];
if((self.latitude != 0) && (self.longitude != 0))
{
self.geocoder = [[CLGeocoder alloc] init ];
[self.geocoder reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *myplacemark = [placemarks objectAtIndex:0];
NSString *locatedAt = [[[myplacemark addressDictionary] objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
[myString setString:locatedAt];
}];
}
[previousParkingsPlaceName addObject:placeName];
NSLog(@"Test %@",myString);
[defaults setObject:previousParkingLat forKey:@"historyLat"];
[defaults setObject:previousParkingLon forKey:@"historyLon"];
[defaults setObject:previousParkingsDate forKey:@"historyDate"];
[defaults setObject:previousParkingsPlaceName forKey:@"historyName"];
[defaults synchronize];
}
你的問題是什麼? – 2013-03-24 01:11:48
並且可以標記給您帶來麻煩的代碼行。 – 2013-03-24 01:14:03
你甚至不需要'__block',因爲變量沒有被賦值爲其他值。還有問題是在執行塊之前打印'myString'的值 – 2013-03-24 01:16:39