Iphone 如何创建数组字符串
我有一个包含学生信息的NSMutableArray,现在我只想提取学生姓名和他们的平均分数,所以我做了什么Iphone 如何创建数组字符串,iphone,arrays,string,nsmutablearray,Iphone,Arrays,String,Nsmutablearray,我有一个包含学生信息的NSMutableArray,现在我只想提取学生姓名和他们的平均分数,所以我做了什么 NSMutableArray *stuCollection = [[NSMutableArray alloc] initWithCapacity:[students count]]; for (int i = 0; i < [students count]; i++) { if([students objectAtIndex:i] != NULL) {
NSMutableArray *stuCollection = [[NSMutableArray alloc] initWithCapacity:[students count]];
for (int i = 0; i < [students count]; i++)
{
if([students objectAtIndex:i] != NULL)
{
NSDictionary *dic = [students objectAtIndex:i];
NSString *temp = [dic objectForKey:@"name"];
[stuCollection addObject:name];
}
}
for(int j=0; j< [stuCollection count]; j++)
{
NSLOG(@"Name %@",[stuCollection objectAtIndex:j]);
}
NSMutableArray*stuCollection=[[NSMutableArray alloc]initWithCapacity:[students count]];
对于(int i=0;i<[学生计数];i++)
{
如果([students objectAtIndex:i]!=NULL)
{
NSDictionary*dic=[学生对象索引:i];
NSString*temp=[dic objectForKey:@“name”];
[stuCollection addObject:名称];
}
}
对于(int j=0;j<[Stu集合计数];j++)
{
NSLOG(@“Name%@,[stuCollection objectAtIndex:j]);
}
我可以第一次运行它,但当我进行自动扫描时,我可以执行第一、第二、第三个循环,但随后应用程序终止显示如下
2009-12-02 14:57:37.908 AppBuzz[13073:207]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*-[NSCFArray insertObject:atIndex::]:尝试插入nil”
2009-12-0214:57:37.916 AppBuzz[13073:207]堆栈:(
820145437,
837578260,
819694387,
819694291,
814683071,
814716415,
814716245,
17529,
24097,
814480795,
819893443,
819891231,
858682228,
861592624,
861585968,
8997,
8860
)
在抛出“NSException”实例后调用terminate
程序收到信号:“SIGABRT”
如何改进这一点
谢谢因为学生们看起来像是一个可可系列,所以你可以使用以下内容:
NSArray*studentNames=[students valueForKey:@“name”]代码>
有关更多信息,请参阅KVC。您了解断言吗
assert(students);
assert([students count]);
NSMutableArray * stuCollection = [[NSMutableArray alloc] initWithCapacity:[students count]];
assert(stuCollection);
for (int i = 0; i < [students count]; i++) {
if ([students objectAtIndex:i] != NULL) {
NSDictionary * dic = [students objectAtIndex:i];
assert(dic);
NSString * temp = [dic objectForKey:@"name"];
assert(temp);
assert(name);
[stuCollection addObject:name];
}
}
...
assert(学生);
断言([学生计数]);
NSMUTABLEARRY*stuCollection=[[NSMUTABLEARRY alloc]initWithCapacity:[学生计数]];
断言(集合);
对于(int i=0;i<[学生计数];i++){
如果([students objectAtIndex:i]!=NULL){
NSDictionary*dic=[学生对象索引:i];
断言(dic);
NSString*temp=[dic objectForKey:@“name”];
断言(临时);
断言(名称);
[stuCollection addObject:名称];
}
}
...