how to read the fields in the object that is store in the array in objective-c - iphone

my appdelegate is having the array book, this array is storing the many object like latitude and longitude coming from server. this object are containing the many latitude and longitude values coming from server.
values coming from the sever is store in the object file booknew and then this objects are stored in the array.
how can i read that values of latitude and longitude store in the object and that objects are store in the array.

Assuming the objects have accessors for these values, you'd just call those.

If the data that comes from the server is an XML file it's easy. A simple way is to define the data as an array of objects, where each object is an NSDictionary. In each dictionary you can have values for the keys "lat" and "lng", as well as other information connected to each location. If this is how the data is arranged, you could access those values with something like this:
for (id dict in book) {
float latitude=[[dict objectForKey:#"lat"] floatValue];
float longitude=[[dict objectForKey:#"lng"] floatValue];
//code to use values here
}
If this is close to how the data can be arranged in the file, you can read it in with NSArray *book=[NSArray alloc] initWithContentsOfFile:filename] or similar.
If you want to be able to change the objects or the array, you must use an NSMutableArray array or NSMutableDictionary objects.
If the above is not doable (for example, if you have no influence over the file format), you will have to spend much more time writing custom code to read it in, parse it, and create objects yourself.

Related

How can i save the order of an NSMutableArray

I have an NSMutableArray and I change the order of the contents of it. How can I save the order of it in core data without using a relation ship, or should I use NSUserDefaults?
Why not have a second array that is a copy of the first one at the time that you want to preserve its order? It need not be a mutable array - use [myArray immutableCopy]
If you don't want to copy all the items in the array for memory reasons, perhaps store the location as an NSInteger? Have the first array contain the items, and the second array contains indexes for the first array.

How do I watch for changes made in NSMutableArray or NSMutableDictionary?

I've noticed that the method 'hashcode' for both NSMutableDictionary and NSMutableArray is giving me the hashvalue to be the number of keys irrespective of the value for NSMutableDictionary and number of objects in case of NSMutableArray.
Basically, I want to detect the change in NSMutableDictionary. My dictionary contains key/value pairs as string/NSMutableArray. And I'want to detect the change in dictionary if an item is added/deleted from any of its values.
In case if I go for calculating hash for NSMutableArray which are the values in my dictionary, it's not feasible since there's also a possibility of a different value added and existing deleted in which case simply giving me the number of items are going to remain same but the hash should be different.
Which is the best way to handle such change in dictionary?
You can retain the original dictionary an test if it is equal to the new one
isEqualToDictionary:
Returns a Boolean
value that indicates whether the
contents of the receiving dictionary
are equal to the contents of another
given dictionary.
- (BOOL)isEqualToDictionary:(NSDictionary*)otherDictionary
Docs here

Loading data objects into an NSArray causes a painfully slow startup

I have a Core Data database of about 500 objects. These objects are 'cards' that will be viewed and modified by the user. When a user modifies the card, an attribute called 'groupNumber' will change.
The order of these cards in each group is very important and is determined by the user. I load the database objects into an array. When a user makes a change, I save the order of the array into a plist using the 'title' attribute.
My problem comes when the app is relaunched. I need to load the group array in the order it was saved in. But when I use the plist to do a fetch request, it is painfully slow.
The slow code is:
// get array from plist sorted by 'title'
NSMutableArray *group1Temp = [plistData objectForKey:#"group1ArrayData"];
for (int i = 0; i < [group1Temp count]; i++) {
// set predicate to 'title' attribute
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"title == %#", [group1Temp objectAtIndex:i]];
// load filtered database object into temp array
NSArray *temp = [self.vocabDeckArray filteredArrayUsingPredicate:predicate];
[self.group1Array addObjectsFromArray:temp];
}
When this executes 500 times, it is just too slow. If I could save an NSArray of the database objects themselves into a plist, then I wouldn't need to do a predicate search, but it seems I can't do that.
I'm thinking my entire approach to this has been wrong. How can I save/load the order of an array of database objects in a faster way? Is loading database objects into an array itself bad practice?
Thanks for any help!
Traversing the entire vocabDeckArray and filtering it once for each object in group1Temp is very inefficient. There are any number of ways you could rebuild this sorted data set in less than O(n^2) time.
One easy option might be to store a dictionary with the object's titles as keys and their position in the array as values. That way you can construct an array of known length and put every object in vocabDeckArray into the correct position in a single pass (get first object from vocabDeckArray, lookup where in belongs in group1Array from the dictionary, insert into group1Array, move on to next object). That's still not particularly fast but it seems like a minimal change to your current behavior.
In addition consider the number of method calls within your loop. self.vocabDeckArray and self.group1Array are method calls which you make on every iteration of your loop even though they always return the same objects. Keeping local variables which reference those objects instead would save you the overhead of 2 method calls on every iteration.

Objective-C, I'm having some problems with arrays and dictionaries atrting at zero or not?

I'm using a dictionary and an array to store the section title for a table view, along with the number of rows in each. I'm adding to the array and the dictionary in a loop of the records from my database in mu populateDataSource method.
I've got two dates from three records in my database. 2010-11-05 and 2010-11-07
I'm not sure if its because the table view events need to start at zero and the dictionary / array start at one ?
Or maybe my sectionTitles addObject, is added values each time, rather than adding unique dates. If so how i can search or only allow unique values ?
Any ideas ?
Arrays have a starting index of 0, but dictionaries don't essentially work that way. What is the purpose of the dictionary in this case? To get an object from the dictionary you give it a key. If you're going through a loop and not getting the expected result, maybe the dictionary isn't getting the key that it should be.

Separating UITableView data into sections from one source

I have one table in my core data source which holds some articles with NSDate's. I basically want to separate the NSManagedObject into days, so you can scroll through and separate articles by date.
What is the best way to approach this? My context is queried out in descending date order, so just need to split that up into days for the UITableView sections, rather than 1 big section.
When you load the data for your table -- whether that's using NSFetchedResultsController and Core Data, or loading a .plist file into an NSDictionary object -- you can "section" it as you like.
You'll first step through the data (e.g. use the fetchedObjects property if using NSFetchedResultsController) and determine what sections you want. Since you're wanting to split on dates, you might store cutoffs represented by NSDate objetcts in an array. You would then use this array to implement the various UITableViewDataSource methods: in numberOfSectionsInTableView: you can return the count of this array, and in sectionIndexTitlesForTableView: you can return NSString representations of those stored NSDates for the section titles.
Methods like tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath: are a little trickier. You'll probably want to use another "preprocessed" data structure (perhaps a two-level array of arrays, where the inner arrays contain the entity objects you fetched), and you'll want to set up as the same time as your array of NSDate objects.