Objective-c SEL & Class IMP

Source: Internet
Author: User
Classes (class type) in OBJC (objective-c), Selector (selector sel), function pointer (IMP) I saw a cow in the garden today "OBJECTIVE-C 2.0 with Cocoa Foundation--- The 5,class type, selector selector, and function pointer are very exciting, and can not help but annotate its code with annotations for later viewing. Personal experience: The "class type variable" in OBJ-C is more flexible than the object base class in C #, and it can be used to generate any type of instance (but it is not nsobject). and the selector sel and the function pointer imp, if you want to have a relationship with C #, these two combine, on the point similar to the Reflection + delegate in C #, you can invoke the method directly according to a method name string. "OX" base class Cattle.h View source print?
1 #import <Foundation/Foundation.h>
2
3 @interface Cattle: nsobject {
4 int legscount;
5 }
6 -(void) saysomething;
7 -(void) Setlegscount: (int) count;
8 @end
CATTLE.M View Source print?
01 #import "Cattle.h"
02
03 @implementation Cattle
04
05 -(void) saysomething
06 {
07 NSLog (@ "Hello, I am a cattle, I have%d legs.", legscount);
08 }
09
10 -(void) Setlegscount: (int) count
11 {
12 Legscount = count;
13 }
14 @end
Subclass "Bull" Bull.h View source print?
01 #import <Foundation/Foundation.h>
02 #import "Cattle.h"
03
04 @interface Bull:cattle {
05 nsstring *skincolor;
06 }
07 -(void) saysomething;
08 -(nsstring*) Getskincolor;
09 -(void) Setskincolor: (nsstring *) color;
10 @end
BULL.M View Source print?
01 #import "Bull.h"
02
03
04 @implementation Bull
05
06 -(void) saysomething
07 {
08 NSLog (@ "Hello, I am a%@ bull, I have%d legs.", [self getskincolor],legscount);
09 }
10
11 -(nsstring*) getskincolor
12 {
13 return skincolor;
14 }
15
16 -(void) Setskincolor: (nsstring *) color
17 {
18 Skincolor = color;
19 }
20 @end
Proxy class DoProxy.h (here's the key code) View source print?
01 #import <Foundation/Foundation.h>
02
03 Defining several string constants
04 #define Set_skin_color @ "Setskincolor:"
05 #define BULL_CLASS @ "BULL"
06 #define CATTLE_CLASS @ "cattle"
07
08
09 @interface Doproxy: nsobject {
10 BOOL Notfirstrun;
11
12 ID Cattle[3];
13 Define two selectors
14 SEL say;
15 SEL Skin;
16
17 Define a function pointer (traditional C language processing method)
18 void (*SETSKINCOLOR_FUNC) (ID,SEL,nsstring*);
19
20 A function pointer that defines an IMP method (the recommended way in obj-c)
21st IMP Say_func;
22
23 Define a class
Related Article

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.