简体   繁体   中英

iOS: Object returns nil description after alloc and init

Object returns nil description after alloc and init..can any one tell me i have to display the data which comes from server...when i clicks on edit button it goes to shoow room class in that class.. inside textfields hav to display the information .can any one help me

@interface HeadOfficeDetails : NSObject

@property(nonatomic,strong) NSMutableArray *ofcContact;

@property(nonatomic,strong) OfficeDocument *ofcDocument;
@property(nonatomic,strong) OfficePrice *ofcPrice;
@property(nonatomic,strong) CityClass *city;
@property(nonatomic,strong) StateClass *state;
@property(nonatomic,strong) DistrictClass *district;

@property(nonatomic,strong)NSString *shrtname;
@property(nonatomic,strong)NSString *shwrmname;
@property(nonatomic,strong)NSString *shop;
@property(nonatomic,strong)NSString *door;
@property(nonatomic,strong)NSString *buildng;
@property(nonatomic,strong)NSString *flr;
@property(nonatomic,strong)NSString *tele;
@property(nonatomic,strong)NSString *pin;
@property(nonatomic,strong)NSString *main;
@property(nonatomic,strong)NSString *dist;
@property(nonatomic,strong)NSString *designan;
@property(nonatomic,strong)NSString *depart;
@property(nonatomic,strong)NSString *emai;
@property(nonatomic,strong)NSString *mobile;
@property(nonatomic,strong)NSString *strt;
@property(nonatomic,strong)NSString *area;


@property(nonatomic,strong) NSString *jwleryname;
@property(nonatomic,strong) NSString *officeid;


-(void)setAttributes:(NSDictionary *)attributes;




///////////////////////////////////////////////
showroom class



 headdetails=[[HeadOfficeDetails alloc]init];


self.txtshowroom.text=headdetails.jwleryname;
NSLog(@"%@ :",self.txtshowroom.text);


NSNumber *officeid=headdetails.officeid;
NSLog([NSString stringWithFormat:@"%@",officeid]);

[self.txtshortname setText:headdetails.jwleryname];
[self.txtshowroom setText:headdetails.shwrmname];
[self.txtstreet setText:headdetails.strt];
[self.txtarea setText:headdetails.area];
[self.txtmain setText:headdetails.main];
[self.txtbuilding setText:headdetails.buildng];
[self.txtname setText:self.cntctcls.contactname];
[self.txtdesign setText:self.cntctcls.desig];
[self.txtdepartmnt setText:self.cntctcls.depart];
[self.txtmbl setText:self.cntctcls.mobile];
[self.txtemail setText:self.cntctcls.email];
[self.txtfloor setText:headdetails.flr];
[self.txttel setText:headdetails.tele];
[self.txtpincode setText:headdetails.pin];
[self.txtshop setText:headdetails.shop];

Here are i put example code for show NSObject class data that save during data fetch from server and show on another class by using its instance.

Following is create a NSObject class for storing FBLogin userDetauls:

#import <Foundation/Foundation.h>

@interface FBUser : NSObject

@property (nonatomic,strong) NSString *FBUserId;
@property (nonatomic,strong) NSString *FBUserName;
@property (nonatomic,strong) NSString *FBUserFirstName;
@property (nonatomic,strong) NSString *FBUserLasttName;
@property (nonatomic,strong) NSString *FBUserEmail;
@property (nonatomic,strong) NSString *FBUserBio;
@property (nonatomic,strong) NSString *FBUserLocation;

+(instancetype)SharedFBUser;
-(void)ClearFBuser;
+(NSString*)CheckEmtryElseAlert:(NSString*)strValue;

@end

Following is an Implementation class here we create SharedFBUser that allcate FBUser object once if that found nil so when no need to do code alloc init during show time

#import "FBUser.h"


static FBUser* fbObject;

@implementation FBUser

+(instancetype)SharedFBUser
{
    if(fbObject == nil)
    {
        fbObject =[[FBUser alloc]init];
    }

    return fbObject;
}
-(void)ClearFBuser
{

    fbObject = nil;
    self.FBUserId = nil;
    self.FBUserName = nil;
    self.FBUserFirstName =nil;
    self.FBUserLasttName = nil;
    self.FBUserEmail = nil;
    self.FBUserBio = nil;
    self.FBUserLocation = nil;

}

Now following code for how to use FBUser class for save data .H class:

#import <UIKit/UIKit.h>
#import "FBUser.h"
@interface LoginViewController : UIViewController
@property(nonatomic,strong)FBUser *fbObject;
@end

And you can save data in FBCallback:

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error)
         {
             NSLog(@"resultis:%@",result);
             [FBUser SharedFBUser].FBUserId =[result valueForKey:@"id"];
             [FBUser SharedFBUser].FBUserEmail =[result valueForKey:@"email"];
              [FBUser SharedFBUser].FBUserName =[result valueForKey:@"name"];
             [FBUser SharedFBUser].FBUserFirstName =[result valueForKey:@"first_name"];
             [FBUser SharedFBUser].FBUserLasttName =[result valueForKey:@"last_name"];
         }
         else
         {
             NSLog(@"Error %@",error);
         }
     }];

And following code for show the data that have stored in FBuser class

 #import "FBUser.h"
    self.lblEmail.text = [FBUser SharedFBUser].FBUserEmail;
    self.lblusename.text = [FBUser SharedFBUser].FBUserName;
    self.lblfirstname.text = [FBUser SharedFBUser].FBUserFirstName;
    self.lbllastname.text = [FBUser SharedFBUser].FBUserLasttName;

Hope that help this fully description about use and create. this is my working code.

Not sure what are you doing, may you need something like parse dictionary to object first.

headdetails=[[HeadOfficeDetails alloc]init];

New Object created so all properties will nil.

you need assign value for it like

headdetails=[[HeadOfficeDetails alloc]initWithDictionary:attributes];

You can parse it your self or looking some 3rd library like: JSonModel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM