我對於目標c有點新,並遇到了一個有趣的問題。我使用JSON從谷歌地方api得到的搜索結果,它工作正常。找到該地點後,我想加載顯示業務詳情的第二個屏幕。我計劃通過使用Places API的地點詳情搜索來完成此操作。爲了在兩個視圖之間傳遞信息,我創建了一個數據類來保存變量。谷歌地方API「參考」鍵崩潰iPhone應用程序
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返回詳細信息時,出現以下錯誤。我使用相同的代碼來拉和解析JSON數據,因爲我與之前的api請求一樣,但它引發了這個錯誤「***由於未捕獲的異常'NSInvalidArgumentException'終止應用程序,原因:' - [__ NSCFString objectForKey:] :無法識別的選擇發送到實例0x7bbdab0'我使用的代碼是barDetail.phone = [dictionary objectForKey:@「formatted_phone_number」];這與我以前使用的JSON數據相同 –