Question

Don't understand why my NSString data is null.

I am trying to parse a number string from a json website from my app. It works successfully, however when I am trying to show case that number in my view controller it reads null. I will post an example code for everyone to understand better.

Handler.m

JsonModel *jsonModel =[[JsonModel alloc]init];
jsonModel.idNumber = [jsonDict valueForKey:@"id"];
nslog(@"%@",jsonModel.idNumber);

JsonModel.h

 #import <Foundation/Foundation.h>

 @interface JsonModelModel : NSObject

 @property (nonatomic, strong) NSString *idNumber;

JsonModel.m

#import "JsonModel.h"

@implementation JsonModel
@synthesize idNumber;

This part works successfully, I am able to get the necessary data parsed and the NSLog proves it. However, here comes the problem. P.S. idNumber is the NSString.

ViewController.m

-(void)method{
JsonModel *jsonModel =[[JsonModel alloc]init];
NSLog(@"%@",jsonModel.idNumber);
}

The NSLog appears null for some reason. Even though the json data was parsed first then the method in view controller is called.I must be missing some small detail. Will appreciate it if someone helped me figure it out.

Was it helpful?

Solution

You are creating a completely new, empty JsonModel object when you execute

JsonModel *jsonModel =[[JsonModel alloc]init];

So this instance of JsonModel is completely unrelated to the one you created previously (and set jsonModel.idNumber).

So ultimately what you need to do is keep the original jsonModel object that you created, and use that same object in the view controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top