Amazon.com Widgets initWith: - Chap 10, pg 206, Prog 10.1
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 06:00:56 AM
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
| |-+  Chapter Study
| | |-+  Chapter 10 - More on Variables and Data Types
| | | |-+  initWith: - Chap 10, pg 206, Prog 10.1
Pages: 1 [2]   Go Down
Print
Author Topic: initWith: - Chap 10, pg 206, Prog 10.1  (Read 4126 times)
applefan90
Newbie
*
Posts: 29






« Reply #15 on: April 29, 2011, 07:16:51 PM »

Instead of overriding self with

Code: (Objective-C)
self = [super init];

Why can't you just do this:

Code: (Objective-C)
[self [super init]];

Hmm, now that I typed it I think I may understand.  Correct me if I'm wrong please:
The method will have to return itself in order to initialize properly.  If you do the previous block of code, you would have no way of returning the overridden self variable.
« Last Edit: April 29, 2011, 07:20:20 PM by applefan90 » Logged
skronwith
Newbie
*
Posts: 17


Email




« Reply #16 on: September 08, 2011, 11:31:45 AM »

My question in all of this is why bother?  That is why bother with writing our own initialization methods?  It may save a few lines of code, though I don't even see it really doing that.  Is there a reason to do this besides a bit of elegance to the code-writing?
Logged
Trimakas
Newbie
*
Posts: 9






« Reply #17 on: November 06, 2011, 09:46:13 AM »

I'm kinda with you one this one.. I did have a problem understanding this as well.. But someone else's pain helped me to understand what was going on..

Thanks for that!
Todd
Logged
seerex
Full Member
***
Posts: 177


Email




« Reply #18 on: November 06, 2011, 01:14:46 PM »

The above code you've written doesnt work coz when you say [self [super init]; you arent returning a value to self. You are really trying to call the super init within self. So to speak. Atleast i think so :=) you are accessing the super init within self.

When you do self = [super init]; you call the parents initializer, and the initialized object is returned into self. If you dont do the = assignment operator, it would have no way of returning it.

 As to your question on why you would ever write your own initializer, i was wondering this as well. But trust me, when you start writing some code on your own you will at some point say "hey, if i write a new initializer, i can do this much faster". I did that in my previous project, where i had some values inside a class i knew i needed to set for each instance i created, thus it made sense to make it part of the initialization message.

Hope i helped.

Best Regards
/JBJ
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Pages: 1 [2]   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.