NSString containing just # ist not Key Value Compliant - ios

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.

Related

NSMutableArray is affecting the another NSMutableArray?

I have two NSMutableArrays, both getting the same data. My problem is that when I assign the same data to both the mutable arrays from different dictionaries and perform operations on one NSMutableArray, it is affecting both arrays.
When I perform operations like replaceObjectAtIndex:WithObject:, the first time, the array is not affected but when the second replace is called both arrays have the replaced value. I think it is a reference issue.
Does anyone have a solution to this?
Name of the NSMutableArrays is helper.urlsRecording and helper.holdingArr.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
[dict setValue:outputFileURL forKey:#"URL"];
[dict setValue:#"1" forKey:#"index"];
[dict2 setValue:outputFileURL forKey:#"URL"];
[dict2 setValue:#"1" forKey:#"index"];
[helper.urlsRecording addObject:dict];
[helper.holdingArr addObject:dict2];
[helper.urlsRecording replaceObjectAtIndex:button.tag withObject:urlAr];//When this called second time, both the arrays is effected(helper.urlsRecording as well as helper.holdingArr).
How can I prevent the copying of the reference to another array?
Button Click:
if([button isSelected] == NO){
NSLog(#"Url Recording : %#",helper.urlsRecording);
[[helper.urlsRecording objectAtIndex:button.tag] removeObjectForKey:#"URL"];
button.selected = YES;
NSLog(#"Url Recording : %#",helper.urlsRecording);
}
else{
[helper.urlsRecording replaceObjectAtIndex:button.tag withObject:[helper.holdingArr objectAtIndex:button.tag]];
button.selected = NO;
NSLog(#"Url Recording : %#",helper.urlsRecording);
}
Note: NSMutableArray is defined globally in a class to access.
This is because your instance values are same for both dictionary.
So First create one mutableDictionary like below
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:outputFileURL forKey:#"URL"];
[dict setValue:#"1" forKey:#"index"];
And create second dictionary through mutableCopy, so instance will be different for both.
NSMutableDictionary *dict2 = [dict mutableCopy];
After that you can add them in to NSMutableArray and update accordingly.
Take a copy/mutableCopy dictionary & then add object to the MutableArray
[helper.urlsRecording addObject:[dict copy]];
[helper.holdingArr addObject:[dict2 copy]];

Combining dictionaries: No class method for selector "addEntriesFromDictionary"

I am struggling with a dictionary in which I want to create a new dictionary from a series of keys (P, SP, and RP).
I have attempted to create a new NSMutableDictionary that combines individual Dictionaries that have have all the values for the P, SP, and RP keys respectively, but have gotten the error "No class method for selector "addEntriesFromDictionary" "
Here is my code:
NSMutableDictionary *newDict = [NSMutableDictionary addEntriesFromDictionary:self.allSPPositions];
and
#interface className ()
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
#end
#implementation className
- (void)viewDidLoad {
Any help or insight would be appreciated! Thanks!
The compiler is telling you exactly what's wrong. You're trying to add items from a dictionary to the NSMutableDictionary class. You need to send the ad items from dictionary message to an instance of NSMutableDictionary.
This line:
NSMutableDictionary *newDict =
[NSMutableDictionary addEntriesFromDictionary:self.allSPPositions];
Should read
NSMutableDictionary *newDict =
[someDictionary addEntriesFromDictionary: self.allSPPositions];
(Where someDictionary is the dictionary to which you want to add items.)
or even
NSUInteger count = [self.allSPPositions count];
NSMutableDictionary *newDict =
[NSMutableDictionary dictonaryWithCapacity: count];
[newDict addEntriesFromDictionary: self.allSPPositions];
(Since your code appears to be trying to create a new, mutable dictionary and add the contents of self.allSPPositions.)
If your goal is to get a mutable copy of self.allSPPositions, there is a cleaner way to do that:
NSMutableDictionary *mutablePositions = [self.allSPPositions mutableCopy];
Uhh, here's an example that will compile and use the method in question, you should probably NOT call that method in your Interface since it's part of Apple's framework and is already predefined, unless you have a special reason for doing so:
NSMutableDictionary *dict =[[NSMutableDictionary alloc] init];
[dict setValue:#"Seattle" forKey:#"name"];
[dict setObject:[NSNumber numberWithInt:3] forKey:#"age"];
[dict setObject:[NSDate date] forKey:#"date"];
dict[#"city"]=#"Seattle";
[dict addEntriesFromDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys: #"Washington",#"location", nil]];
This is merely meant to get you started, but it shows you how to do this so that it works for you in your circumstances

I want to store NSString data in NSMutableDictionary

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.

Initialize NSMutableDictionary

I am developing an iOS application in which I want to use an NSMutableDictionary. Basically what I am doing is converting java code to objectiveC.
So in java I have something like this:
Map<String, ClassA> dict1 = new HashMap<>();
Map<Integer,Character> dict2 = new HashMap<>();
Map<Integer, Map<String,String>> dict3 = new HashMap<>();
Can someone please guide me as what would be the Obj-C equivalent code for the above three lines using NSMutableDictionary and also how can I set and get the pairs in/from the dictionaries.
The Objective-C collection classes are not strongly typed so all three dictionaries would be created using:
NSMutableDictionary *dictX = [NSMutableDictionary new];
In order to populate the dictionary use [NSMutableDictionary setObject:forKey:]:
[dict1 setObject:classAInstance
forKey:#"key1"];
[dict2 setObject:[NSString stringWithFormat:#"%c", character]
forKey:#(1)];
[dict3 setObject:#{ #"innerKey" : #"innerValue" }
forKey:#(2)];
etc.
Since Objective C does not have generic types all you have to type is this:
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init];
There's a couple ways to get and set values. The shorthand form is much like accessing arrays.
To set a value with shorthand:
dict1[#"key"] = #"value";
To get a value with shorthand:
NSString *value = dict1[#"key"];
More verbose syntax is like so:
[dict1 setObject:#"value" forKey:#"key"];
NSString *value = [dict1 valueForKey:#"key"];

NSDictionary valueForKey crash

How can i retrieve the key value from the below NSMutableArray array. The below code crashes on isEqualToString. However i can see the value of nsRet in the variable view window as #\x18\xaa\x01\xc8\a before running that statement.
NSMutableArray* nsMyList = [[NSMutableArray alloc] init];
[nsMyList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"valueOfKey", #"Key",
nil]];
NSString *nsRet = [nsMyList valueForKey:#"Key"];
if ([nsRet isEqualToString:#"deviceClass"])
{
NSLog(#"Key value:%#", nsRet);
}
Can anyone here please help me get the correct value for the key?
Thanks.
This is because you need objectForKey:, not valueForKey:. The valueForKey: method is for key-value programming. Moreover, the call should be on the [nsMyList objectAtIndex:0], like this:
NSString *nsRet = [[nsMyList objectAtIndex:0] objectForKey:#"Key"]
You've stored the NSDictionary in an array. The correct access based on your code would be:
NSDictionary *dict = [nsMyList objectAtIndex:0];
nsret = [dict valueForKey:#"Key"];
It looks like you are trying to get the valueForKey: on an NSMutableArray rather than on the dictionary.
What you want is:
[[nsMyList objectAtIndex:0] valueForKey:#"Key"];
I am a bit lost.
In order to access the dictionary you just create you need to obtain the first element in the NSMutableArray and then the dictionary.
It will be something like this:
NSString *nsRet = [nsMyList[0] objectForKey:#"Key"]
I think it can solve it.

Resources