2013-08-25 100 views
-2

我创建了一个类,将保存用户和设备的信息。该类称为Account。 这里是类代码(ARC项目):着用来初始化自定义类

#import <Foundation/Foundation.h> 
@interface Account : NSObject 
@property(strong, nonatomic) NSString *deviceID; 
@property(strong, nonatomic) NSString *accountID; 
@property(strong, nonatomic) NSString *oAuthCode; 
@property(strong, nonatomic) NSString *type; 
@property(strong, nonatomic) NSString *deviceToken; 
@property(strong, nonatomic) NSString *os; 
@property(strong, nonatomic) NSString *deviceName; 
@end 



#import "Account.h" 

@implementation Account 

@synthesize deviceID = _deviceID; 
@synthesize deviceName = _deviceName; 
@synthesize deviceToken = _deviceToken; 
@synthesize type = _type; 
@synthesize oAuthCode = _oAuthCode; 
@synthesize accountID = _accountID; 
@synthesize os = _os; 



-(NSString*)deviceID{ 
    return [ThinkerBell_OpenUDID value]; 
} 

-(NSString*)deviceToken{ 
    return [[NSUserDefaults standardUserDefaults]valueForKey:K_DEVICE_TOKEN]; 
} 

-(NSString*)os{ 
    return [[UIDevice currentDevice]systemVersion]; 
} 

-(NSString*)deviceName{ 
    return [[UIDevice currentDevice]model]; 
} 
@end 

我在这里创建一个实例,并试图访问他的属性:

_ac = [[Account alloc]init]; 
_ac.accountID = @"[email protected]"; 
_ac.type = @"[email protected]"; 
_ac.oAuthCode = @"[email protected]"; 
_ac.deviceName = @"[email protected]"; 
_ac.deviceToken = @"[email protected]"; 
_ac.deviceID = @"[email protected]"; 
_ac.os = @"[email protected]"; 

比我通过这个对象作为参数的方法:

[self appendAccount:_ac]; 

当我试图读取Account例如他的字段。

-(void)appendAccount:(Account*)account{ 
    NSLog(@"%@",account); 
} 

有什么想法吗?

+0

请澄清你认为此代码的作用... – Eiko

回答

1

你还没有在你的类中实现“描述”方法,所以NSLog没有任何东西可以打印出来。

这里是你如何能做到这一点:

@implementation Account 

- (NSString *)description 
{ 
    return [NSString stringWithFormat:@"<Account ID %@>", self.accountID]; 
} 

@end 

此外,删除的代码,通常并不需要他们这些行,并可能导致问题:

@synthesize deviceID = _deviceID; 
@synthesize deviceName = _deviceName; 
@synthesize deviceToken = _deviceToken; 
@synthesize type = _type; 
@synthesize oAuthCode = _oAuthCode; 
@synthesize accountID = _accountID; 
@synthesize os = _os; 

(你可能得到的是从旧示例代码,我建议找到一些更新的和最新的)

1

它,因为你没有制定者反映干将。值写入_deviceid,但从thinkerbell读取...

+0

所以我必须为每一个吸气剂写入setters? –

+0

@orazran不,你应该很少需要写一个getter或setter。 –

+0

@AbhiBeckert,如果主题创始人写他自己的getter返回的值不反映合成的getter,则调用setter写入_var绝对没用。 –