Hi i am having a NSdictionary in which i am adding a array with key "countries ". Now i take the value of this dictionary into array and sort the array in alpahbatical order .Now i want to add this array into my Dictionary (that is i want to update my dictionary with new sorted array and remove the old array from it )........ how to do this
My code is as follows
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:#"Iceland", #"Greenland", #"Switzerland", #"Norway", #"New Zealand", #"Greece", #"Italy", #"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:#"Countries"];
NSArray *tmpary = [countriesToLiveInDict valueForKey:#"Countries"];
NSLog(#"ary value is %#",ary);
NSArray *sortedArray = [tmpary sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"sortedArray is %#",sortedArray);
Here i want to remove the countriesToLiveInArray and replace it with sortedArray with same key value i.e. Countries
Thanks in advance..
First you need to use a NSMutableDictionary and put this code :
[countriesToLiveInDict removeObjectForKey:#"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:#"Countries"];
First of all make your NSDictionary to NSMutableDictionary & then write the following line of code
[countriesToLiveInDict removeObjectForKey:#"Countries"];
This will definitely resolve your issue.
NSDictionary cannot remove anything, please use NSMutableDictionary, like this:
NSMutableDictionary *countriesToLiveInDict = [NSMutableDictionary dictionaryWithObject:countriesToLiveInArray forKey:#"Countries"];
for Swift 3
as #MathieuF answered it
First you need to use a NSMutableDictionary and put this code :
countriesToLiveInDict.removeObject(forKey: "Countries")
i post my answer as i was search for same question and get inspired by #MathieuF
Related
At the moment I'm creating an array as per below:
NSArray *data = #[#[#20, #40, #20, #60, #40, #140, #80],];
However I'd like to be able to create the same array from data already in an NSMutableArray.
I've tried this but the library I'm passing the array to does not like it.
NSArray *data = [NSArray arrayWithArray:self.altData];
("altData" is an NSMutableArray)
Any ideas?
An NSMutbaleArray is a subclass of an NSArray. So you can just do NSArray *immutable = [myMutableArray copy] or if you know that you're not going to change the mutable array after a point then you can do NSArray *immutable = mutableArray; maybe followed by mutableArray = nil. Your solution is also fine.
I am new to objective c. I am interesting in having something like this:
- for each key I want to store multiple values like:
2 holds a,b,c
3 holds d,e,f
When pressing 2 3 or 2 3 3, I want to have at output all the combinations from these 6 values. Should I use a NSMutableDictionary for this? I need some advices!
You can store arrays in dictionaries. For example
NSDictionary *mapping = #{#"2": #[#"a", #"b", #"c"]};
and you could for each key press add the objects from the array in the dictionary to an intermediate array
NSMutableArray *values = [NSMutableArray array];
...
// For each time a key is pressed
[values addObjectsFromArray:#[mapping[keyPressed]]];
...
When you want to display the output you calculate all combinations for all values in the values array.
For store multiple value of single key, you need to add array as value of dictionary key, such like,
NSArray *temArray1 = ...// which has value a,b,c
NSArray *temArray2 = ...// which has value d,e,f
Add this array as value of specific key, such like
NSMutableDictionary *temDic = [[NSMutableDictionary alloc] init];
[temDic setValue:temArray1 forKey#"2"];
[temDic setValue:temArray1 forKey#"3"];
NSLog(#"%#", temDic)
Above code describe simple logic as per your requirement change it as you need.
Please Try this..
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *aryFlashCardRed=[[NSArray alloc]initWithObjects:#"f1",#"f2",#"f3", nil];
NSArray *aryFlashCardYellow=[[NSArray alloc]initWithObjects:#"f4",#"f5",#"f6", nil];
NSArray *aryFlashCardGreen=[[NSArray alloc]initWithObjects:#"f7",#"f8",#"f9", nil];
NSArray *aryScore=[[NSArray alloc]initWithObjects:#"10",#"20",#"30", nil];
[dictionary setObject:aryFlashCardRed forKey:#"red"];
[dictionary setObject:aryFlashCardYellow forKey:#"yellow"];
[dictionary setObject:aryFlashCardGreen forKey:#"green"];
[dictionary setObject:aryScore forKey:#"score"];
Display dictionary like this
{
green = (
f7,
f8,
f9
);
red = (
f1,
f2,
f3
);
score = (
10,
20,
30
);
yellow = (
f4,
f5,
f6
);
}
Objective-c has 3 types of collections: NSArray, NSSet, NSDictionary (and their mutable equivalents). All this collections can store only objects. This collections uses for different cases, so try to find case which is appropriate to You and use appropriate collection.
P.S. My first wish was to write RTFM
Try like below :-
NSDictionary *yourDict=[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:#"a", #"b", #"c",nil]
forKeys:[NSArray arrayWithObjects:#"2",nil]];
I would like some help sorting an NSArray of NSDictionary values based on each objects ISV key.
This is the code I have so far for creating my array objects so you have a better idea of what I am trying to do.
NSArray *combinedKeysArray = [NSArray arrayWithObjects:#"HASM", #"ISL", #"ISV", nil];
valuesCombinedMutableArray = [NSMutableArray arrayWithObjects:[dict objectForKey:#"HASM"],
[dict objectForKey:#"ISL"],
[dict objectForKey:#"ISV"],
nil];
combinedDictionary = [NSDictionary dictionaryWithObjects:valuesCombinedMutableArray
forKeys:combinedKeysArray];
[unSortedrray addObject:combinedDictionary];
// how do I then sort unSortedArray by the string values in each object ISV key?
any help would be greatly appreciated.
This can solve your problem
How to sort an NSMutableArray with custom objects in it?
https://stackoverflow.com/a/805589/1294448
You can use NSSortDescriptor to sort NSArays
Then in NSArray you have a method called sortedArrayUsingDescriptors
Or NSComparisonResult ca also be helpful some time http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/doc/uid/20000138-BABCEEJD
you won't be able to sort unSortedArray because it will only have one element in it (ie in your last line of code you are adding a single object by addObject).
That said, you cannot sort the dictionary either.. b/c dictionaries are unsorted by definition.
you can iterate over the keys of the dictionary in a specific order though, you can sort an array containing the keys of the dictionary.
NSArray *keys = [theDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:#selector(compareMethod:)];
You can use -sortedArrayUsingComparator: to sort any way you need.
[unSortedrray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *dict1, NSDictionary *dict2) {
return [[dict1 objectForKey:#"ISV"] localizedCompare:[dict2 objectForKey:#"ISV"]];
}];
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.
I want to add #"ALL ITEMS" object at the first index of NSARRAY.
Initially the Array has 10 objects. After adding, the array should contains 11 objects.
you can't modify NSArray for inserting and adding. you need to use NSMutableArray. If you want to insert object at specified index
[array1 insertObject:#"ALL ITEMS" atIndex:0];
In Swift 2.0
array1.insertObject("ALL ITEMS", atIndex: 0)
First of all, NSArray need to be populated when it is initializing. So if you want to add some object at an array then you have to use NSMutableArray. Hope the following code will give you some idea and solution.
NSArray *array = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"0", nil];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
[mutableArray addObject:#"ALL ITEMS"];
[mutableArray addObjectsFromArray:array];
The addObject method will insert the object as the last element of the NSMutableArray.
I know that we have six answers for insertObject, and one for creating a(n) NSMutableArray array and then calling addObject, but there is also this:
myArray = [#[#"ALL ITEMS"] arrayByAddingObjectsFromArray:myArray];
I haven't profiled either though.
Take a look at the insertObject:atIndex: method of the NSMutableArray class.To add an object to the front of the array, use 0 as the index:
[myMutableArray insertObject:myObject atIndex:0];
NSArray is immutable array you can't modify it in run time. Use NSMutableArray
[array insertObject:#"YourObject" atIndex:0];
NSArray is immutable but you can use insertObject: method of NSMutableArray class
[array insertObject:#"all items" atIndex:0];
As you are allready having 10 objects in your array,and you need to add another item at index 11...so,you must try this.... hope this helps..
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithCapacity:11];
[yourArray insertObject:#"All Items" atIndex:0];
NSArray is not dyanamic to solve your purpose you have to use NSMutableArray. Refer the following method
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
Apple documents says NSMutableArray Methods
[temp insertObject:#"all" atIndex:0];
Swift 3:
func addObject(){
var arrayName:[String] = ["Name1", "Name2", "Name3"]
arrayName.insert("Name0", at: 0)
print("---> ",arrayName)
}
Output:
---> ["Name0","Name1", "Name2", "Name3"]