I am somewhat new to objective c and have run into an interesting issue. I use JSON to get search results from google places api which works fine. Once the place is found I want to load a second screen that that displays the details of the business. I plan on doing this by using the places details search through the places API. In order to pass information between the two views I have created a data class to hold the variables.
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
In my first view I add the reference value like this:
DataClass *obj=[DataClass getInstance];
NSString *barRef = [NSString stringWithFormat:@"%@", searchResult.reference];
obj.Ref = barRef;
The searchResult.reference is valid and when I put it into barRef and use NSLog it outputs correctly but when I try to add it to the obj object it crashes the app. The string looks like this
“CnRrAAAABX_U5FcybhlJgmWGAv19Fhemk_Bu7ytKKuL33201sKfce2aIzeZ2P8cWdKPV8hCsbUbAzYcoA9QDmbMPeYqCX8idypsQH4LXvGwxW_qtW4jBod2bufelyxeLaBlS1DoNfDtaH4evksVluW9gsqCGcRIQkJXwM_RcSewilknJowaghhoUFoR64jZTUDCsrXvmOqg4eqJx5uU”
and even if I use
NSString *barRef = @”CnRrAAAABX_U5FcybhlJgmWGAv19Fhemk_Bu7ytKKuL33201sKfce2aIzeZ2P8cWdKPV8hCsbUbAzYcoA9QDmbMPeYqCX8idypsQH4LXvGwxW_qtW4jBod2bufelyxeLaBlS1DoNfDtaH4evksVluW9gsqCGcRIQkJXwM_RcSewilknJowaghhoUFoR64jZTUDCsrXvmOqg4eqJx5uU”;
and then obj.Ref = barRef; it crashes. Any idea why this would be happening or a way to fix it?
It seems, your dictionary object is not a dictionary, but string.
Probable solutions:
1) Check if you have @”formatted_phone_number” key in your dictionary, using debugger.
Debug your code and check if your dictionary is nice. You can also use NSLog to output your dictionary content and see, if it is good.
2) If your dictionary is bad, check if you have something like this. This is how I parse an HTML response from Google API that is in JSON format: