How do I create a category in Xcode 6 or higher?

2019-01-05 07:48发布

I want to create a category on UIColor in my app using Xcode 6. But the thing is that in Xcode 6 there is no Objective-C category file template.

Is there any option to create a category in Xcode 6?

8条回答
等我变得足够好
2楼-- · 2019-01-05 08:16

Xcode6-Beta5 update

The interface has now changed and it's possible to add a Category directly from the New > File window.

See unmircea's answer.


I was surprised myself, and I guess because of Swift they forgot about good old Objective-C.

You have two options:

  1. Create an Objective-C class with the category name, example UIView+Powerups, then manually change the interface to match the one of category. Note that the snippet for the category interface and implementation is still working, so that's extra easy: type @interface-category and @implementation-category.

  2. Import it from Xcode 5! Use this command:

    cp -r /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Cocoa\ Touch/Objective-C\ category.xctemplate /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File\ Templates/Source/
    

    Close and reopen Xcode 6 and you'll find "Objective-C Category" in the wizard for the new file.

查看更多
爷、活的狠高调
3楼-- · 2019-01-05 08:19

To create CategoryBaseClass+CategoryName.m/.h:

  1. File → New → File... or use ⌘N.
  2. Select Objective-C File.

enter image description here

  1. Type in category name, select File Type: Category, and then select the base class.

enter image description here

  1. Complete the flow to create the category.
查看更多
登录 后发表回答