iOS Basics (15) Knowledge points

Source: Internet
Author: User

An SEL

1. Where to store the method

The method address lists for each class are stored in the class object.

Each method has an object of the SEL type that corresponds to it.

The address of the method can be found based on a Sel object, and the method is called.

Person.h

1 #import <Foundation/Foundation.h> 2  3@interface person:nsobject  4  5 + (void) test1; 6 7 -(void) test2; 8 9 -(void ) Test3: (NSString *) ABC; Ten  @end

Person.m

1#import"Person.h"2 3 @implementation Person4 5+ (void) test1{6NSLog (@"Call method Test1---\ n");7 }8 9- (void) test2{TenNSLog (@"Call method Test2---\ n"); One } A  -- (void) Test3: (NSString *) param1{ -NSLog (@"Call method test3---%@", param1); the } -  -@end

Main.m

1 [P test2]; 2     3 [P performselector: @selector (test2)];

Line 1th The first way: Call the Test2 method directly from the P object.

Line 2nd The second way: Call the Test2 method indirectly.

1) Wrap the test2 into SEL type data.

2) Find the corresponding method address according to the SEL data.

3) Call the corresponding method according to the method address.

1 [P test3:@ "123"]; 2 3 [P performselector: @selector (test3:) Withobject:@ "123"]; 4

Line 1th: The P class calls the method Test3 with parameters directly.

Line 3rd: The P class indirectly calls the method with the parameter test3, and notice that when the method with the parameter is called indirectly, it is called using the form @selector (method:).

1 @" test2 " ; 2 SEL s2 = nsselectorfromstring (name); 3 NSLog (@ "%p"  , s); 4 NSLog (@ "%p" , S2);

iOS Basics (15) Knowledge points

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.