-1

I am learning about the alloc and init but I don't understand what its supposed to mean. they gave example of

NSString *emptyString = [[NSString alloc] init];
NSArray *emptyArray = [[NSArray alloc] init];
NSDictionary *emptyDictionary = [[NSDictionary alloc] init];

And i got no clue what it would even be used for.


Next i had to use the stringWithString and did this. no clue whats going on here too. Can someone help me but explain it in a understandable/easy matter. thank you

NSString *firstName = @"Daniel";
NSString *copy = [[NSString alloc] stringWithString:firstName];
NSLog(@"%@ is a copy of %@", copy, firstName);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jeff
  • 45
  • 7

2 Answers2

1
  1. Creating the empty array and dictionary is a bit strange; normally you'd only do that with their mutable versions. I suppose the empty string could be used for comparisons with other string objects, but even then, using @"" seems like it would be easier to read.

    alloc is a class method that allocates space for an object of the appropriate class. init is an instance method that initializes that new instance so that it can behave correctly as an object of that class.

  2. This use of stringWithString is incorrect; you shouldn't be using it with alloc. Something like:

    NSString *copy = [firstName copy];
    

    would be more appropriate.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

First Point:

If you are familiar with C++ code, when you create an object, basically you create a new instance of that class as follows:

ClassA object1= new ClassA();

new is nothing but alloc+init in the objective-C

ClassA *object1 = [[ClassA alloc] init];

Second Point:

[NSString stringWithString:@"some string"]

It is equivalent to

[[NSString alloc] initWithString:@"some string"]

Here is some more information:

alloc : Class method of NSObject. Returns a new instance of the receiving class.

init : Instance method of NSObject. Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.

new : Class method of NSObject. Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.

release : Instance method of NSObject delegate. Decrements the receiver’s reference count.

autorelease : Instance method of NSObject delegate. Adds the receiver to the current autorelease pool.

retain: Instance method of NSObject delegate. Increments the receiver’s reference count.

copy : Instance method of NSObject delegate. Returns a new instance that’s a copy of the receiver.

So to conclude we can say that

alloc goes with init

new = alloc + init

More information could be found https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Initialization/Initialization.html

zaph
  • 111,848
  • 21
  • 189
  • 228
casillas
  • 16,351
  • 19
  • 115
  • 215
  • It should be pointed out that C++ `new SomeClass()` is actually two operations: The `new` operation (which can be overridden but rarely is) and the `SomeClass()` constructor. These correspond quite closely to `alloc` and `init`. – Hot Licks Apr 23 '15 at 00:31
  • Note that `new` can be used: `ClassA *object1 = [ClassA new];` – zaph Apr 23 '15 at 02:09