阻止 Xcode 在新 headers 上创建 NS_ASSUME_NONNULL_BEGIN 和 NS_ASSUME_NONNULL_END

Prevent Xcode from creating NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END on new headers

我已经更新了 Xcode,自更新以来,我在每个新的 header 文件周围为每个新的 class 获取了 NS_ASSUME_NONNULL_BEGINNS_ASSUME_NONNULL_END 宏] 我创造的。

我知道它的作用,但我对它的可空性注释不感兴趣,当我尝试在我的应用程序中将某些属性设置为 nil 时它会导致不必要的警告(完全可以设置为nil).

如何防止 Xcode 在我每次创建新文件时都创建这些文件? (除了显而易见的:单独删除它们)

基于 this and this 文章,我能够创建自定义模板来摆脱 NS_ASSUME_NONNULL* 宏。

第一篇文章摘录:

Location

These user-defined templates are located in ~/Library/Developer/Xcode/Templates/File Template. If such folder is missing you can create it yourself and Xcode will be linked to it upon reopening. Also you can group them into subfolders inside that folder. For example, ~/Library/Developer/Xcode/Templates/File Template/Custom Templates and Xcode will render those groups properly when creating a new file.

Template Setup

Each template is a folder with .xctemplate extension. That folder contains file templates and resources that will be populated and added to a project and configuration TemplateInfo.plist.

All template settings and fields that user can fill in during template creation are defined in TemplateInfo.plist.

  • 创建路径如下:

    ~/Library/Developer/Xcode/Templates/File Templates/Source/My Awesome Template.xctemplate

  • 转到 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate
  • 复制 TemplateIcon.pngTemplateIcon@2x.pngTemplateInfo.plist 到我们的 My Awesome Template.xctemplate 文件夹。如果您在浏览器中不需要它们或使用您自己的图标,您可以省略复制图标。
  • 复制 NSObjectObjective-C and NSObjectSwift 文件夹以便能够创建 Obj C 和 Swift 文件。
  • 编辑 ___FILEBASENAME___.h___FILEBASENAME___.m___FILEBASENAME___.swift,例如放置您的宏或在我们的例子中删除周围的 NS_ASSUME_NONNULL*
  • 现在您可以在最顶部的文件创建浏览器中找到您的模板
  • 盈利!

我找到了一个最好的方法:

  1. 转到/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate/NSObjectObjective-C
  2. 复制___FILEBASENAME___.h到任何地方
  3. 打开并删除NS_ASSUME_NONNULL_BEGINNS_ASSUME_NONNULL_END
  4. 最后替换原文件并输入系统密码