Selector in Objective C

Source: Internet
Author: User

Quoted from http://www.roseindia.net/tutorial/iphone/examples/iphone-selector.html

Selector in Objective C
In short, selector can either be a name of method or a message to an object when used in the source code. and SEL is the complied form of a selector. also remember that all methods with the same name have the same selector. we can also use selector to invoke a method on object.

Let's find out the iPhone selector syntax given below ..

Selectorappdelegate. h

#import <UIKit/UIKit.h>@interface selectorAppDelegate : NSObject <UIApplicationDelegate> {UIWindow *window;NSString *string;}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) NSString *string;@end

Selectorappdelegate. m

#import "selectorAppDelegate.h"@implementation selectorAppDelegate@synthesize window, string;- (void)applicationDidFinishLaunching:(UIApplication *)application {       // Override point for customization after application launch    [window makeKeyAndVisible];    NSString *str = @"HELLO, THIS IS NEELAM";        SEL sel = @selector(lowercaseString);    NSString *lower = (([str respondsToSelector:sel]) ? @"YES" : @"NO");    NSLog (@"Responds to lowercaseString: %@", lower);    if ([str respondsToSelector:sel]) //(lower == @"YES")        NSLog(@"lowercaseString is: %@"  , [str lowercaseString]);    }- (void)dealloc {    [window release];    [super dealloc];}@end

As you can see in the code, Sel is the complied form of @ selector. A selector can be written

Sel sel = @ selector (lowercasestring );

Lowercasestring will change all the uppercase string into the lower case.

On running the application it'll look like as given image:

 

Quoted from http://stackoverflow.com/questions/738622/selectors-in-objective-c

You have to be very careful about the method names. In this case, the method name is just"lowercaseString", Not"lowercaseString:"(Note the absence of the colon). That's why you're gettingNOReturned, becauseNSStringObjects respond tolowercaseStringMessage but notlowercaseString:Message.

How do you know when to add a colon? You add a colon to the Message name if you wocould Add a colon when calling it, which happens if it takes one argument. If it takes zero arguments (as is the caselowercaseString), Then there is no colon. If it takes more than one argument, you have to add the extra argument names along with their colons, as incompare:options:range:locale:.

You can also look at the documentation and note the presence or absence of a trailing colon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.