谷歌放置API“参考”密钥崩溃iPhone应用程序

时间:2012-05-15 01:06:07

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

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

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;它崩溃了。知道为什么会发生这种情况或者解决它的方法吗?

1 个答案:

答案 0 :(得分:0)

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

可能的解决方案:

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];