xcode > reorganise Mutable array values - objective-c

I am new to Xcode and i have created a NSmutableArray that retrieves its values from a function. i would like to able to re organise the contents of my Array
When i output my array, i get the following results
(value A,value B,value C,value D)
I would like to re organize my array to appear a customised way so i would like it to appear in the following way
(value B,value D,value A,value C)
Any ideas on how to acheve this ?
I am using xcode 4.6.2

User can see the custom order and sorted order. right?
Have a NSMutableArray. Organise the content and Iterate to show it in UITableview
When you want to show sorted content in UITableView use the below method to get the sorted array.
-(NSMutableArray *)mySortedArray
{
return [myArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
}

Related

How to reorder MKMapView annotations array

I'd like to reorder the array of annotations shown on a map in order to create a next/prev button for quickly cycling through all annotations (sorted by date) using a simple iterator.
As far as I see the annotations array used as a store [worldmap annotations] is not mutable and therefore I cannot reorder it. I tried the following to create a temporary copy of the annotations array, sort it by date and re-attach it.
(worldmap is my MKMapView object)
//COPY
NSMutableArray *annotationSort = [[NSMutableArray alloc]initWithArray:[worldmap annotations]];
//SORT
[annotationSort sortedArrayUsingComparator:^NSComparisonResult(EventPin* obj1, EventPin* obj2) {
return [obj1.eventItemObject.eventDateBegin compare: obj2.eventItemObject.eventDateBegin];
}];
//ADDED SORTED ARRAY
[worldmap removeAnnotations:[worldmap annotations]];
[worldmap addAnnotations:annotationSort];
This doesn't seem to work. Any idea how can I reorder the MKMapKit annotations array?
As the answer in the linked question mentions, there is no guarantee that the map view's annotations property will preserve any order.
In addition, since the annotations property includes the userLocation if you have showsUserLocation turned on (but which you don't yourself explicitly call addAnnotation for), the annotation order will not be what you may expect.
Don't rely on the order of the annotations in the map view's annotations array.
Instead, keep your own array of references to the annotations and sort them any way you want (like your annotationSort array).
But there's no point in removing them from the map and adding them back.
Keep in mind that the map view's annotations array may contain the MKUserLocation annotation so when constructing your array, check the type of each annotation before including it or accessing custom properties.
However, note that the code to sort the array:
[annotationSort sortedArrayUsingComparator:...
is flawed itself because sortedArrayUsingComparator returns an NSArray.
It does not sort the array in-place.
Instead, to sort an NSMutableArray, call its sortUsingComparator method.
Using this sorted array, your app can access or select the annotations in the order desired.

Count of dictionaries in NSMutableArray

I have a NSMutableArray *objectsArray; containing dictionaries, each NSDictionary have a "Name" string with the objects name as value (no surprise). UITableView is already sorted using NSSortDescriptor by name in ascending order.
Now I need to section the TableView using the name's first letter, but I cannot find the needed code for:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
/*Return: How many letters from a-z are represented
as the first letter of the objects name?
I.e. If there is no object in the dictionary with name starting with letter B,
the number of sections are now 25 if all the other letters are included.*/
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Return: How many objects are starting with each letter
So I need to know how to return the correct number of section and rows?
Ps. Is there anything else that I have to do to make this it work, like in cell creation process cellForRowAtIndexPath or when passing the objectsArray through the segue to the DetailViewController?
Additional info about app structure:
objectsArray is from a plist array with dictionaries. using a custom prototype cell to populate the Table View. Xcode 4.2 with storyboard. App for iPhone 5.1
Create a NSMutableSet before you sort. Have your comparator store, in your set, the first letter of each Name it encounters as it sorts. Then the number of sections is the size of your set. This way, you pay very little cost since your comparators are already having to iterate through the array anyway.

Comparing items using number codes (JSON)

in my app, the users can create groups. When a user creates a group, a new folder is uploaded on dropbox, and inside there is a JSON file like this:
{"group":"0864798478"}
The code you see is generated randomly in the following way:
NSString *randomKey = [NSString stringWithFormat:#"%0.10u", arc4random()];
Then I have a table view which displays all the folders (groups). But I would like the UITableView to display only the groups which mach with the codes the iPad has saved on.
So if I have:
{"group a":"0864797073"}
{"group b":"0764898478"}
{"group c":"2864758479"}
And on the iPad there is a file containing the code:
0864797073 (group a)
The table View displays only that group, not all.
How can I do this??
Thanks in advance for the help!!
You can use JSONKit or SBJson for parse JSON files and obtain NSDictionaries with the data. Then you can create a NSDictionary in wich you could save the result of comparing previous dictionaries, an then show the result in the table with method tableView:cellForRowAtIndexPath. OR, make the comparison in the method tableView:cellForRowAtIndexPath. Good Luck!
EDIT:
If the file reads:
{"group a":"0864797073"}
You parse it and get a dictionary like in wich "group a" will be the KEY and the NSString "0864797073" will be the value. So to compare the dictionary wich holds group a with wich holds group b you have to do:
if([[firstParsedJson objectForKey:#"group a"] isEqualToString:[secondParsedJson objectForKey:#"group b"]])
{
//Do some stuff...
}
Where firstParsedJson and secondParsedJason are NSDictionaries that represent parsed json with, for example, SBJson.
As Luis has mentioned, SBJson is the easiest framework that you allows you to parse JSON using objective-C. You can parse the JSON and go through the CellForRowAtIndexPath delegate method of the UITableView to show all the values.
If you have having any troubles with visulizing the JSON then this link will help you to get a better idea of how your JSON is being created.

Distinct Object using NSPredicate

I have an NSArray of custom objects. Consider that the custom objects have a PageNumber property. I would like to filter my NSArray with a condition like "customObject.PageNumber is distinct".
I know I can loop through the array and eliminate object with duplicate pageNumbers. But is there any easy way to do it? I have tried,
[myarray valueForKeyPath:#"distinctUnionOfObjects.pageNumber"];
It is giving me the unique page numbers (like 7, 8, 9). But I want the custom object itself rather than just page numbers. Can any predicate help me?
I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC distinct method:
NSArray* itemsWithUniquePageNumbers = [items distinct:^id(id item) {
return [item pageNumber];
}];
This returns an array of objects, each one with a unique page number.
Yes, that is possible with the help of NSPredicate
customObject=[(NSArray*)[myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.PageNumber==%d",pageNumber]] lastObject];
//pageNumber is an integer
The filtered array is an NSArray of your custom objects which is the result of filtering using the predicate. Since your page number is unique, it will return only an array of one object. We get that by passing lastObject message to it.
Refer:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/predicates.html#//apple_ref/doc/uid/TP40001798-SW1

traverse Core Data object graph with added predicate?

I want to load a client object and then pull their related purchase orders based on whether they have been placed or not, purchase orders have an IsPlaced BOOL property.
So I have my client object and I can get all purchase orders like this, which is working great:
purchaseordersList =[[myclient.purchaseorders allObjects] mutableCopy];
But ideally I would actually like 2 array's - one for each order type: IsPlaced=YES and IsPlaced=NO
How do I do that here? Or do I need to do another fetch?
First, there is no reason to be turning the set into an array unless you are sorting it and there is no reason to be turning that array into a mutable array. Did you get that from some example code?
Second, you can filter an array or a set by using a predicate so you can create two sets (or arrays) easily via:
NSSet *placed = [[myclient purchaseorders] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"isPlaced == YES"]];
NSSet *notPlaced = [[myclient purchaseorders] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:#"isPlaced == YES"]];
If you are wanting to use this for a UITableView then look into a NSFetchedResultsController instead. It will save you a LOT of boiler-plate code.
Do you remember what example code you got that from? Been seeing that -mutableCopy a lot lately and would love to quash it. :)