0

I don't know if the question was asked previously and Im searching for some good answer.

Question is : Whenever sometimes I don't use [[... alloc] init] for some mutable class , I get crash.

Example :

NSMutableDictionary *myDict = someObject ; //[allocation of some other dictionary object directly without using alloc , init].

For some stages compiler warns me at runtime that you can not change values within myDict even though it is mutable.

Instead of that if I write code :

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithDictionary:someObject]; 

then it works.

So why alloc , init is necessary ? Or What is the problem actually ?

Thanks.

Vizllx
  • 9,135
  • 1
  • 41
  • 79
Rajesh
  • 546
  • 9
  • 29
  • 6
    are you sure you arent assigning a normal `NSDictionary` to the `NSMutableDictionary` variable and then trying to mutate it? – Fonix Jul 29 '15 at 07:14
  • Im assigning a normal dictionary to mutable object . Whats the problem in that ? @Fonix – Rajesh Jul 29 '15 at 07:32
  • 1
    well think about it, you cant mutate a unmutable `NSDictionary`, even if its assigned to a `NSMutableDictionary` variable type, that does not mean the NSDictionary has become an NSMutableDictionary. you have to do it with the alloc init, so that it creates an NSMutableDictionary with the same key/values as the NSDictionary before – Fonix Jul 29 '15 at 07:34
  • 1
    @RSD You can't do that. Assigning an object to another type does not change your objects original type. If you want to assign an `NSDictionary` to an `NSMutableDictionary` reference, you have to use the `mutableCopy` method of `NSDictionary`. – halileohalilei Jul 29 '15 at 07:34
  • 1
    No, There is no mutable object, just a pointer that you have misstated as mutable. If you need a mutable copy then create a mutable copy: `NSMutableDictionary *myDict = [someObject mutableCopy] `. – zaph Jul 29 '15 at 07:36
  • @Fonix thats what i was expecting ... thanks ! – Rajesh Jul 29 '15 at 07:36
  • @halileohalilei thanks !! – Rajesh Jul 29 '15 at 07:37

2 Answers2

3

Objective-C is based on C that is not a strongly typed language: you can put object of some type in variable of other type (basically because, in objective-C, they are all represented by their pointers).

When you write:

NSDictionary *immutableDict = [[NSDictionary alloc] init...];
NSMutableDictionary *mutableDict = immutableDict;

you just store the pointer to the immutable dictionary into the variable prepared to store a pointer to a mutable dictionary. But it doesn't magically transform your immutable dictionary in a mutable one.

But when you write:

NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] initWithDictionary:immutableDict];

you create a mutable dictionary that you initialize (fill) with the values contained in your immutable dictionary. So you can now modifiy your newly allocated mutable dictionary because it is mutable.

Nicolas Buquet
  • 3,880
  • 28
  • 28
2

This is very basic question there are already number of solutions present to your question ( to understand alloc, init, new refer following )

alloc, init, and new in Objective-C

Use of alloc init instead of new

alloc and init what do they actually do

Community
  • 1
  • 1
Shrikant K
  • 1,988
  • 2
  • 23
  • 34