iOS:Object在alloc和init之后返回nil描述

时间:2016-05-27 07:27:31

标签: ios objective-c

对象在alloc和init..can之后返回nil描述任何人都告诉我必须显示来自服务器的数据...当我点击编辑按钮时,它会转到该类的shoow room class ..在textfields中如果要显示信息,可以帮助我。

@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];

2 个答案:

答案 0 :(得分:0)

以下是我为show NSObject类数据提供的示例代码,该数据在从服务器获取数据期间保存,并使用其实例显示在另一个类上。

以下是创建一个用于存储FBLogin userDetauls的NSObject类:

#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

以下是一个实施课程,我们创建SharedFBUser,如果找到nil,则会FBUser一次对象,因此在展示时间内无需执行代码alloc init < / p>

#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;

}

现在关注如何使用FBUser类来保存数据.H类:

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

您可以在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);
         }
     }];

以下代码显示存储在FBuser

中的数据
 #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;

希望有助于此充分描述使用和创建。这是我的工作代码。

答案 1 :(得分:0)

不确定你在做什么,可能需要像解析字典这样的东西来反对。

headdetails=[[HeadOfficeDetails alloc]init];

创建了新对象,因此所有属性都将为零。

你需要为它分配值,如

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

您可以自己解析或查找第三个库,例如:JSonModel