【发布时间】: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