I have a textfield dynamically generated and it is added in to an mutable array named field:
[field addObject:textfield];
textfield contains all the properties of the textfield (like placeholder etc...).
Now I have a mutable dictionary with a key-value pair like this:
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:#{} forKey:#"V"];
Now what I want to achieve is that I want to change the value of m.V[field[i].placeholder] with the value #"abc". It is inside a for loop.
What I want is that to add a value corresponding to field[i].placeholder key.
I dont know how to write the above code.
Can anyone please help? I hope the question is clear.
EDIT:
for(UITextField *f in field){
[[m objectforkey:#"V"] setobject:#"abc" forkey:#"placeholder"];
}
This is what i am trying to achieve.But it shows error.
I suspect the issue is that the inner dictionary is immutable as you have used the newer Objective-C literal syntax. Instead, create the dictionary with the following code, which explicitly creates an inner mutable dictionary:
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
NSMutableDictionary *inner = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"value1", #"key1",
#"value2", #"key2",
nil];
[m setObject:inner forKey:#"V"];
Your question is not clear, but I guess this is the answer for your question.
for (UITextField *textfield in [m valueForKey: #"V"]){
textField.placeholder = #"abc";
}
Related
I have a very strange behaviour with NSArray.
Firstly, i parsed an XML file and return a valid NSArray with NSDictionary inside:
NSArray *arrayOfDict = [parser parseWithXMLFile:filename];
In debugger it's fully valid. Then, i want to proccess all dictionaries in this array:
NSMutableArray* arrayOfProducts = [[NSMutableArray alloc] init];
for (NSDictionary* dict in arrayOfDict) {
Product* product = [[Product alloc] initWithName:dict[#"name"]
type:dict[#"type"]
imageName:dict[#"image"]
description:dict[#"description"]];
[arrayOfProducts addObject:product];
[product release];
}
And in this loop is a problem: a dict variable has value nil. And i don't know what to do with this. In debugger i evaluate value of arrayOfDict[someIndex] and get a right value, but in the programm itself it doesn't work.
May be it's the problem with MRR, i don't feel myself confidenly while using MRR and there is a mistake of using it.
P.S. I know that using MRR is stupid today, but in this project i must use it.
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 have a NSDictionary and a NSArray:
self.dataDic = [[[NSDictionary alloc] init] autorelease];
self.dataDicKey = [[[NSArray alloc] init] autorelease];
These are the key and value of my NSDictionary:
self.dataDic =
#{#"Ring Cloud" :#[#"Contact Cloud", #"Image Cloud", #"Data Cloud"],
#"Ring Tools" :#[#"Sticker Market", #"Live Poll", #"Mail"],
#"Ring Apps" :#[#"Studio", #"Player"]};
Now what I want is to insert all the key value of my self.dataDic into self.dataDicKey sequentially. That means it looks like this:
self.dataDicKey = [[[NSArray alloc] initWithObjects:#"Ring Cloud", #"Ring Tools", #"Ring Apps",nil] autorelease];
I have tried with this:
self.imageDicKey = [[[self.imageDic allKeys] reverseObjectEnumerator] allObjects];
But it's not actually sorted the values. If any one have any suggestion please share it with me.
Thanks a lot in advance.
If you are trying to get the keys as objects in your NSArray you should try this method
self.DataDicKey = [[NSArray alloc]initWithArray:[self.imageDic allKeys]];
One thing to remember that NSDictionary allKeys method returns only the keys regardless of order they were saved. as its KVP
This will copy your NSDictionary keys into NSArray
NSDictionary keys are unsorted - maybe you could save the keys in array before you enter the data to the dictionary
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"];
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.