Skip to content

Typdef、Enum 、Struct

Silver edited this page Sep 3, 2020 · 5 revisions

Typedef

Same to Objective-C

typedef NSInteger dispatch_once_t;

Enum

Not srpport NS_ENUM and NS_OPTION.

You should use C syntax.

Example:

You can run the code by changing the content of ViewController1 in OCRunnerDemo.

// link NSLog
void NSLog(NSString *format, ...);
// the code is editing from UIKit.h and has deleted the pre-compiles. it can execute in OCRunner directly.
// it will add a type named 'UIControlEvents' and it's super type is NSUInteger. same to typedef.
// and all of them are global constant and the type of them is 'NSUInteger'
typedef enum: NSUInteger{
    UIControlEventTouchDown                                         = 1 <<  0,      // on all touch downs
    UIControlEventTouchDownRepeat                                   = 1 <<  1,      // on multiple touchdowns (tap count > 1)
    UIControlEventTouchDragInside                                   = 1 <<  2,
    UIControlEventTouchDragOutside                                  = 1 <<  3,
    UIControlEventTouchDragEnter                                    = 1 <<  4,
    UIControlEventTouchDragExit                                     = 1 <<  5,
    UIControlEventTouchUpInside                                     = 1 <<  6,
    UIControlEventTouchUpOutside                                    = 1 <<  7,
    UIControlEventTouchCancel                                       = 1 <<  8,

    UIControlEventValueChanged                                      = 1 << 12,     // sliders, etc.
    UIControlEventPrimaryActionTriggered                            = 1 << 13,     // semantic action: for buttons, etc.

    UIControlEventEditingDidBegin                                   = 1 << 16,     // UITextField
    UIControlEventEditingChanged                                    = 1 << 17,
    UIControlEventEditingDidEnd                                     = 1 << 18,
    UIControlEventEditingDidEndOnExit                               = 1 << 19,     // 'return key' ending editing

    UIControlEventAllTouchEvents                                    = 0x00000FFF,  // for touch events
    UIControlEventAllEditingEvents                                  = 0x000F0000,  // for UITextField
    UIControlEventApplicationReserved                               = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved                                    = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents                                         = 0xFFFFFFFF
}UIControlEvents;

int main(){
    UIControlEvents events = UIControlEventTouchDown | UIControlEventTouchDownRepeat;
    if (events & UIControlEventTouchDown){
        NSLog(@"UIControlEventTouchDown");
    }
    NSLog(@"enum test: %lu",events);
    return events;
}
main();

Struct

You can directly write the struct delaration in Script.

Example:

You can run the code by changing the content of ViewController1 in OCRunnerDemo.

// link NSLog
void NSLog(NSString *format, ...);
// CGPoint must be in front of CGRect
struct CGPoint { 
    CGFloat x;
    CGFloat y;
};
// CGSize must be in front of CGRect
struct CGSize { 
    CGFloat width;
    CGFloat height;
};
struct CGRect { 
    CGPoint origin;
    CGSize size; 
};
int main(){
    CGRect rect;
    rect.size.width = 1;
    rect.size.height = 2;
    rect.origin.x = 3;
    rect.origin.y = 4;
    NSLog(@"struct test: %@",[NSValue valueWithCGRect:rect]);
    return 0;
}
main();
Clone this wiki locally