Declaring Private Methods

Hi,
I'm having question about how to declare, use and call private method in objective-c?
For example, if I have something like this:
 
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

-(NSString *) genRandStringLength: (int) len {
    
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%c", [letters characterAtIndex: rand()%[letters length]] ];
    }
    
    return randomString;
}

Open in new window


how can call it?

Cheers,
P.
pizdzielecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SnapplesCommented:
In short, you can't, objective C doesn't support private methods.
You can however hide methods by using a category so that they can only be used in that class.

Something like this.


@interface MyClass (hidden)
-(NSString *) genRandStringLength: (int) len;
@end

@implementation MyClass

-(NSString *) genRandStringLength: (int) len {
    
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%c", [letters characterAtIndex: rand()%[letters length]] ];
    }
    
    return randomString;
}

@end

Open in new window

0
pizdzielecAuthor Commented:
Well, I'm just at the beginning on my way to be a coder so could you tell me, please,  exactly how to use your solution?

Cheers,
P.
0
SnapplesCommented:
Well, the piece of code I posted above is all you need to do really.

At the top of your .m file where you want to declare your "private" method, above @implementation, just put this:

@interface MyClass (hidden)
-(void)thisMethodIsHidden;
@end

Open in new window


MyClass is the name of your class, and hidden can be replaced with any word you want, it's just a name you give to the category. And between that and @end you can declare your methods just like you would in the .h file, with the difference that the method will be hidden.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

pizdzielecAuthor Commented:
Sorry but it doesn't works. Next to @interface there is error about "Cannot find interface declaration for MyClass". And next to @implementation MyClass I got warrning ""Cannot find interface declaration for MyClass"

As you can see, all I need is to display random characters in "randomCharacters" text field after pressing button "makeRandomChars"

This are my .m and *.h files:
//
//  random_charAppDelegate.m
//  random_char

#import "random_charAppDelegate.h"

@interface MyClass (hidden)
-(void)thisMethodIsHidden;
@end

@implementation MyClass

    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

-(NSString *) genRandStringLength: (int) len {
    
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%c", [letters characterAtIndex: rand()%[letters length]] ];
    }
    
    return randomString;
}

@end

@implementation random_charAppDelegate

@synthesize window;

- (IBAction)makeRandomChars:(id)sender
{
    
    NSString* result = @"";
    [randomCharacters setStringValue:result];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

@end

Open in new window

//
//  random_charAppDelegate.h
//  random_char
//

#import <Cocoa/Cocoa.h>

@interface random_charAppDelegate : NSObject <NSApplicationDelegate> {
    
    NSWindow *window;
    IBOutlet NSTextField *randomCharacters;
    
}


@property (assign) IBOutlet NSWindow *window;

- (IBAction)makeRandomChars:(id)sender;

@end

Open in new window

0
SnapplesCommented:
The "MyClass" was just an example, you should put it at the top of your .m file where you want to use the private method. Like this.


//
//  random_charAppDelegate.m
//  random_char

#import "random_charAppDelegate.h"

@interface random_charAppDelegate (hidden)
-(NSString*)genRandStringLength:(int)len;
@end

@implementation random_charAppDelegate

@synthesize window;

- (IBAction)makeRandomChars:(id)sender
{
    
    NSString* result = @"";
    [randomCharacters setStringValue:result];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

-(NSString *) genRandStringLength: (int) len {
    
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
    for (int i=0; i<len; i++) {
        [randomString appendFormat: @"%c", [letters characterAtIndex: rand()%[letters length]] ];
    }
    
    return randomString;
}

@end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pizdzielecAuthor Commented:
Thx a lot.

Cheers,
P.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
iOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.