Is there any similiar to a private method in Objective-C? - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 13, 2008, 12:36 PM   #1
fenrus110
macrumors regular
 
Join Date: Mar 2008
Is there any similiar to a private method in Objective-C?

I want to just write a simple helper method, but I don't want to expose it to the public interface (since that breaks encapsulation and what not).

Does Objective-C support anything like this?

Like if I write this
NSString *s = [self helper:txtFirst.text];
s = [self helper:txtLast.text];

Obviously, I'll get a compile error if I haven't declared "helper" in the header file. Is there any way around this?
fenrus110 is offline   0 Reply With Quote
Old Apr 13, 2008, 12:40 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Just declare helper: in the .m file before @implementation, like so:

Code:
@interface YourClass (PrivateMethods)
- (NSString *)helper:(NSString *)arg;
@end
Then you can call it all you want but only the code in your .m can see it.
kainjow is offline   0 Reply With Quote
Old Apr 13, 2008, 12:50 PM   #3
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Nice tip, I didn't realise you could do that.
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is online now   0 Reply With Quote
Old Apr 13, 2008, 12:55 PM   #4
fenrus110
Thread Starter
macrumors regular
 
Join Date: Mar 2008
thanks
fenrus110 is offline   0 Reply With Quote
Old Apr 13, 2008, 01:16 PM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Note that unlike Java this doesn't actually prevent other objects accessing these methods. You'll get the normal warning about methods not existing when you compile, but it'll work at runtime.
robbieduncan is offline   0 Reply With Quote
Old Apr 13, 2008, 02:05 PM   #6
Krevnik
macrumors 68020
 
Krevnik's Avatar
 
Join Date: Sep 2003
Quote:
Originally Posted by robbieduncan View Post
Note that unlike Java this doesn't actually prevent other objects accessing these methods. You'll get the normal warning about methods not existing when you compile, but it'll work at runtime.
Yes, the problem is that the Obj-C runtime doesn't offer the protections during execution, as it doesn't know what is private and what isn't when it comes to methods.

But, for the sake of most development, the distinction of public/private isn't to actually protect code, but act as enforcement for good code policy, and help make it clear what can and shouldn't be used in APIs. Categories are sufficient for this purpose.
__________________
iMac 2013 27", 13" rMBP, iPad 4, iPhone 5s
Krevnik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Objective-C Method Calls counter TheHeavySoldier Mac Programming 4 Mar 12, 2014 07:47 AM
example objective c code arkmannj iPhone/iPad Programming 2 Jul 26, 2013 10:05 PM
Class Method -> Function -> Instance Method? Blakeasd Mac Programming 11 Jun 19, 2013 02:53 AM
Objective-C noob: EXC_BAD_ACCESS with simple print method? dancks Mac Programming 14 Apr 27, 2013 04:47 PM
Imac program similiar to power point? Vell843 Mac Applications and Mac App Store 2 Aug 20, 2012 12:42 PM

Forum Jump

All times are GMT -5. The time now is 04:12 AM.

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

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