为什么这段代码没有被执行?我从我的另一个项目中复制并粘贴它,它的工作正常。我也在我的其他应用程序中尝试了同样的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
答案 0 :(得分:1)
您确定您的“地理编码器”实例不是零吗? 如果您向“nil”对象发送消息,则不会发生任何事情...:)
NSLog(@"%@",geocoder);
答案 1 :(得分:0)
我仍然不确定发生了什么。我在我的模拟器上运行了以下代码(原始代码减去JSON操作),它运行得很好。也许你需要全新安装Xcode& iOS模拟器?
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *addressString = [NSString stringWithFormat:@"1 Infinite Loop, Cupertino, CA"];
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: %@",[anError description]);
}
}];
[geocoder 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;
}
@end
输出是:
2011-12-21 18:56:30.311 Test[44445:f803] AAAAAAAAAAAAAAAAAAAAAAAA
2011-12-21 18:56:30.312 Test[44445:f803] Placemark count:1
2011-12-21 18:56:30.314 Test[44445:f803] Placemark: 1 Infinite Loop, Cupertino, CA 95014-2083, United States @ <+37.33168400,-122.03075800> +/- 100.00m, region (identifier <+37.33168400,-122.03075800> radius 71.01) <+37.33168400,-122.03075800> radius 71.01m
我唯一能想到的是你可能太早发布你的地理编码器!也许尝试将版本移动到块中?这样,您就会知道地理编码器仅在完成地理编码操作后才会被释放。
另外,你在块里面的错误处理时犯了一个错误
它应该是NSLog(@"Error: %@",[anError description]);
而不是NSLog(@"Error: %@",[error description]);
。
另外,请确保您没有使用ARC ......
答案 2 :(得分:0)
我知道这是一个古老的问题,但这不起作用的原因是你无法同时发出多个转发请求。在发送另一个属性之前,您必须检查属性isGeocoding。