I have a fixed data which will be used in a UITableView later and the user will not update/add on this data.
This data looks like a table with 4 column:
Name (String) | Tel (INT) | Logo (URL) | PDF File (URL)
The UITableView will be fill with this data and if the user select a row he will navigate to another pages which show the PDF file.
The question is should I use the core data or Array, if the answer is Array how can I have column in the Array?
Thanx,
You can easily use an array with a NSDictionary inside. Both structures might be mutable and you can use the dictionary to simulate what you call columns.
Example:
[myMutDict setObject:name forKey:#"name"];
[myMutDict setObject:[NSNumber numberWithInt:n] forKey:#"number"];
[myMutArray addObject:myMutDict];
I'd not go for coredata
In your didSelectRowAtIndexPath you'll select your data like this:
NSDictionary *dict = [myMutArray objectAtIndex:indexPath.row];
NSString *name = [dict objectForKey:#"name"];
int tel = [[dict objectForKey:#"number"] intValue];
For this it is better to use NSMutable Array.
For this data to save in NSArray Initially you need to save every row in a NSMutableDictionary like
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]initWithCapacity:4];
[dictionary setObject:value forKey:#"Name"];
[dictionary setObject:value forKey:#"Tel"];
[dictionary setObject:value forKey:#"Logo"];
[dictionary setObject:value forKey:#"PDF File"];
so add this dictionary to a NSMutableArray like this
[array addObject:dictionary];
You can easily retrive the data from array
Related
I am using a for loop to loop though and insert data into a NSMutableDictionary and then inserting that NSMutableDictionary into an NSMutableArray, the code is fairly simple and shown below:
for (NSDictionary *selectedOption in selectedOptions) {
NSString *name = selectedOption[#"name"];
NSString *value = selectedOption[#"value"];
[variantRow setObject:name forKey:#"name"];
[variantRow setObject:value forKey:#"value"];
[variantInfo addObject:variantRow];
}
The problem I am trying to solve is that *name and *value always gets the last value of the loop even for previously inserted dicts into the variantInfo NSMutableArray, I am assuming my problem is because I am inserting pointers etc, but I don't understand how else I can do it? I need to insert the values and have future inserts not affect previous ones.
I hope the description makes sense as its not to easy to explain.
You are modifying the same NSDictionary object reference(variantRow) in every iteration and appending it to the array variantInfo. You need to create a new NSDictionary object:
for (NSDictionary *selectedOption in selectedOptions) {
NSString *name = selectedOption[#"name"];
NSString *value = selectedOption[#"value"];
if(name && value) {
NSDictionary* newVariantRow = #{"name": name, #"value":value};
[variantInfo addObject: newVariantRow];
}
}
I have a JSON array being pulled into XCode with a key and value. I can get the keys. I can get the values. But is there an easy way to combine them into a single array?
The following code works, but I end up with two separate arrays (channels and channelKeys).
This seems like an inelegant way to create a single array which contains both the key and its value.
-(void) convertArray : (NSMutableArray *)data{
// Set data
NSMutableDictionary *dic = [data objectAtIndex:0];
for (NSString *key in [dic allKeys]) {
[channels addObject:[dic objectForKey:key]];
}
// Set Key Array
NSMutableDictionary *dic3 = [data objectAtIndex:0];
NSArray *keys = [dic3 allKeys];
[channelKeys addObjectsFromArray: keys];
}
If you are trying to create an array of the form [key1, value1, key2, value2, key3, value3...] then try something like the following (recall that keys are not restricted to NSStrings)
for (id key in [dic allKeys]) {
[resultArray addObject:key];
[resultArray addObject:[dic objectForKey:key]];
}
I'm trying to store data in NSMutableDictionary. continue change NSString value I want to store this value in NSMutableDictionary, but when second time store value in NSMutableDictionary then clear old value so how to store one by one value in NSMutableDictionary.
NSString *tempitemname = itemselectedlbl.text;
NSString *temprate = ratelbl.text;
NSString *tempquant = quantitylbl.text;
NSString *temptotal = totallbl.text;
NSString *temptotalbill = totalbilllbl.text;
dict = [[NSMutableDictionary alloc]init];
[dict setValue:tempitemname forKey:#"itemname"];
[dict setValue:temprate forKey:#"rate"];
[dict setValue:tempquant forKey:#"quantity"];
[dict setValue:temptotal forKey:#"total"];
[dict setValue:temptotalbill forKey:#"totalbill"];
Continue change all labels value I want store these values in NSMutableDictionary.
NSDictionary/NSMutableDictionary doesn't allow duplicate key to store inside them. So when you set the values second time then it overwrites the data if there is already a key available inside the dictionary. So you can't store again and again and still expect it to hold all the data.
I have a dictionary with key-value pair populated from JSON returned data.What I wish to do is use the dictionary to populate UITableView.
I have this structure for table:
[Product Name]
By [Manufacturer Name]
What this means is that key is Product Name and Value is Manufacturer Name. I need to get the name of the key and the name of the value. How can this be done? and is it possible without for-loop?
I'd use the enumerateKeysAndObjectsUsingBlock: method. The following code builds a list of the strings you require.
NSMutableArray *names = [NSMutableArray array];
[dictionary enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSString *object, BOOL *stop) {
[names addObject[NSString stringWithFormat:#"%# By %#",key, object]];
}];
You can use the keyEnumerator of NSDictionary and for each key look up the value. This could look something like this:
for (NSString *p in dict)
{
NSString *m = [dict objectForKey:p];
// do something with (p,m)
}
You should not be concerned with avoiding for-loops. After all, something like a for loop will always happen somewhere underneath.
If your keys are dynamic from json then you can use
NSArray *keys = [dictionary allkeys];
Then in the table View Cell for row at index path method you can populate the table view with the corresponding keys and their values.
NSArray * keys = [results allKeys];
for (int i = 0;i<[keys count];c++){
NSString* productName = [key objectAtIndex:i];
NSString* manufacturerName = [results objectForKey:productName];
}
Hope this helps...
I have assumed the name as strings, you can change the type according to your situation..
I’m looking for the best way to store data like this...
Value 1
-item 1
-item 2
-item 3
...
-item 9
Value 2
-item 1
...
-item 9
Value 3
etc...
Then I want to pick a subset of the items for a given “value”
Is NSMutableDictionary the way to go for this? I’m getting a little confused in setting this up.
I’ve been trying this... but its not quite right apparently. Thanks for the help.
NSMutableDictionary *dictionary;
[dictionary setObject:#"Entry1" forKey:#"1"];
[dictionary setObject:#"1-Entry2" forKey:#"1"];
[dictionary setObject:#"Entry2" forKey:#"2"];
[dictionary setObject:#"Entry3" forKey:#"3"];
NSLog(#"1: %#", [dictionary objectForKey:#"1"]);
NSLog(#"/n 2: %#", [dictionary objectForKey:#"1"]);
If you want multiple objects under a single key, you need to use arrays inside the dictionary:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSArray arrayWithObjects:#"item1", #"item2", nil] forKey:#"Key1"];
and so on.