Objective c 向数组添加对象,数组返回null
我有两个数组,它们从数据库接收信息。其中一个名为json,包含具有用户id的帖子,另一个名为userJson,包含具有用户id的所有用户。如果来自json的用户id等于来自userJson的用户id,我想用该用户用户名填充第三个数组。我正在使用以下代码:Objective c 向数组添加对象,数组返回null,objective-c,arrays,nsmutablearray,Objective C,Arrays,Nsmutablearray,我有两个数组,它们从数据库接收信息。其中一个名为json,包含具有用户id的帖子,另一个名为userJson,包含具有用户id的所有用户。如果来自json的用户id等于来自userJson的用户id,我想用该用户用户名填充第三个数组。我正在使用以下代码: NSDictionary *info = [json objectAtIndex:indexPath.row]; NSDictionary *userInfo = [userArray objectAtIndex:0]; for (int i=
NSDictionary *info = [json objectAtIndex:indexPath.row];
NSDictionary *userInfo = [userArray objectAtIndex:0];
for (int i=0; i<[json count]; i++) {
if ([[userInfo objectForKey:@"user_id"] isEqualToString:[info objectForKey:@"user_id"]]) {
[usernameArray addObject:[userInfo objectForKey:@"username"]];
}
}
NSLog(@"%@", usernameArray);
NSDictionary*info=[json-objectAtIndex:indexath.row];
NSDictionary*userInfo=[userArray objectAtIndex:0];
对于(int i=0;i您需要首先添加以下内容:
usernameArray = [[NSMutableArray alloc]init];
然后将循环更改为以下内容:
NSDictionary *info = [json objectAtIndex:indexPath.row];
NSDictionary *userInfo;
for (int i=0; i<[userArray count]; i++) {
userInfo = [userArray objectAtIndex:i];
if ([[userInfo objectForKey:@"user_id"] isEqualToString:[info objectForKey:@"user_id"]]) {
[usernameArray addObject:[userInfo objectForKey:@"username"]];
}
}
NSDictionary*info=[json-objectAtIndex:indexath.row];
NSDictionary*用户信息;
对于(int i=0;i您需要首先添加以下内容:
usernameArray = [[NSMutableArray alloc]init];
然后将循环更改为以下内容:
NSDictionary *info = [json objectAtIndex:indexPath.row];
NSDictionary *userInfo;
for (int i=0; i<[userArray count]; i++) {
userInfo = [userArray objectAtIndex:i];
if ([[userInfo objectForKey:@"user_id"] isEqualToString:[info objectForKey:@"user_id"]]) {
[usernameArray addObject:[userInfo objectForKey:@"username"]];
}
}
NSDictionary*info=[json-objectAtIndex:indexath.row];
NSDictionary*用户信息;
对于(int i=0;iMy的第一个问题是,您有这行代码吗?usernameArray=[[NSMutableArray alloc]init];没有。@Ryan Tobin我的第一个问题是,您有这行代码吗?usernameArray=[[NSMutableArray alloc]init];没有。@Ryan Tobin