簡體   English   中英

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

[英]iOS: Object returns nil description after alloc and init

對象在alloc和init之后返回nil描述。任何人都可以告訴我我必須顯示來自服務器的數據...當我單擊“ edit”按鈕時,它將進入該類中的小房間類。在文本字段中必須顯示該信息。有誰能幫助我

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

這是我的示例代碼,用於顯示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是allcate FBUser如果發現零,所以當不需要做代碼對象一次alloc init期間放映時間

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

希望對本文的使用和創建有所幫助。 這是我的工作代碼。

不確定您在做什么,可能需要像parse dictionary這樣的對象才能首先成為對象。

headdetails=[[HeadOfficeDetails alloc]init];

創建了新對象,因此所有屬性都將為零。

您需要為其分配值,例如

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

您可以自己解析它,也可以查看一些第3個庫,例如: JSonModel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM