Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 18, 2013, 05:38 PM   #1
MickeyT
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
"Hiding" init method

I have written a class where one of its instance variables needs to point to the object which created it. I have written an initWithName:andContainer method where one of the second argument requires an instance of the creating object. A weak pointer is then created to this "containing" object.

Normally, I would also override the init method, call the above method and pass some dummy parameters. However, in this case I don't want init to be "available" because I can't pass a dummy parameter for the containing object. These objects must never be created without an "owning" container.

Is there a way to stop init being available, or do I just set it up so that the init method returns nil which would, I assume, cause an exception to be thrown and thus force the use of the initWithName:andContainer method?

Thank you
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3

Last edited by MickeyT; Feb 20, 2013 at 03:49 AM.
MickeyT is offline   0 Reply With Quote
Old Feb 18, 2013, 06:00 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by MickeyT View Post
do I just set it up so that the init method returns nil which would, I assume, cause an exception to be thrown
Returning nil would not cause an exception to be thrown unless you somehow set it to throw yourself. In Obj-C, nil evaluates to zero and any calls to it will simply return zero.

There's an answer on StackOverflow over here suggesting that you should throw an NSInternalInconsistencyException:

http://stackoverflow.com/questions/1...in-objective-c

Edit: Actually, I think I like the answer below it more... doesNotRecognizeSelector: sounds like exactly what you want:

http://stackoverflow.com/a/5772821/901641
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)

Last edited by ArtOfWarfare; Feb 18, 2013 at 06:06 PM.
ArtOfWarfare is offline   0 Reply With Quote
Old Feb 18, 2013, 06:52 PM   #3
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Quote:
Returning nil would not cause an exception to be thrown unless you somehow set it to throw yourself. In Obj-C, nil evaluates to zero and any calls to it will simply return zero.
Sorry - what I meant was that when an attempt is subsequently made to use the object, I thought an exception would be thrown because the [super init] method would never have been called and so it wouldn't have been set up properly.

But I realised just now that you're absolutely right - I coincidentally didn't initialise an NSString object just now, and when I use it there's no exception, it just doesn't work.

Thanks for the reply - I'll have a look over those links and see if I'm happy before resolving the thread.

Many thanks.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Feb 18, 2013, 06:59 PM   #4
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
I wouldn't call [self release] and then [super whatever] since you'll probably be dealloced from the [self release] call. At any rate, it's neurotic to be trying to do memory management to avoid a memory leak when on the next line you're going to deliberately crash your app, as suggested in one of those links.

Any of the suggested solutions is fine.
PhoneyDeveloper is offline   1 Reply With Quote
Old Feb 18, 2013, 09:29 PM   #5
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by PhoneyDeveloper View Post
I wouldn't call [self release] and then [super whatever] since you'll probably be dealloced from the [self release] call. At any rate, it's neurotic to be trying to do memory management to avoid a memory leak when on the next line you're going to deliberately crash your app, as suggested in one of those links.
Not necessarily... you could catch the exception somewhere, could you not?
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Feb 18, 2013, 10:24 PM   #6
PBG4 Dude
macrumors 65816
 
Join Date: Jul 2007
If I understand you correctly, you're saying that Object A instantiates Object B and requires a pointer to Object A?

If so, you should set up Object A to be a delegate of Object B.
PBG4 Dude is offline   0 Reply With Quote
Old Feb 19, 2013, 04:48 PM   #7
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Quote:
If so, you should set up Object A to be a delegate of Object B.
Forgive me - what is the difference between what I am doing and setting up as a delegate?

Whenever I have set something as the delegate of something else, I was under the impression that "delegate" was just the name that was given to the instance variable I was assigning the object to (and I was using the setter method setDelegate to do that). In this case, that's what I do, but my instance variable isn't called "delegate", it is called something else.

And I am doing this via an init method, because I absolutely want this to always happen.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3

Last edited by MickeyT; Feb 19, 2013 at 05:04 PM.
MickeyT is offline   0 Reply With Quote
Old Feb 19, 2013, 05:03 PM   #8
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by MickeyT View Post
Forgive me - what is the difference between what I am doing and setting up as a delegate?
Delegation usually has a protocol associated with it. And delegate methods that may need to be implemented. All you're doing, I believe, is capturing a simple relationship between two objects, a parent-child one, as it were.
__________________

Last edited by dejo; Feb 19, 2013 at 06:49 PM. Reason: Fixed typo.
dejo is offline   0 Reply With Quote
Old Feb 19, 2013, 05:27 PM   #9
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Quote:
All you're doing, I believe, it capturing a simple relationship between two objects, a parent-child one, as it were.
That's right. Whether what I'm doing is a good way or a bad way to achieve what I'm setting out to do is possibly irrelevant at this stage; I want to do it so that if it turns into a nightmare then I will learn from that.

Essentially, I might end up with a hierarchy of perhaps three or four levels. When I'm down at level four, I want to be able to access instance variables at level one if the situation warrants it. Chances are the view controller dealing with the level four object won't have any knowledge of the level one object, and so I was thinking I would need a way of moving up and down the chain without having to bring the level one object with me everywhere.

That might not make sense - the app is going to be navigation controller based and so I was making my model layer to sort of follow the same drill down convention.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Feb 19, 2013, 07:40 PM   #10
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by MickeyT View Post
I have written a class where one of its instance variables needs to point to the object which created it. I have written an initWithName:andContainer method where one of the second argument requires an instance of the creating object. A weak pointer is then created to this "containing" object.

Normally, I would also override the init method, call the above method and pass some dummy parameters. However, in this case I don't want init to be "available" because I can't pass a dummy parameter for the containing object. These objects must never be created without an "owning" container.

Is there a way to stop init being available, or do I just set it up so that the init method returns nil which would, I assume, cause an exception to be thrown and thus force the use of the initWithName:andContainer method?

Thank you

One of the stack overflow links posted by another poster suggested adding a compiler directive that would throw a compile-time error:

-(id)init __attribute__((unavailable("init not available")));

That seems like the best option to me. You get warned at compile time so you know not to use the init method.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote
Old Feb 20, 2013, 03:49 AM   #11
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Thank you everyone - very helpful.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Recursively calling init method… am I doing this right? ArtOfWarfare Mac Programming 19 Feb 2, 2014 06:15 PM
2010 27" iMac screen issue..."dirty", "cloudy", image retention issues MMcCraryNJ iMac 1 May 18, 2013 05:02 PM
Start a new tab similar to "iOS blog" and "Mac blog" but make it "IPhone Leaks?" Dewroo Site and Forum Feedback 2 Aug 23, 2012 10:47 AM
Best method to play games on MBP 13" phas3 MacBook Pro 7 Jul 7, 2012 10:36 AM
Any method for connecting 27" TB to 24" ACD? jonnysods Mac Peripherals 8 Jun 26, 2012 09:02 AM

Forum Jump

All times are GMT -5. The time now is 06:37 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC