?
Solved

Declaring Private Methods

Posted on 2011-10-14
6
Medium Priority
?
360 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:pizdzielec
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:Snapples
ID: 36972860
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
 

Author Comment

by:pizdzielec
ID: 36978603
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
 
LVL 2

Expert Comment

by:Snapples
ID: 36982534
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:pizdzielec
ID: 36984300
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
 
LVL 2

Accepted Solution

by:
Snapples earned 2000 total points
ID: 36996368
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
 

Author Closing Comment

by:pizdzielec
ID: 37009646
Thx a lot.

Cheers,
P.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     Intro      In this article, I will show you how to parse a JSON in Swift using an open source library called SwiftyJSON (https://github.com/SwiftyJSON/SwiftyJSON). If you haven't heard of a JSON before, it stands for JavaScript Object Notation…
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question