我正在使用这个方法来初始化一个 nsmutablearray
- (void)getAllContacts
{
Contact *contact = [[Contact alloc] init];
self.allContacts = [[NSMutableArray alloc] init];
int i=0;
for (i=0; i<5; i++)
{
contact.nome = [[NSString alloc] initWithFormat:@"Bruno %d", i];
[self.allContacts insertObject:contact atIndex:i];
}
}
非常简单!但紧接着,我做了一个用于打印它的元素,如:
for (int i=0; i<[self.allContacts count]; i++)
{
Contact *c = [self.allContacts objectAtIndex:i];
NSLog(@"i=%d\nNome:%@", i, c.nome);
}
它会显示最后一个元素“Bruno 4”的 5 次。它不是从 0 开始并递增。我该怎么做才能从0开始?
最佳答案
试试这个:
- (void)getAllContacts
{
Contact *contact = nil;
self.allContacts = [NSMutableArray array];
int i=0;
for (i=0; i<5; i++)
{
contact = [Contact new];
contact.nome = [NSString stringWithFormat:@"Bruno %d", i];
[self.allContacts addObject:contact];
[contact release]
}
}
请查看:Memoy Management
关于ios - NSMutablearray 从最后一个元素而不是第一个开始打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10190664/