开发小技巧

Objective-C分析之一

2017-10-12  本文已影响10人  MrSong

Objective-C分析,再深入了解一下,发现还是有很多不懂得地方,有些方法属性从来都没有用过。官方文档确实很详细。

基础

NSObject

初始化类

create,copy,dealloc对象

    - (instancetype)init {
        self = [super init];
        if (self) {
            // Initialize self
        }
        return self;
    }

方法

    if ([self respondsToSelector:@selector(test)]) {
        IMP imp = [self methodForSelector:@selector(test)];
       
        NSLog(@"对象方法imp:%x",imp);
    }
    
    if ([AppDelegate respondsToSelector:@selector(test1)]) {
        IMP imp = [AppDelegate methodForSelector:@selector(test1)];
        
        NSLog(@"类方法imp:%x",imp);
    }

- (void)test {
    NSLog(@"test");
}

+ (void)test1 {
    NSLog(@"test1");
}
    if ([AppDelegate instancesRespondToSelector:@selector(test)]) {
        IMP imp = [AppDelegate instanceMethodForSelector:@selector(test)];
        
        NSLog(@"对象方法imp:%x",imp);
    }
    NSMethodSignature *methodSignature = [AppDelegate instanceMethodSignatureForSelector:@selector(test)];
    NSLog(@"参数%zd个,参数总长度:%zd,返回类型:%s,返回长度:%zd",methodSignature.numberOfArguments,methodSignature.frameLength,methodSignature.methodReturnType,methodSignature.methodReturnLength);
    // 参数2个,参数总长度:224,返回类型:v,返回长度:0
    
    methodSignature = [AppDelegate instanceMethodSignatureForSelector:@selector(testWith:number:)];
    NSLog(@"参数%zd个,参数总长度:%zd,返回类型:%s,返回长度:%zd",methodSignature.numberOfArguments,methodSignature.frameLength,methodSignature.methodReturnType,methodSignature.methodReturnLength);
    // 参数4个,参数总长度:224,返回类型:@,返回长度:8
    if([methodSignature isOneway]) {
        NSLog(@"异步");
    }

- (void)test {
    NSLog(@"test");
}
- (NSString *)testWith:(NSString *)one number:(NSInteger )num {
    NSLog(@"testWith:%@ number:%zd",one,num);
    return @"";
}

/*  NSMethodSignature.h
    Copyright (c) 1994-2016, Apple Inc. All rights reserved.
*/

#import <Foundation/NSObject.h>

NS_ASSUME_NONNULL_BEGIN

NS_SWIFT_UNAVAILABLE("NSInvocation and related APIs not available")
@interface NSMethodSignature : NSObject {
@private
    void *_private;
    void *_reserved[6];
}
/// 创建对象
+ (nullable NSMethodSignature *)signatureWithObjCTypes:(const char *)types;
/// 参数个数,至少两个,一个self,一个_cmd
@property (readonly) NSUInteger numberOfArguments;
/// 获取参数类型,self -> id ,_cmd -> IMP
- (const char *)getArgumentTypeAtIndex:(NSUInteger)idx NS_RETURNS_INNER_POINTER;
/// 参数总长度,在内存堆栈上的大小,和硬件架构有关
@property (readonly) NSUInteger frameLength;
// 调用着是否是异步的
- (BOOL)isOneway;
/// 返回值类型
@property (readonly) const char *methodReturnType NS_RETURNS_INNER_POINTER;
/// 返回值长度,内存堆栈上的长度
@property (readonly) NSUInteger methodReturnLength;

@end

NS_ASSUME_NONNULL_END
    methodSignature = [self methodSignatureForSelector:@selector(testWith:number:)];
    NSLog(@"对象方法参数%zd个,参数总长度:%zd,返回类型:%s,返回长度:%zd",methodSignature.numberOfArguments,methodSignature.frameLength,methodSignature.methodReturnType,methodSignature.methodReturnLength);
    // 参数2个,参数总长度:224,返回类型:v,返回长度:0
    if([methodSignature isOneway]) {
        NSLog(@"异步");
    }
    
    methodSignature = [AppDelegate methodSignatureForSelector:@selector(test1)];
    NSLog(@"类方法参数%zd个,参数总长度:%zd,返回类型:%s,返回长度:%zd",methodSignature.numberOfArguments,methodSignature.frameLength,methodSignature.methodReturnType,methodSignature.methodReturnLength);
    // 参数2个,参数总长度:224,返回类型:v,返回长度:0
    if([methodSignature isOneway]) {
        NSLog(@"异步");
    }

- (NSString *)testWith:(NSString *)one number:(NSInteger )num {
    NSLog(@"testWith:%@ number:%zd",one,num);
    return @"";
}

+ (void)test1 {
    NSLog(@"test1");
}

/***********    Discardable Content     ***********/

@protocol NSDiscardableContent
@required
- (BOOL)beginContentAccess; // count + 1
- (void)endContentAccess;  // count - 1
- (void)discardContentIfPossible; // 丢弃内存数据
- (BOOL)isContentDiscarded; // 是否丢弃内存数据
@end

@interface NSObject (NSDiscardableContentProxy)
// 返回遵守NSDiscardableContent协议的代理
@property (readonly, retain) id autoContentAccessingProxy NS_AVAILABLE(10_6, 4_0);
@end

接下来继续往下看

上一篇下一篇

猜你喜欢

热点阅读