@encode

作者: 码农二哥 | 来源:发表于2017-07-29 17:24 被阅读15次

    苹果官方文档https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

    demo

    - (void)testEncodeChar
    {
        NSLog(@"int              : %s", @encode(int));
        NSLog(@"int *            : %s", @encode(int*));
        NSLog(@"unsigned 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(@"NSString *       : %s", @encode(NSString *));
        NSLog(@"NSObject *       : %s", @encode(NSObject *));
        NSLog(@"NSArray[]        : %s", @encode(typeof(@[@"str"])));
        NSLog(@"id               : %s", @encode(id));
        NSLog(@"NSObject         : %s", @encode(NSObject));
        NSLog(@"[NSObject class] : %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)));
    }
    
    2018-01-14 14:26:49.855018+0800 WiOSDemo[82490:45356764] int              : i
    2018-01-14 14:26:49.855146+0800 WiOSDemo[82490:45356764] int *            : ^i
    2018-01-14 14:26:49.855231+0800 WiOSDemo[82490:45356764] unsigned int     : i
    2018-01-14 14:26:49.855344+0800 WiOSDemo[82490:45356764] float            : f
    2018-01-14 14:26:49.855451+0800 WiOSDemo[82490:45356764] float *          : ^f
    2018-01-14 14:26:49.855545+0800 WiOSDemo[82490:45356764] char             : c
    2018-01-14 14:26:49.855659+0800 WiOSDemo[82490:45356764] char *           : *
    2018-01-14 14:26:49.855739+0800 WiOSDemo[82490:45356764] BOOL             : B
    2018-01-14 14:26:49.855809+0800 WiOSDemo[82490:45356764] void             : v
    2018-01-14 14:26:49.855906+0800 WiOSDemo[82490:45356764] void *           : ^v
    2018-01-14 14:26:49.855984+0800 WiOSDemo[82490:45356764] NSString *       : @
    2018-01-14 14:26:49.856064+0800 WiOSDemo[82490:45356764] NSObject *       : @
    2018-01-14 14:26:49.856159+0800 WiOSDemo[82490:45356764] NSArray[]        : @
    2018-01-14 14:26:49.856288+0800 WiOSDemo[82490:45356764] id               : @
    2018-01-14 14:26:49.856478+0800 WiOSDemo[82490:45356764] NSObject         : {NSObject=#}
    2018-01-14 14:26:49.856661+0800 WiOSDemo[82490:45356764] [NSObject class] : #
    2018-01-14 14:26:49.856841+0800 WiOSDemo[82490:45356764] NSError **       : ^@
    2018-01-14 14:26:49.856971+0800 WiOSDemo[82490:45356764] int[]            : [5i]
    2018-01-14 14:26:49.857150+0800 WiOSDemo[82490:45356764] float[]          : [3f]
    2018-01-14 14:26:49.857298+0800 WiOSDemo[82490:45356764] struct       : {_struct=sqQ}
    

    问题一

    // 通过NSInvocation,怎么调用下面这个函数?
    - (NSString *)testParamForcharValue:(char)charValue//c
                      unsignedCharValue:(unsigned char)unsignedCharValue//C
                             shortValue:(short)shortValue//s
                     unsignedShortValue:(unsigned short)unsignedShortValue//S
                               intValue:(int)intValue//i
                       unsignedIntValue:(unsigned int)unsignedIntValue//I
                              longValue:(long)longValue//l
                      unsignedLongValue:(unsigned long)unsignedLongValue//L
                          longLongValue:(long long)longLongValue//q
                  unsignedlongLongValue:(unsigned long long)unsignedlongLongValue//Q
                             floatValue:(float)floatValue//f
                            doubleValue:(double)doubleValue//d
                              boolValue:(BOOL)boolValue//B
                          selectorValue:(int(^)(int, int))selectorValue//:
                              rectValue:(CGRect)rectValue//{name=type...}
                              sizeValue:(CGSize)sizeValue//{name=type...}
                            charPointer:(char *)charPointer//*
                                  point:(int *)pint//^
                             classValue:(Class)classValue//#
                          nsstringValue:(NSString *)nsstringValue//@
                           nsarrayValue:(NSArray *)nsarrayValue//@
                                idValue:(id)idValue//@;
                            rectPointer:(CGRect *)rectPointer
    {
        NSLog(@"2 + 3 = %d", selectorValue(2, 3));
        pint += 1;
        *rectPointer = CGRectMake(100, 1000, 10000, 100000);
        return @"return string";
    }
    

    参考连接:https://www.jianshu.com/p/a4d0d3346afe

    问题二

    // 通过NSInvocation,怎么调用一个任意参数和任意返回值的函数?
    

    相关文章

      网友评论

        本文标题:@encode

        本文链接:https://www.haomeiwen.com/subject/lgjclxtx.html