【发布时间】:2011-12-18 02:49:11
【问题描述】:
为什么这段代码没有被执行?我从我的另一个项目中复制并粘贴了它,它工作得很好。我还在我的另一个应用程序中尝试了它,使用我在这里插入的相同 addressString,它运行良好。
NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
NSLog(@"Address string: %@",addressString);
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(@"Placemark count:%d",[placemarks count]);
for(CLPlacemark *placemark in placemarks)
{
NSLog(@"%@",placemark);
}
if(anError)
{
NSLog(@"Error: %@",[error description]);
}
}];
任何地标和错误消息都不会记录到控制台。
这是我的整个 AppDelegate.m:
@implementation AppDelegate
@synthesize window = _window;
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSError *error = nil;
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSString *JSONString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Streets" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
if(error)
{
NSLog(@"%@",[error description]);
NSLog(@"Break");
}
NSDictionary *dict = [parser objectWithString:JSONString error:&error];
if(error)
{
NSLog(@"%@",[error description]);
NSLog(@"Break");
}
NSArray *addresses = [[dict objectForKey:@"results"] retain];
NSDictionary *boroughs = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Bronx",@"Brooklyn",@"New York", @"Queens",@"Staten Island",nil] forKeys:[NSArray arrayWithObjects:@"B",@"K",@"M",@"Q",@"S", nil]];
int i = 1;
for(NSDictionary *file in addresses)
{
NSString *borough = [file objectForKey:@"Borough"];
NSString *ID = [file objectForKey:@"ID"];
NSString *leftBound = [file objectForKey:@"LeftBound"];
NSString *rightBound = [file objectForKey:@"RightBound"];
NSString *sideOfStreet = [file objectForKey:@"SideOfStreet"];
NSString *street = [file objectForKey:@"Street"];
NSString *addressString = [NSString stringWithFormat:@"%@ and %@, %@, NY", street, rightBound, [boroughs objectForKey:borough]];
// NSLog(@"Address string: %@",addressString);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *anError)
{
NSLog(@"AAAAAAAAAAAAAAAAAAAAAAAA");
NSLog(@"Placemark count:%d",[placemarks count]);
for(CLPlacemark *placemark in placemarks)
{
NSLog(@"Placemark: %@",placemark);
}
if(anError)
{
NSLog(@"Error: %@",[error description]);
}
}];
[geocoder release];
NSLog(@"%d",i++);
}
[parser release];
[addresses release];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"geocoder"])
{
NSLog(@"AAAAAAA");
}
}
@end
【问题讨论】:
标签: iphone ios geocoding objective-c-blocks