Mutable Object within an Immutable object - iphone

Is it acceptable to have a NSMutableArray within an NSDictionary? Or does the NSDictionary also have to be mutable?
The NSMutableArray will have values added to it at runtime, the NSDictionary will always have the same 2 NSMutableArrays.
Thanks,
Dan

Yes, it's perfectly acceptable. Keep in mind, the contents of the array are the pointers to your NSMutableArrays--those are what can't change in the immutable dictionary structure. What the pointers point to can change all you want. To wit:
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary *dict = [NSDictionary dictionaryWithObject:arr forKey:#"test"];
[arr addObject:#"Hello"];
NSString *str = [[dict objectForKey:#"test"] objectAtIndex:0];
NSLog("%#", str);

It's quite acceptable. But, it's precisely the sort of setup that suggests you should seriously consider replacing the dictionary with an NSObject subclass that sports two properties for accessing the arrays.

Related

how to add object at specified index in NSMutable array?

How can I add object at specified index?
in my problem
NSMutableArray *substring
contains index and object alternatively
and I need to add it to the another array str according to index I getting from this array.
NSMutableArray *str=[NSMutableArray new];
if ([substrings containsObject:#"Category-Sequence:"])
{
NSString *index=[substrings objectAtIndex:5];
//[substrings objectAtIndex:5]
gives me integer position at which I need to add object in `str` array,
gives 5,4,8,2,7,1 etc
NSString *object=[substrings objectAtIndex:1];
//[substrings objectAtIndex:1] gives object,gives NSString type of object
[str insertObject:object atIndex:(index.intValue)];
}
please suggest some way to achieve it.
Thanks in advance!
Allocate the array first & then try to add objects in it.
NSMutableArray *str = [[NSMutableArray alloc]init];
if ([substrings containsObject:#"Category-Sequence:"])
{
NSString *index=[substrings objectAtIndex:5];
NSString *object=[substrings objectAtIndex:1];
[str insertObject:object atIndex:(index.intValue)];
}
Allocate the NSMutableArray before inserting objects into it:
NSMutableArray *strMutableArray = [[NSMutableArray alloc] init];
(You’ll also need to release it when you’re done if you’re not using ARC.)
Or you could also use a temporary object, if you don’t need to keep strMutableArray:
NSMutableArray *strMutableArray = [NSMutableArray array];
Then you can insert objects into the NSMutableArray.
Be careful with using indexes of and in different arrays, however. There might be a better way to do what you want.

How to set array value and key in NSDictionary in iphone?

Here i am using NSMutableArray to store date, then i tried to set key and assign ArrayValue in dictionary but the app crashed, please help me
Thanks in ADvance
Here i tried the code for your reference:
[DateArray addObject:dateString]; //NSMutablArray
NSMutableDictionary *myDictionary =[[NSMutableDictionary alloc]init];
[myDictionary setObject:DateArray forKey:#"Date"]; //put array value and set key in NSDictionary.
NSDictionary Class is immutable. You must convert to NSMutableDictionary.
If you are using XCode version 4.4 or later you can jus do this:
[dateArray addObject:dateString]; //NSMutablArray
NSDictionary *myDictionary = #{ #"Date", dateArray };
You are using NSDictionary. You should use NSMutableDictionary.
NSDictionary is immutable. If you want to use NSDictionary then use below method:
- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
You're using an NSDictionary when you should be using NSMutableDictionary
Try this line :
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
Most of the collection classes in iOS have a mutable and nonmutable version (i.e.
NSArray -> NSMutableArray
NSSet -> NSMutableSet
NSDictionary -> NSMutableDictionary
(and others)
A mutable version will let you modify the contents. However, if you know you're not going to change it then a non mutable version will be slightly faster to use.
You can (usually) get a mutable class from a nonmutable one by using the mutableCopy method i.e.
// This array can't be changed
NSArray *myArray = #[ #"A", #"B", #"C" ];
// This array contains everything from the previous array but can now be modified
MSMutableArray myArray2 = [myArray mutableCopy];
NB : There are also other classes that have mutable subclasses i.e. NSURL -> NSMutableURL
Do it like this:
for(int i=0;i<[DateArray count]; i++)
{
[myDictionary addObject:[DateArray objectAtIndex:i] forKey:#"Date"];
}

how to split string into NSMutableArray

I want to split string into NSMutableArray
I know
- (NSArray *)componentsSeparatedByString:(NSString *)separator
is available but this one is for NSArray not for NSMutableArray.
I need because after spliting i want to remove element from array by using
-(void)removeObjectAtIndex:(NSUInteger)index
which is not possible with NSArray.
Thank you
You can also just get a mutable copy of the returned array:
NSMutableArray *array = [[myString componentsSeparatedByString:#"..."] mutableCopy];
Also, remember that copy, like alloc, does allocate new memory. So when used in non-ARC code you must autorelease the copied array or manually release it when you are done with it.
Make a new NSMutableArray using
NSArray *array = [NSString componentsSeparatedByString:#"..."];
NSMutableArray *mutable = [NSMutableArray arrayWithArray:array];
Create a NSMutableArray from the output NSArray created by componentsSeparatedByString.
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:array];

Difference between the following allocations types?

I have a simple code:
NSMutableArray *arrayCheckList = [[NSMutableArray alloc] init];
[arrayCheckList addObject:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"2011-03-14 10:25:59 +0000",#"Exercise at least 30mins/day",#"1",nil] forKeys:[NSArray arrayWithObjects:#"date",#"checkListData",#"status",nil]] ];
[arrayCheckList addObject:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"2011-03-14 10:25:59 +0000",#"Take regular insulin shots",#"1",nil] forKeys:[NSArray arrayWithObjects:#"date",#"checkListData",#"status",nil]]];
Now I want to add a specific index of above array to a dictionary. Below are two way, which one is better and why? What are the specific drawbacks of the latter?
NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary:[arrayCheckList objectAtIndex:1]];
OR
NSDictionary *tempDict = [arrayCheckList objectAtIndex:1];
What would the impact on the latter since I am not doing any alloc/init in it?
1:
NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary:[arrayCheckList objectAtIndex:1]];
Creates a new immutable dictionary object as a copy of the original one. If you add objects to the mutable dictionary in your arrayCheckList they will not be added to your copied reference.
2:
NSDictionary *tempDict = [arrayCheckList objectAtIndex:1];
This directly pulls the mutable dictionary from your array and not a copy. The following two lines will be equivalent:
[[arrayCheckList objectAtIndex:1] addObject:something];
[tempDict addObject:something];
The first one potentially copies the dictionary a index 1 of the array. (It should, since you're creating an immutable dictionary but the one in the array is mutable.) The second only gets a reference to the dictionary in the array -- there's no chance of creating a new object.

Creating a Two-Dimensional Array with Concrete Positions

I need to create a custom array:
In php I would define as follows:
$myarray[100][80] = 1;
But I don't know how to do it in objective-c...
I don't need an array [0][0],[0][1],[0][2], ... I only need concrete positions in this array [80][12], [147][444], [46][9823746],...
The content of these positions always will be = 1;
for this you would use a dictionary rather than an array as they are always 0,1,2 keyed so something along the lines of:
NSNumber *one = [NSNumber numberWithInt:1];
NSString *key = #"80,12";
NSDictionary *items = [NSDictionary dictionaryWithObject:one forKey:key];
Then to pull them out again you would use the objectForKey: method.
You cannot put ints directly into arrays or dictionaries that's why it is wrapped in the NSNumber object. To access the int after getting the NSNumber out of the dictionary you would use something like:
NSNumber tempNum = [items objectForKey:key];
int i = tempNum.intValue;
See the docs here for a full explanation of the NSDictionary class. Hope this helps...
I an not a PHP master but I believe in php arrays are not real arrays they are hash tables right?
Anyway, I think you are looking for NSDictionary or NSMutableDictionary class.
That looks more like a bitset than an array.
Allocating so many cells for that seems useless, so maybe you could revert the problem, and store the positions in an array.
Well in objective c we can use NSMutableArray to define 2-D arrays.
See the following code, it might help you
NSMutableArray *row = [[NSMutableArray alloc] initWithObjects:#"1", #"2", nil];
NSMutableArray *col = [[NSMutableArray alloc] init];
[col addObject:row];
NSString *obj = [[col objectAtIndex:0] objectAtIndex:0];
NSLog(#"%#", obj);