Order NSDictionary allObjects by Keys [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can i sort the NSDictionary on basis of key in Objective-C?
I have a several strings in a .plist file that I am trying to return into the NSDictionary then into an array but the order being returned isn't the same as I saved it in the playlist.
So far I have tried
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *strs = [NSArray arrayWithArray:[dict allKeys]];
strs = [strs sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
but that is only returning the key names.

I actually was just looking through a few forums on google and found that this worked: Can i sort the NSDictionary on basis of key in Objective-C?

No, you can't.
You could keep an ordered array with your keys around as well, though.

Related

How to modify value in NSArray object? [duplicate]

This question already has answers here:
How do you change the elements within an NSArray?
(3 answers)
Closed 7 years ago.
I want to modify value in NSArray at index 0.
Here is sample program
NSArray *test = #[#"abc",#"rec",#"myPM"];
NSLog (#"%#",[test objectAtIndex:0]);
test[0]=#"001";
NSLog (#"%#",[test objectAtIndex:0]);
Here I am getting lot of errors. please help me in this.
You should use an NSMutableArray
And with this method :
[yourArray replaceObjectAtIndex:INDEX withObject:OBJECT].

I'm trying to sort an NSMutableArray of objects by a field of type double [duplicate]

This question already has an answer here:
Sorting with sortedArrayUsingDescriptors and Key Paths
(1 answer)
Closed 8 years ago.
the code I'm currently trying doesn't give any errors but after it runs nothing is sorted. This is my first time in objective-c so I'm hoping there's something obvious here that I'm missing.
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"distanceOfPlace" ascending:true] ;
[surroundingRestaurants sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
sortedArrayUsingDescriptors returns a new array, rather than modifying the original array. If your object is an NSArray, this is your only option, so you should store it back in the original array:
surroundingRestaurants = [surroundingRestaurants sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
If, however, you have an NSMutableArray, you can use sortUsingDescriptors, and it will modify the original array:
[surroundingRestaurants sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
By they way, [NSArray arrayWithObject:sortDescriptor] can be written more succinctly as #[sortDescriptor].
I think you mean...
NSArray *mySortedArray = [surroundingRestaurants sortedArrayUsingDescriptors:#[sortDescriptor]];
Refer to the docs. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html

Objective-C - sort array of strings based on string slice [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
How to do a natural sort on an NSArray?
(5 answers)
Closed 9 years ago.
NSMutableArray looks like ("xxx_20", "xxx_18", "xxx_16", "xxx_19", "xxx_17")
I want to sort it into ("xxx_16", "xxx_17", "xxx_18", "xxx_19", "xxx_20")
I've tried to use selectors, but I can't even get proper syntax. Does anyone know how to do this properly?
Try this,
NSMutableArray *myarray = [ NSMutableArray arrayWithObjects:#"xxx_20", #"xxx_18", #"xxx_16", #"xxx_19", #"xxx_17", nil];
NSArray *sortedArray = [myarray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"%#",sortedArray);
It worked fine at my end.

How to declare a variable named after the value of another variable in Objective C [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Syntax help - Variable as object name
I have a very basic beginner's question in Objective C:
I'd like to declare a variable that is named after the content/value/literal of another variable. How can I do that?
I.e.
NSString *s = [NSString stringWithFormat:#"variableName"];
// now create a second variable that is named after the literal in s
int *s = 42; // This is what doesn't work. The integer-type variable should be named variableName
Does anyone know how to do this?
Thanks for the answers so far. The reason why I am asking this questions is the following:
I have an array containing values I load from an xml file structured as follows:
<string>name</string><integer>23</integer><real>3.232</real><real>4.556</real> ... (44 times another real number)<string>nextName</string>...(and so on).
The file contains the names for MKPolygons, the number of points for each polygon and the latitude and logitude values for each point of the polygon. I managed to load the file and have its content in an array. Now I want to create MKPolygons from this array which are named as the strings in the array.
What you are looking for is a hash table. You can use NSDictionary or NSMutableDictionary or NSHashTable.
Here is an example:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:anObj forKey:#"foo"];
[dict objectForKey:#"foo"];
[dict removeObjectForKey:#"foo"];
[dict release];
This is not possible in Objective-C, nor in most mainstream languages (save for PHP). What are you trying to accomplish with this?
I don't think you can do it. However, you can create an NSDictionary and create a key called variableName and a value called 42. (an NSNumber). Then whenever you want to retrieve this value, you can just do valueForKey.

How can I do a multidimensional lookup table in Objective-C?

New to Objective-C here...
I have a unique string code as the key that I'd like to use to look up associated values.
It seems an NSDictionary is what I'm looking for but I believe this is only used to lookup one value for a given key.
Can anyone provide the code on how to declare/fill a multidimensional immutable NSDictionary? Also the code to retrieve the values?
Also, please let me know I'm going about this the wrong way
Thanks
EDIT: example of array within a dictionary .... using example data from your comment.
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
NSArray *firstSet = [[NSArray alloc] initWithObjects:43, 22];
NSArray *secondSet = [[NSArray alloc] initWithObjects:32, 50];
[myDictionary setValue:firstSet forKey:#"010"];
[myDictionary setValue:secondSet forKey:#"011"];
// Get a value out - should be the 50 that you want
NSInteger *myValue = [[myDictionary objectForKey:#"011"] objectAtIndex:1];
Not tested - but should be 95% right. Does this make sense?
You can make any object you want be the value for a given key in an NSDictionary. This includes NSArray or even another NSDictionary. Using either of these would allow you to associate multiple values with one key.
For nested NSDictionaries or custom KVC-complient classes you can use keyPaths, and for nested NSArrays indexPaths.
Also stackoverflow will give you many examples.