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.
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 already initialized two arrays named - questionArray and correctAnswerArray.
I want to add these two arrays in a NSDictionary/ NSMutableDictionary (the one more appropriate) such that the questionArray will be the value (each index of it) and the correctAnswerArray will be the key.
Whether you chose NSMutableDictionary or not depends on if you want to change the data later on. Either way you can just change the following if you want a standard dictionary.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:questionArray forKeys:correctAnswerArray];
** EDIT **
To retrive a value just do:
NSString *value = [dict valueForKey:#"KEY"];
or
NSString *value = dict[#"KEY"];
Why is a NSString #"#" not Key Value Compliant?
Are there other strings that aren't compliant as well?
You can try that it is failing with this code for example:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"Some Object" forKey:#"#"];
NSString *theObject = [dict valueForKey:#"#"];
Setting it as a key is ok but not querying for that key..
Sure you can work around this error by appending some other string you later on remove another time like doing the following when you want to have the key #:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"Some Object" forKey:#"keyConst#"];
NSString *theObject = [dict valueForKey:#"keyConst#"];
The counterpart to setObject:forKey: is
objectForKey: (and not valueForKey:) to retrieve an item from a dictionary:
NSString *theObject = [dict objectForKey:#"#"];
Alternatively, use the "new" dictionary subscripting syntax:
dict[#"#"] = #"Some Object";
NSString *theObject = dict[#"#"];
valueForKey: uses Key-Value coding methods if the key starts with #.
From the documentation of -[NSDictionary valueForKey:]:
If key does not start with “#”, invokes objectForKey:. If key does
start with “#”, strips the “#” and invokes [super valueForKey:] with
the rest of the key.
For example,
NSString *x = [dict valueForKey:#"#description"];
does the same as
NSString *x = [dict description];
So in almost all cases, you should use objectForKey:, unless you explicitly
want to do some Key-Value coding magic.
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
I tried to do this to store an empty dictionary in NSUserDefaults.
NSMutableDictionary* fruits = [NSMutableDictionary alloc];
[defaults setObject:fruits forKey:#"fruits"];
and then later this to retrieve it.
NSMutableDictionary* fruits = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:#"fruits"]];
However, retrieving the dictionary crashes my application. Why? How do I store a dictionary in NSUserDefaults?
You get a immutable dictionary back. You do not need to "capsulate" it in another dictionary. If you want to make it mutable write:
NSMutableDictionary* animals = [[defaults objectForKey:#"animals"] mutableCopy];
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
Values returned from NSUserDefaults are immutable, even if you set a
mutable object as the value. For example, if you set a mutable string
as the value for "MyStringDefault", the string you later retrieve
using stringForKey: will be immutable.
Note: The user defaults system, which you programmatically access through the NSUserDefaults class, uses property lists to store objects representing user preferences. This limitation would seem to exclude many kinds of objects, such as NSColor and NSFont objects, from the user default system. But if objects conform to the NSCoding protocol they can be archived to NSData objects, which are property list–compatible objects. For information on how to do this, see ““Storing NSColor in User Defaults”“; although this article focuses on NSColor objects, the procedure can be applied to any object that can be archived.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html
You can use:
Save:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mutableArray];
[[NSUserDefaults standardUserDefaults] setObject:stack forKey:#"Your Key"];
Retrieve:
NSMutableArray *mutableArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
You need to init your dictionary and set is as object later. This way works, it's the same as your example but just with properly initialization.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"someValue", #"someKey", nil];
[defaults setObject:dict forKey:#"slovnik"];
[dict release];
NSLog(#"READ: %#", [defaults objectForKey:#"slovnik"]);
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:#"slovnik"]];
NSLog(#"READ2: %#", newDict);
Now I get to log console and app do not crash:
2012-04-12 08:47:55.030 Test[12179:f803] READ: {
someKey = someValue;
}
2012-04-12 08:47:55.031 Test[12179:f803] READ2: {
someKey = someValue;
}
NSMutableDictionary* fruits = [NSMutableDictionary alloc];
should be
NSMutableDictionary* fruits = [[NSMutableDictionary alloc] init];
You need to always initialize objects after allocating them.