Prevent Xcode from creating NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END on new headers

1.6k views Asked by At

I've updated Xcode and since updating I've been getting NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END macros around every new header file for every new class that I create.

I know what it does, but I'm not interested in nullability annotations it and it causes unnecessary warnings when I try to set some properties to nil in my app (which are perfectly okay to set to nil).

How do I prevent Xcode from creating these every time I create a new file? (other than the obvious: deleting them individually)

2

There are 2 answers

1
schmidt9 On BEST ANSWER

Based on this and this arcticles I was able to create custom template to get rid of NS_ASSUME_NONNULL* macros.

Excerpt from the first arcticle:

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.

  • create path as follows:

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

  • go to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate
  • copy TemplateIcon.png, [email protected] and TemplateInfo.plist to our My Awesome Template.xctemplate folder. You can omit copying icons if you do not need them in browser or use your own.
  • copy NSObjectObjective-C and NSObjectSwift folders to be able to create both Obj C and Swift files.
  • edit ___FILEBASENAME___.h, ___FILEBASENAME___.m, ___FILEBASENAME___.swift, for example put your macros or in our case delete surrounding NS_ASSUME_NONNULL* macros
  • now you are able to find your template in file creation browser on the very top
  • Profit!
0
k.machine On

I found a best way:

  1. go to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source/Cocoa Touch Class.xctemplate/NSObjectObjective-C
  2. copy ___FILEBASENAME___.h to anywhere
  3. open and delete NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END
  4. Finally replace the original file and enter System password