iOS Add object in key on NSMutableDictionary (not so simple as that) - iphone

How do I add more then one object for the same key? is that possible?
For example, I need to have a dictionary like this:
object:textField.text for key #"Permanent key"
I need to always add objects to that permanent key depending on how many times the user types something on the textfield.
So my dictionary would have lots of entries for the key "permanent key".

This is not possible, keys have to be unique for each value in the dictionary.
If what you need is several objects like this, why not just make an NSMutableArray or NSMutableSet of them? Or have your object be itself an NSMutableArray and you assign it to that key and simply add to this NSMutableArray the new values that are required.

You can Use an NSMutableArray as object, in which you put the objects you want to assign to that unique key.

If you were able to add (say) object A and object B for the same key; Which object would you get when you retrieve with that key? A or B?
Keys must be unique by definition; They are like the numerical indices of an array, but instead of consecutive numbers, they can be arbitrary strings.
As others pointed out, you want to store an array of objects for that key, instead of a single object.

Related

Can you obtain an unique identifier for a MATLAB object?

I am debugging some MATLAB code, and want to make sure that two references to an object are actually referring to the same object. Is there a way to obtain a unique identifier for the objects (such as a memory address)?
As far as I know I am not able to add my own IDs to the objects, as they are MATLAB random number streams.
If you are using OOP then you could add a property ID and set it during the construction of the object.
java.rmi.server.UID() is a nice way to obtain unique ID's
However testing by == will check the actual handles, so this is more of a usability issue.
classdef yourClass < handle
properties
ID
end
methods
function obj = yourClass()
obj.ID = java.rmi.server.UID();
end
end
end
It will then be rather simple to check your objects.
If the objects you're wanting to compare are MATLAB random number streams (i.e. they are of class RandStream), then they are handle objects. In that case you don't need unique IDs: if you compare them using eq or == and they are equal, then they are the same object.
As you say, you are not able to add your own properties to an object of class RandStream, but if you really wanted to you could subclass RandStream and add a property of your own to the subclass. You could store a unique identifier in the property, generated with char(java.util.UUID.randomUUID).
You can use the UserData field, which is present in every graphical object, to store a unique identity generated by you. If working with a user-defined class, you can add a similar field in your class.
The identities can be kept unique by using a global counter to assign each new identity.

Can I insert an object into an NSDictionary?

I am pulling in a list of coordinates from our web server. I'm storing the whole list as an NSDictionary. While I am using the data (lat, long, radius, name) I would like to update the list of data with 1 or 2 more fields. Is there a way to insert these 2 items to the existing dictionary?
My only existing thought is that I would have to load the data into an NSMutableDictionary to allow additions and just create the new dictionary and overwrite the existing one with the new updated data. Hoping for a simpler or cleaner way rather than ghetto rigging it. Thanks in advance.
From the docs:
The NSDictionary class declares the programmatic interface to objects
that manage immutable associations of keys and values.
So, NSDictionary is immutable--you may not add to it. Using an NSMutableDictionary is not unclean or "ghetto rigging"...this is the whole purpose for an NSMutableDictionary.
Your thought is correct, you have to create an NSMutableDictionary, as the NSDictionary can not be modified.
NSMutableDictionary *newDoc = [[NSMutableDictionary alloc] initWithDictionary:doc];
If you have any immutable arrays or dictionaries in the NSDictionary, you might need a deep copy for modifcations.
NSMutableDictionary *newDoc = (NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, doc, kCFPropertyListMutableContainers);

How should i add Object in NSDictionary for the same key at runtime?

I am trying to add value as array in NSDctionary for the same key, i don't how should i add like that, what i mean in PHP, we have add multi dimension array at runtime like fetching the value form DB like for the same key, we need to add into NSDictonary?
If you have multiple objects associated with a single dictionary key, then you can store those objects in an NSArray, then add an NSArray instance for each key:

NSMutableArray of NSString in CoreData(iPhone

I have a class, which describe an object for parsing XML. This class contains NSStrings and NSMutableArrays of NSStrings.I want to store parsed data using CoreData. How should I write CoreData model to store such objects if there is no such data type like NSMutableArray? Is there any way?
You can create two entities, I'll call them Array and String. Array has one relationship, strings, which is a one-to-many relationship with the String entity. The String entity then has one attribute, I'll call it string too, which actually is a string and contains the value you need. This will allow you to store them as a set, if the order is important add another attribute to the String entity called order and make it a number.
There isnt an array data type, but u can box the string in a core data object (lets call it A for the remainder of the post). Then in the object (B) which you want to have the array of string (that persists) in just make a to-many relationship with the object A which contains the string, then youll have a Set of string on the object B which you can turn into an array and do with what you like. hope that helps

Can I tell Core Data to use a specific unique ID for an y object when saving it?

Example: I read data from an XML file. This data has unique id elements. I want to store those objects with their original unique id. How would I do that?
I figured out I could ask the managed object for it's ID, like this:
NSManagedObjectID *moID = [managedObject objectID];
but here the problem is: The XML tells me with the id element which object this is, and I need to look up in the database of core data if this object already exists in there, or not. So is it the only option to make an id attribute in my managed object model for that entity and then query for that? Then I will have two id systems right?
Don't worry about the ObjectID of Core Data. This is an internal unique ID which is not guarantied to be constant during the object's life cycle (e.g. it will change when you save the object to sql store). Just create a new mandatory attribute in your model and flag it as indexed so retrieval will be fast.
In the entity associated to this kind of objects, simply add another attribute of type string, call it objectID or similar and declare it to be mandatory.