Amazon.com Widgets Creating vs. Initialising
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 06:23:42 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  Creating vs. Initialising
Pages: [1]   Go Down
Print
Author Topic: Creating vs. Initialising  (Read 724 times)
Smikey
Newbie
*
Posts: 11



Email




« on: November 25, 2009, 01:23:15 PM »

Ok, apologies if this is a rather daft question, but I can't quite seem to grasp the difference between creating an instance of and initialising a class.

Using the NSMutableArray as an example, there are 2 methods:

+ arrayWithCapacity:
– initWithCapacity:

I understand that the '+' denotes a Class method, where as the '-' denotes an instance method, but what exactly is the difference between these 2 methods? Presumably they both create new arrays?

Thanks Smiley

Michael
Logged
toby
Full Member
***
Posts: 104






« Reply #1 on: November 25, 2009, 04:21:26 PM »

Without getting into too much detail on the technicalities here: creating an instance gives you an instance of the class, however its not ready to be used yet, it requires some base setup work to be done. Initialising the class does the base setups that are required before you actually use your new class and in some cases may also include setting default values for the object's instance variables (or values given as an argument).

In the example below, your "arrayWithCapacity" class method does not require initialising. However if you were to have created the array using "alloc" then you would need to use the "initWithCapacity" to set it up. In allot of cases (all I think but I ain't making promises here) the instances you create using class methods similar to these (not alloc, ones which give you more than a blank instance) should not require initialisation(they should have been automatically initialised to allow their initial value to be set - in the example this would be the size of the array-), but when you create a blank instance with alloc then you need to initialise it.
« Last Edit: November 25, 2009, 04:23:46 PM by toby » Logged
Smikey
Newbie
*
Posts: 11



Email




« Reply #2 on: November 26, 2009, 02:17:15 PM »

Right... Thanks for your reply. I'm afraid I'm still a little confused.

If you initialise a class, you create an instance of the object, with default values.

But if you initialise an instance of a class, what are you creating?

So here I am initialising an instance of a class:

NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity: 10]

But how would I use arrayWithCapacity: instead? And what would be the advantage?

Thanks for the help Smiley
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #3 on: November 26, 2009, 03:39:48 PM »

Michael,

Quote
But if you initialise an instance of a class, what are you creating?
You aren't creating anything. The alloc method created the instance for you.

Have you read about arrayWithCapacity in the Developer Documentation?

Quote
arrayWithCapacity:
Creates and returns an NSMutableArray object with enough allocated memory to initially hold a given number of objects.

So:

NSMutableArray *myArray = [NSMutableArray arrayWithCapacity: 10];

Would be the same as:

NSMutableArray *myArray = [[[NSMutableArray alloc] initWithCapacity: 10] autorelease];

Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
Smikey
Newbie
*
Posts: 11



Email




« Reply #4 on: November 29, 2009, 04:05:26 AM »

Ah ok - I think I get it now. Thanks for the help!  Cheesy
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.