Objective-c @ encode keyword

Source: Internet
Author: User

1. If you want to know the specific data type in nsnumber, the @ encode keyword will be used.

Use @ encode (Atype) to return the C string of this type, represented by const char. For example, @ encode (INT) returns I; @ encode (float) returns F.

Then, the const char * returned by the objctype method in nsvalue is okay with the comparison above. Use strcmp to compare the size by ASCII value.

int a = 10;NSNumber *number = [NSNumber numberWithInt:a];if (strcmp([number objCType], @encode(int)) == 0) {      int result = [number intValue];      NSLog(@"%d", result);}

2. The data type of objective-C, or even the meta type of a custom type, function, or method, can all use ASCII encoding.

        NSLog(@"int        : %s", @encode(int));        NSLog(@"float      : %s", @encode(float));        NSLog(@"float *    : %s", @encode(float*));        NSLog(@"char       : %s", @encode(char));        NSLog(@"char *     : %s", @encode(char *));        NSLog(@"BOOL       : %s", @encode(BOOL));        NSLog(@"void       : %s", @encode(void));        NSLog(@"void *     : %s", @encode(void *));                NSLog(@"NSObject * : %s", @encode(NSObject *));        NSLog(@"NSObject   : %s", @encode(NSObject));        NSLog(@"[NSObject] : %s", @encode(typeof([NSObject class])));        NSLog(@"NSError ** : %s", @encode(typeof(NSError **)));                int intArray[5] = {1, 2, 3, 4, 5};        NSLog(@"int[]      : %s", @encode(typeof(intArray)));                float floatArray[3] = {0.1f, 0.2f, 0.3f};        NSLog(@"float[]    : %s", @encode(typeof(floatArray)));                typedef struct _struct {            short a;            long long b;            unsigned long long c;        } Struct;        NSLog(@"struct     : %s", @encode(typeof(Struct)));

The result is:

int        : ifloat      : ffloat *    : ^fchar       : cchar *     : *BOOL       : cvoid       : vvoid *     : ^vNSObject * : @NSObject   : {NSObject=#}[NSObject] : #NSError ** : ^@int[]      : [5i]float[]    : [3f]struct     : {_struct=sqQ}

Reference: http://nshipster.com/type-encodings/

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.