Objective-C Storing Information - ios

In an iPhone app, the User can create Items - each Item needs to have a CGPoint, NSString and a few integers with information about it. The User can keep adding these Items.
How can I store all these variables for each of the Items and programmatically keep adding them to a list or array or something?
I tried using a struct array but it can't hold a NSString. I tried using a NSMutableAray of a custom class, but I can only add them if I make and name them by hand.
Any suggestions, ideas? Could I use a NSDictionary?

Use an NSArray or NSMutableArray, but you have to wrap your non-object values (CGPoints and integers) in wrapper objects. The integers can be wrapped in NSNumbers and the points can be wrapped in NSValues.

Create an Item class, that contains your CGPoint, NSString, and any other piece of info you want. Then, make an NSMutableArray and add new instances of your Item class as you see fit. NSDictionary and NSArray aren't really what you're looking for.

Related

How to maintain the order of key added in NSMutableDictionary

I didn't knew this at first place, I thought that the key/value pair I am adding to my dictionary will be in the same order when I try to retrieve it back...
Now here is my question -
I have a ViewController which I have made as singleton, inside this VC I have defined:
#property (nonatomic) NSMutableDictionary *dictionary;
Now I try accessing this dictionary from various other classes and set its contents via:
[[[ViewController sharedViewController] dictionary] setValue:[NSNumber numberWithBOOL:NO] forKey:#"xx"] , yy ,zz and so on
I have my delegates implemented which would hit for a particular key(xx,yy,zz and so on). Now whenever a particular delegate method is hit I need to update my dictionary with same key but [NSNumber numberWithBOOL:YES] which is happening .. but the order in which I added the items in dictionary is not maintained ... How can I achieve this?? I need to maintain the order in the same way in which I have added my items to the dictionary at first place??
Can someone please help me out??
As pointed, you should use NSArray to have your values ordered. You can also use some 3rd party APIs like: M13OrderedDictionary or others, however, that's not a pretty solution.
Obviously, the desired solution is to create your own object or struct and keep an array of those objects/structs.
NSDictionary is just a mapping between keys and values that has no defined order. If you try to iterate over the keys, you may or may not get them back in the same order you added them, and you should never depend on the ordering of the keys when iterating over them.
One thing you could do is create an NSArray that contains the keys in the order you want them. Then you can iterate over the keys in the array and get the values from the NSDictionary in the order you wanted. The combination of these two basically gives you a sorted key NSDictionary.
Another thing you could do is to just use an NSArray to stores the values, unless the keys that you're using matter as well. But if all you're trying to do is get the values in a certain order, then I would say NSArray should work fine.
You can make a copy of key by using the following code:
NSArray *allKeys = [dictionary allKeys];
Then, the keys all saved in order and you can access particular value by getting specific key in allKeys array and retrieving it by using dictionary[allKeys[2]].

Data structure that allows you to add multiple objects for the same key

I am looking for a data structure that allows me to add multiple objects for the same key.
I have a table that is downloading images and when it finishes downloading an image I want it to set the image in all the cells with that image name.
So I want a data structure where the key is the image name and it multiple objects i.e pointers to the cells.
I know I can't do this in NSMutable dictionary as it just overwrites the keys. Is there another way?
Simply create a NSDictionary containing a NSMutableArray :
NSDictionary *myDico = #{#"imageName":[NSArray arrayWithObjects:...]};
Where ... = the references to the cells.
Edit :
You also can, if you have multiple images, use a NSMutableDictionary.
How about using NSMutableDictionary whose value is a NSMutableArray? You can append the array when new cells are added or check nil and add an empty array when the key is not stored before.
Just use NSArray to add multiple values against single key.
NSDictionary *dictt = #{#"imageKey":#[#"image1", #"image2"]};

How to sort UITableView headers according to declaration order

So I have a little problem here, I think I made my point clear in the title of this post. I want to sort UITableView headers according to the order I declared them in code. I tried surfing the net already but I can't find any solution.
I think one from here can help. Thanks in advance!
EDIT: This is my code to of the table view contents
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:accSettingsOpts, #"Account",notifSettingsOpts,#"Notifications",aboutOpts,#"About",logoutOpt,#"Sign Out",nil];
I want to display the table view sorted by
-Account
-Notifications
-About
-Sign Out
But it displays as
-About
-Account
-Notifications
-Sign Out
EDIT: This is how the problem is addressed.
Based from the answer provided in which I accepted below, I modified it as
#interface myClass ()
NSArray *headerArr;
#end
at viewDidload I added
headerArr = [[NSArray alloc]initWithObjects:#"Account",#"Notifications",#"About",#"Sign Out", nil];
and lastly...
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [headerArr objectAtIndex:section];
}
Thanks for the help guys. I didn't imagine that it's just that simple.
A dictionary is an unordered collection. If you store your items in a dictionary, the order in which you defined them is not saved anywhere.
Table views are an inherently ordered list of objects. Therefore a dictionary is not suitable as a way of storing the items for a table view. An array, however, makes a natural data source for a table view.
Use an array rather than a dictionary. It's as simple as that. You can use an array of dictionaries, or an array of some other data objects, and then write your cellForRowAtIndexPath method to extract the data from your model (array elements) and install the values in the cell.
The reason the order is different from the declaration order is that NSDictionary is unordered: it is sorted based on the hash code of your strings, which is rather arbitrary.
To force a particular order, use a container with a fixed order, such as NSArray. For example, you can store your accSettingsOpts, notifSettingsOpts, and so on, in a single array, add a header property to the Opts class, and pull that property to set section headers in your UITableView.

NSDictionary with NSObjects as keys

I'm trying to create an NSDictionary that keeps track of calling objects for a function. I'd like to create a unique string for each object without knowing anything about it. My first thought is to use the memory address of the object's pointer, but I'm not sure how to do that.
Any thoughts? I need to use some sort of unique id from an NSObject as the keys in my dictionary.
If your application supports iOS6 only check the NSDictionaryOfVariableBindings macro.
The code would be something like :
// Create the dictionary
NSObject *firstObject = [NSString stringWithString:#"My first item"];
NSObject *secondObject = #"[#"an", #"array", #"of", #"strings"]";
NSDictionary *theDic = NSDictionaryOfVariableBindings(firstObject, secondObject);
// Access data
NSString *singleString = [theDic objectForKey:#"firstObject"];
NSArray *listOfStrings = [theDic objectForKey:#"secondObject"];
My suggestion is not to use a dictionary. If you were to place them into an array, you could think of it as a dictionary with automatically generated unique keys (the indexes). It's really exactly what you are describing. If for some reason you have to use a dictionary, my suggestion is to implement that same model I'm speaking of, but you would have to generate and maintain the keys.
While I agree that your solution sounds like it may not be the best approach, have you considered -hash in the NSObject protocol? All NSObjects should return one. Be forewarned that it's a hash, so there's a chance that two different objects could have the same hash.
You could also consider a category on NSObject that your collection implements. The category could generate a UUID to use as a key.

how to get data form NSMutablearray in to NSString

Hello.......
in my apps, i have problem with xml parising .i have same tags differentiate with integer value.each tag has different value.
now the problem me facing is accessing this value for that particular tag only.
plz anybody have idea abat this.
let me know.
mean's the problem is that i have one MutableArray and all record in it. and i want to show some values in one view and reaming some value show in another view and reaming in another view..
But the MutableArray is same..
and one more thing is that i want to get tag value
<subject>XYZ</subject>
the output is subject i want.
i do not need of XYZ i need only subject..
sorry i can not show what i want but please understand my question and give me the answer
Thanks
Use NSMutableDictionary instead NSMutableArray.
From documentation.
The NSMutableDictionary class declares the programmatic interface to
objects that manage mutable associations of keys and values. It adds
modification operations to the basic operations it inherits from
NSDictionary.

Resources