I'm farely new to objective-C and got a question regarding dictionaries, arrays and tables.
我对Objective-C很新,并且对词典,数组和表格提出了疑问。
The situation: I got a JSON dictionary containing data. I can access this data via keys, i.e.:
情况:我得到了一个包含数据的JSON字典。我可以通过密钥访问这些数据,即:
NSString = *myString [jsonDict valueForKey:@"somestring"];
Some of those dictionary items though contain arrays instead of a simple string and this is where my trouble starts.
其中一些字典项虽然包含数组而不是简单的字符串,但这是我的麻烦开始的地方。
[jsonDict valueForKey:@"someArrays"];
...contains a dynamical amount of Arrays (with one value each). I want to print these arrays out as strings in a list with bulletpoints (in HTML I would use an ul). I was told that the best way to do so would be by printing each array into a tableView/ tableView cell?
...包含动态数量的数组(每个都有一个值)。我想将这些数组作为带有弹出点的列表中的字符串打印出来(在HTML中我将使用ul)。有人告诉我,最好的方法是将每个数组打印到tableView / tableView单元格中?
Personally I don't care what method is the best - I only need some method that works. How am I supposed to print these arrays as single strings?
我个人并不关心什么方法是最好的 - 我只需要一些有效的方法。我怎么打算将这些数组打印为单个字符串?
My current attempt looks like this:
我目前的尝试看起来像这样:
NSArray *arrayData = [jsonDict valueForKey:@"arrays"];
for (int i = 0; i < [arrayData count]; i++) {
NSString *arrayStrings = [NSString stringWithFormat:@"%@ %@", @"\u2022", arrayData[i]];
}
But this will only print out the very last Array in valueForKey:@"arrays" as a string. The rest ist missing (I guess that's because I try to assign all of the arrays to only one string ("arrayStrings"). As a beginner I really can't imagine a right way to do this.
但这只会打印出valueForKey中的最后一个数组:@“arrays”作为字符串。其余的都缺失了(我想这是因为我尝试将所有数组分配给一个字符串(“arrayStrings”)。作为一个初学者,我真的无法想象一个正确的方法来做到这一点。
What do I have to do in order to complete this task efficiently?
为了有效地完成这项任务,我该怎么办?
2 个解决方案
#1
0
Yes guessed right, you are resetting the string in each iteration. Try this:
是的,猜对了,你在每次迭代中重置字符串。尝试这个:
NSString *arrayStrings = [arrayData componentsJoinedByString: @"\r\u2022"];
NSLog(@"%@", arrayStrings);
Or this:
NSMutableString *arrayStrings = [NSMutableString new];
for (int i = 0; i < [arrayData count]; i++) {
[arrayStrings appendString:[NSString stringWithFormat:@"%@ %@", @"\r\u2022", arrayData[i]]];
}
NSLog(@"%@", arrayStrings);
#2
0
You're indeed setting the same string each time, so logging (NSLog
) it would show only the last entry.
您确实每次都设置相同的字符串,因此记录(NSLog)它只显示最后一个条目。
You "should" try and log in your for loop to make sure you're really getting all the string you need. This would print all your strings.
你“应该”尝试登录你的for循环,以确保你真正得到你需要的所有字符串。这将打印所有字符串。
Now if you wanna store it, i suggest you use an array, like the following.
现在,如果你想存储它,我建议你使用一个数组,如下所示。
// Preparing my array of strings
NSMutableArray *allTheString = [[NSMutableArray alloc]init];
NSArray *arrayData = [jsonDict valueForKey:@"arrays"];
for (int i = 0; i < [arrayData count]; i++) {
// According to your code, this gets you your correct log
NSString *arrayStrings = [NSString stringWithFormat:@"%@ %@", @"\u2022", arrayData[i]];
[allTheStrings addObject:arrayStrings]
// Because you just added it above, it is last in the array.
// You could addObjectAtIndex:i instead, and then log the objectAtIndex:i,
// but it's really the same if you start with an empty array
NSLog(@"This is my string : %@", [allTheStrings lastObject])
}
To get your strings from the array, i'm sure you know of [allTheStrings objectAtIndex:#]
, #
being an index (0, 1, 3, .. etc)
要从数组中获取字符串,我确定你知道[allTheStrings objectAtIndex:#],#是索引(0,1,3,...等)
#1
0
Yes guessed right, you are resetting the string in each iteration. Try this:
是的,猜对了,你在每次迭代中重置字符串。尝试这个:
NSString *arrayStrings = [arrayData componentsJoinedByString: @"\r\u2022"];
NSLog(@"%@", arrayStrings);
Or this:
NSMutableString *arrayStrings = [NSMutableString new];
for (int i = 0; i < [arrayData count]; i++) {
[arrayStrings appendString:[NSString stringWithFormat:@"%@ %@", @"\r\u2022", arrayData[i]]];
}
NSLog(@"%@", arrayStrings);
#2
0
You're indeed setting the same string each time, so logging (NSLog
) it would show only the last entry.
您确实每次都设置相同的字符串,因此记录(NSLog)它只显示最后一个条目。
You "should" try and log in your for loop to make sure you're really getting all the string you need. This would print all your strings.
你“应该”尝试登录你的for循环,以确保你真正得到你需要的所有字符串。这将打印所有字符串。
Now if you wanna store it, i suggest you use an array, like the following.
现在,如果你想存储它,我建议你使用一个数组,如下所示。
// Preparing my array of strings
NSMutableArray *allTheString = [[NSMutableArray alloc]init];
NSArray *arrayData = [jsonDict valueForKey:@"arrays"];
for (int i = 0; i < [arrayData count]; i++) {
// According to your code, this gets you your correct log
NSString *arrayStrings = [NSString stringWithFormat:@"%@ %@", @"\u2022", arrayData[i]];
[allTheStrings addObject:arrayStrings]
// Because you just added it above, it is last in the array.
// You could addObjectAtIndex:i instead, and then log the objectAtIndex:i,
// but it's really the same if you start with an empty array
NSLog(@"This is my string : %@", [allTheStrings lastObject])
}
To get your strings from the array, i'm sure you know of [allTheStrings objectAtIndex:#]
, #
being an index (0, 1, 3, .. etc)
要从数组中获取字符串,我确定你知道[allTheStrings objectAtIndex:#],#是索引(0,1,3,...等)