小知识积累

1. 宏

NS_VALID_UNTIL_END_OF_SCOPE

// 该宏表明存储在某些局部变量(obj)中的值,在优化时不应该被编译器强制释放
NSObject *obj NS_VALID_UNTIL_END_OF_SCOPE;

#ifdef ... #else ... #endif/#if ... #else ... #endif在函数中的使用,效果同API_AVAILABLE(ios(10.0))

/**************  #ifdef __IPHONE_9_0  ***************/
#ifdef __IPHONE_9_0
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
#else
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
#endif
{
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.f)
        return UIInterfaceOrientationMaskPortrait;
    else
        return UIInterfaceOrientationMaskAll;
}

/**************  API_AVAILABLE  ***************/
// 属性中使用,只在iOS 10以上版本中编译
@property(nonatomic, assign) NSObject *obj API_AVAILABLE(ios(10.0));

// 函数中使用
// 声明
- (UNUserNotificationCenter *)center API_AVAILABLE(ios(10.0));
// 实现
- (UNUserNotificationCenter *)center API_AVAILABLE(ios(10.0)){
   // Other code...
}

NS_UNAVAILABLE 在子类中使用和以禁用父类API

@interface TLObject : NSObject
// 指定唯一的对象创建方法
+ (instancetype)defaultObject;

// 通过NS_UNAVAILABLE禁用父类API
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

@end

NS_DESIGNATED_INITIALIZER 指定对象的初始化方法可配合NS_UNAVAILABLE使用

@interface TLObject : NSObject
// 指定对象的初始化方法, 必须是init开始的对象方法
- (instancetype)initWithTitle:(nullable NSString *)title NS_DESIGNATED_INITIALIZER;

// 通过NS_UNAVAILABLE禁用父类API
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end


@implementation TLObject

- (instancetype)initWithTitle:(NSString *)title {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    // Other Config...
    
    return self;
}

@end

NS_REQUIRES_SUPER 子类重写某方法时必须调用父类的该方法

// 父类中方法声明
- (void)prepare NS_REQUIRES_SUPER;

// 子类中方法重写
- (void)prepare {
   // 必须调用父类的该方法
   [super prepare];
   // ...
}

定义全局的弱引用self

#define TLWeakSelf __weak typeof(self) this = self;



2. 终端命令

将Objective-C代码转换为C\C++代码

// 在终端进入要转换文件的目录,输入下面命令。`clang  -arch  arm64` :  架构
xcrun  -sdk  iphoneos  clang  -arch  arm64  -rewrite-objc  OC源文件  -o  输出的CPP文件
// 例如:
xcrun  -sdk  iphoneos  clang  -arch  arm64  -rewrite-objc  main.m  -o  main_arm64.cpp

// 在使用clang转换OC为C++代码时,可能会遇到以下问题
// __weak 类型只能通过运行时转换
cannot create __weak reference in file using manual reference
// 解决方案:支持ARC、指定运行时系统版本,比如
pxcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-11.0.0 main.m

生成LLVM中间代码(.ll文件)

// 以main.m文件为例:
// cd 进入main.m文件所在目录,输入命令
clang -emit-llvm -S main.m



3. Xcode 特性

  1. Xcode 10报错
error: Multiple commands produce '...省略.../Debug-iphonesimulator/项目.app':
1) Target '项目' has create directory command with output '...省略.../Products/Debug-iphonesimulator/项目.app'
2) That command depends on command in Target '项目': script phase “[CP] Copy Pods Resources”
error.png
  • 解决方案(由于篇幅拓展,所以改为单独成文)
    请前往:具体的解决方案

  1. Xcode 自定义代码块 的导入和导出
    1. 目录:
~/library/developer/xcode/userdata/codeSnippets
    1. 导出: 进入上面的目录,直接拷贝codeSnippets文件夹或其子文件即可
    1. 导入:进入上面的目录,将上面导出的文件合并到codeSnippets文件夹,如果userdata文件夹目录下没有找到codeSnippets,则直接创建即可
    1. 使用:重启Xcode即可

待续...

你可能感兴趣的:(小知识积累)