Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-09-2010, 08:36 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 7
nexus is on a distinguished road
Exclamation Understanding what I'm doing...

Hi all,

I'm getting into Objective-C and I'm beginning to understand the concepts of OOP.

I'm trying to figure out what and idea of what is going on here:

Code:
- (id) init
{	
	self = [super init];
	
	if (self)
	{
		value = 0.0;
		for (int i = 0; i < 6; ++i)
			savedValues[i] = 0.0;
	}

	return self;
}
Mainly the self [super init] line is what I'm trying to understand.

Any help would be appreciated.

Last edited by nexus; 04-09-2010 at 09:07 AM.
nexus is offline   Reply With Quote
Old 04-09-2010, 09:03 AM   #2 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

The second block of code is initiating the viewcontroller and setting some values.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 04-09-2010, 10:58 AM   #3 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Mr Jack is on a distinguished road
Default

Quote:
Originally Posted by nexus View Post
Mainly the self [super init] line is what I'm trying to understand.
In objective-C the keyword super is a pointer to the class inherited from. This allows you explicitly call functions in the base class (aka superclass) that you've overridden in your derived class (aka sub class).

So, [super init] calls the init function on the class you're inheriting from, which probably calls it on the class inherits from and so on up the chain, ensuring that every "layer" of the inheritance hierarchy gets initialised properly.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 04-09-2010, 12:47 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

In general, classes "extend" or "inherit from" other classes. That means they perform all the functions of the "parent" class, also called a "super class" , plus whatever functions you add.

If you check the your .h file, you'll see that your class extends some other class, probably NSobject.

Code:
@interface MyClass : NSObject
The superclass usually performs important setup in its init method, you you had better call it using [super init] if you want it to be run when your init is called.

In the same vein, if someone ever subclasses your class you want to make sure they run your init method, right? So they call [super init] which runs your init method which calls [super init] which runs NSObject's init method. That's the chain Mr Jack mentioned.

Extra credit:
The reason you say self=[super init] is that for some classes "init" can return a different address than the one it was called on. This only happens in specialized cases like singletons, but you should code for it anyway.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-09-2010, 08:49 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 7
nexus is on a distinguished road
Default

Thanks guys for the responses. A little over my head right now, but I'll learn...

Last edited by nexus; 04-09-2010 at 08:53 PM.
nexus is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 461
14 members and 447 guests
7twenty7, AlanFloyd, David-T, imac74, Jaxen66, logan, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0