我不能打印我的数组,我不知道我的问题出在哪里
在班级里,我有
@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);
}发布于 2013-01-04 20:05:23
在您尝试使用bank.accounts之前,应该对其进行分配和初始化。
self.accounts = [[NSMutableDictionary alloc] init];附录:
要影响NSLog为对象打印的内容,可以覆盖description
所以你的Account应该是这样的:
-(NSString*)description{
return [NSString stringWithFormat:@"Account num=%i plus=%i",self.num,self.plus];
}NSMutableDictionary已经对该格式进行了很好的描述:
2013-01-04 12:03:11.405 AppName[19683:c07] {
key1 = value1;
key2 = value2;
}因此您可以将循环简化为:NSLog(@"%@", bank.accounts);
https://stackoverflow.com/questions/14156804
复制相似问题