2016-03-25 4 views
0

プロパティUserクラスとAuthという名前のSingletonクラスがあります。 Authクラスでは、リクエストをサーバーに送信しています。結果が返ってきたら、Userプロパティを設定します。 setName、setLastnameなどと同じです。しかし、私はUser値を設定することはできず、常にnullを返します。ここで 変数をプロパティークラスにシングルトンで設定するObjective C

はself.userがあるよう

#import "Auth.h" 
#import "User.h" 
#import "ServiceRequest.h" 

@implementation Auth 


@synthesize user; 

+ (Auth *) getInstance 
{ 
    static Auth* sharedInstance = nil; 
    static dispatch_once_t predicate; 
    dispatch_once(&predicate, ^{ 
     sharedInstance = [ [ self alloc ] init ]; 
    }); 
    return sharedInstance; 
} 


-(void) doLogin:(void (^) (BOOL, NSDictionary * err))comletion{ 

    [[[ServiceRequest alloc] init] doLogin:^(NSDictionary *data, NSString *err) { 

     if ([data objectForKey:@"errors"] != [NSNull null]) { 
      comletion (NO, [data objectForKey:@"error"]); 
     }else { 

     [self.user setName:@"USER_NAME"]; 

      NSLog(@"USER NAME IN AUTH:%@",self.user.name); 
      //RETURNS NULL:(

     } 

    }]; 

} 

答えて

1
//Checkpoint 1 
if (self.user == nil) { 
    self.user = [User new]; 
} 
//Checkpoint2 
[self.user setName:@"USER_NAME"]; 

チェックポイント2の前にチェックポイント1を追加しますUser.h

@interface User : NSObject 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *lastname; 
@end 

Auth.h

#import "User.h" 
@interface Auth : NSObject{ 
    User *user; 
} 

@property (nonatomic, retain) User *user; 
@property (atomic, retain) NSString *token; 

+(Auth *) getInstance; 
-(void) doLogin:(void (^) (BOOL, NSDictionary *))comletion; 
@end 

Auth.mです初期化されていないので、この目的のために新しいキーワードを使用してください。

+0

あなたは私の夜を救った!ありがとう! –

関連する問題