可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
i have a NSDictionary
which looks like:
{
"Item1" = 2.4;
"Item2" = 5.5;
"Item3" = 15.6;
}
To use this NSDictionary
Items in a Table View i have to transfer it to a NSArray
, am i right?
So i try:
NSDictionary *dict = [myDict objectForKey:@"items"];
for (id item in dict) {
[_myArray addObject:[dict objectForKey:item]];
}
But _myArray keeps empty? What am i doing wrong?
回答1:
Leaving aside the technical issues with the code you posted, you asked this:
To use this Dictionary Items in a Table View i have to transfer it to a NSArray, am i right?
The answer to which is: not necessarily. There's nothing intrinsic to the machinery of UITableView
, UITableViewDataSource
, or UITableViewDelegate
that means that your data has to be in an array. You will need to implement various methods to tell the system how many rows are in your table, and what data appears in each row. Many people find it much more natural and efficient to answer those questions with an ordered data structure like an array. But there's no requirement that you do so. If you can write the code to implement those methods with the dictionary you started with, feel free!
回答2:
NSArray * values = [dictionary allValues];
回答3:
NSArray *keys = [dictionary allKeys];
NSArray *values = [dictionary allValues];
回答4:
You can create an array of all the objects inside the dictionary and then use it as a datasource for the TableView.
NSArray *aValuesArray = [yourDict allValues];
回答5:
Code Snippet1:
NSMutableArray *array = [[NSMutableArray alloc] init];
NSArray * values = [dictionary allValues];
[array addObject:values];
Code Snippet2: If you want to add further
[array addObject:value1];
[array addObject:value2];
[array addObject:value3];
And so on
Also you can store key values of dictionary to array
NSArray *keys = [dictionary allKeys];
回答6:
There are a few things that could be happening here.
Is the dictionary you have listed the myDict
? If so, then you don't have an object with a key of @"items"
, and the dict
variable will be nil. You need to iterate through myDict
directly.
Another thing to check is if _myArray
is a valid instance of an NSMutableArray. If it's nil, the addObject:
method will silently fail.
And a final thing to check is that the objects inside your dictionary are properly encased in NSNumbers (or some other non-primitive type).
回答7:
You just need to initialize your NSMutableArray
NSMutableArray *myArray = [[NSMutableArray alloc] init];
回答8:
This code is actually used to add values to the dictionary
and through
the data to an Array
According to the Key
.
NSMutableArray *arr = [[NSMutableArray alloc]init];
NSDictionary *dicto = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Hello",@"StackOverFlow",@"Key1",@"StackExchange",@"Key2", nil];
NSLog(@"The dictonary is = %@", dicto);
arr = [dicto valueForKey:@"Key1"];
NSLog(@"The array is = %@", arr);
回答9:
+ (NSArray *)getArrayListFromDictionary:(NSDictionary *)dictMain paramName:(NSString *)paramName
{
if([dictMain isKindOfClass:[NSDictionary class]])
{
if ([dictMain objectForKey:paramName])
{
if ([[dictMain objectForKey:paramName] isKindOfClass:[NSArray class]])
{
NSArray *dataArray = [dictMain objectForKey:paramName];
return dataArray;
}
}
}
return [[NSArray alloc] init];
}
Hope this helps!
回答10:
To get all objects in a dictionary, you can also use enumerateKeysAndObjectsUsingBlock:
like so:
NSMutableArray *yourArray = [NSMutableArray arrayWithCapacity:6];
[yourDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[yourArray addObject:obj];
}];
回答11:
In Swift 4:
let dict = ["Item1":2.4, "Item2": 5.4, "Item3" : 6.5]
let array = Array(dict.values)