【发布时间】:2012-12-18 20:55:08
【问题描述】:
我无法打印我的数组,我不明白我的问题是什么
在班里我有
@property (nonatomic,strong) NSMutableDictionary *accounts;
在银行开户的VOID功能:
-(void)createAccount{
int x=random()%10;
NSString *keys=[NSString stringWithFormat:@"%i",x];
Account *new_account=[[Account alloc]initWithAccountNum:x];
[self.accounts setObject:new_account forKey:keys];
}
在我的班级帐户中
@property int num;
@property int plus;
和方法:
-(id)initWithAccountNum:(int)_num{
self=[self init];
if(self!=nil){
self.num=_num;
self.plus=0;
}
return self;
}
-(NSString*)printer{
return [NSString stringWithFormat:@"Account num=%i plus=%i",self.num,self.plus];
}
我尝试将数据打印到 Nslog i 主文件:
Bank *bank=[[Bank alloc]init];
[bank createAccount];
for (Account *acc in bank.accounts) {
Account *printed_account=[bank.accounts objectForKey:acc];
NSLog(@"%@",printed_account.printer);
}
【问题讨论】:
-
你是在Bank的init中初始化self.accounts还是nil?
-
仅将 for 内容替换为
NSLog(@"%@",acc)。将 -printer 方法签名替换为-(NSString*) description。
标签: objective-c dictionary for-in-loop