【问题标题】:Google places API "reference" key crashes iPhone app谷歌放置 API“参考”密钥使 iPhone 应用程序崩溃
【发布时间】:2012-05-15 01:06:06
【问题描述】:

我对目标 c 有点陌生,遇到了一个有趣的问题。我使用 JSON 从 google places api 获取搜索结果,效果很好。找到该地点后,我想加载显示业务详细信息的第二个屏幕。我计划通过地点 API 使用地点详细信息搜索来做到这一点。为了在两个视图之间传递信息,我创建了一个数据类来保存变量。

DataClass.h

#import <Foundation/Foundation.h>

@interface DataClass : NSObject {    

    NSString *Lat;
    NSString *Long;
    NSString *barLat;
    NSString *barLong;
    NSString *Ref;
    NSString *barName;

}    
@property(nonatomic,retain)NSString *Lat;   
@property(nonatomic,retain)NSString *Long;  
@property(nonatomic,retain)NSString *barLat;  
@property(nonatomic,retain)NSString *barLong;  
@property(nonatomic,retain)NSString *Ref;
@property(nonatomic,retain)NSString *barName;
+(DataClass*)getInstance; 

@end

DataClass.m

#import "DataClass.h"

@implementation DataClass
@synthesize Lat;
@synthesize Long;
@synthesize barLat;
@synthesize barLong;
@synthesize Ref;
@synthesize barName;
static DataClass *instance =nil;    
+(DataClass *)getInstance    
{    
    @synchronized(self)    
    {    
        if(instance==nil)    
        {    

            instance= [DataClass new];    
        }    
    }    
    return instance;    
}   
@end

在我的第一个视图中,我像这样添加参考值:

DataClass *obj=[DataClass getInstance];  
NSString *barRef = [NSString stringWithFormat:@"%@", searchResult.reference];
obj.Ref = barRef; 

searchResult.reference 是有效的,当我将它放入 barRef 并使用 NSLog 时,它会正确输出,但是当我尝试将它添加到 obj 对象时,它会使应用程序崩溃。字符串看起来像这样

"CnRrAAAABX_U5FcybhlJgmWGAv19Fhemk_Bu7ytKKuL33201sKfce2aIzeZ2P8cWdKPV8hCsbUbAzYcoA9QDmbMPeYqCX8idypsQH4LXvGwxW_qtW4jBod2bufelyxeLaBlS1DoNfDtaH4evksVluW9gsqCGcRIQkJXwM_RcSewilknJowaghhoUFoR64jZTUDCsrXvmOqg4eqJx5uU"

即使我使用

NSString *barRef = @"CnRrAAAABX_U5FcybhlJgmWGAv19Fhemk_Bu7ytKKuL33201sKfce2aIzeZ2P8cWdKPV8hCsbUbAzYcoA9QDmbMPeYqCX8idypsQH4LXvGwxW_qtW4jBod2bufelyxeLaBlS1DoNfDtaH4evksVluW9gsqCGcRIQkJXwM_RcSewilknJowaghhoUFoR64jZTUDCsrXvmOqg4eqJx5uU";

然后 obj.Ref = barRef;它崩溃了。知道为什么会发生这种情况或有解决方法吗?

【问题讨论】:

  • 原来这不是我的问题。当遍历 JSON 数据以从地点详细信息 api 返回详细信息时,我收到以下错误。我正在使用与上一个 api 请求相同的代码来提取和解析 JSON 数据,但它会引发此错误”*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[__NSCFString objectForKey:] : unrecognized selector sent to instance 0x7bbdab0' 我使用的代码是 barDetail.phone = [dictionary objectForKey:@"formatted_phone_number"]; 这与我之前使用的 JSON 数据相同

标签: objective-c xcode ios5 google-places-api


【解决方案1】:

看来,您的字典对象不是字典,而是字符串。

可能的解决方案:

1) 使用调试器检查您的字典中是否有 @"formatted_phone_number" 键。 调试您的代码并检查您的字典是否不错。你也可以使用 NSLog 来输出你的字典内容,看看好不好。

2) 如果您的字典不好,请检查您是否有类似的内容。这就是我解析来自 Google API 的 JSON 格式的 HTML 响应的方式:

NSDictionary *dic;
SBJsonParser *jsonParser = [SBJsonParser new];
NSError *error = NULL;
NSURLResponse *response;
NSHTTPURLResponse *httpResponse;

    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlString];

    //URL Request Object
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:userWaitTime];

    NSData *data = [NSURLConnection sendSynchronousRequest:request 
                                         returningResponse:&response error:&error];

    NSString *htmlString = [[NSString alloc]  initWithBytes:[data bytes]
                                                     length:[data length] encoding: NSUTF8StringEncoding];

    //NSLog (@"htmlString: %@", htmlString);

    dic = [jsonParser objectWithString:htmlString error:nil];

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多