本文翻译自:How do I create a category in Xcode 6 or higher?
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. 我想在我的应用程序中使用Xcode 6在UIColor
上创建一个类别。但事实是在Xcode 6中没有Objective-C类别文件模板。
Is there any option to create a category in Xcode 6? 有没有在Xcode 6中创建类别的选项?
#1楼
参考:https://stackoom.com/question/1e3mv/如何在Xcode-或更高版本中创建类别
#2楼
There is no predefined template to create category in Xcode 6 beta(for time being),they may add this option later. 在Xcode 6 beta中没有预定义的模板来创建类别(暂时),他们可能会在以后添加此选项。 As a work around you can create a Cocoa Touch Class
(its not proper i know but no other way) named UIImage+Additions
(ClassName+CategoryName) and override its interface and implementation some thing like 作为一个解决方法你可以创建一个名为UIImage+Additions
(ClassName + CategoryName)的Cocoa Touch Class
(它不是我知道但不是其他方式)并覆盖它的接口和实现一些像
UIImage+Additions.h 的UIImage + Additions.h
#import <UIKit/UIKit.h>
@interface UIImage(Additions)
+(void)testMethod;
@end
UIImage+Additions.m 的UIImage + Additions.m
#import "UIImage+Additions.h"
@implementation UIImage (Additions)
+(void)testMethod
{
}
@end
Edit 编辑
This answer was written before finding a way of creating category in the Xcode 6 beta. 在找到在Xcode 6 beta中创建类别的方法之前,已经写了这个答案。 Check unmircea's answer for the right way of creating category 检查unmircea的答案,了解正确的创建类别的方法
#3楼
Xcode6-Beta5 update Xcode6-Beta5更新
The interface has now changed and it's possible to add a Category directly from the New > File window. 界面现在已更改,可以直接从“新建”>“文件”窗口添加“类别”。
See unmircea's answer . 请参阅unmircea的回答 。
I was surprised myself, and I guess because of Swift they forgot about good old Objective-C. 我很惊讶自己,我想因为斯威夫特他们忘了好老的Objective-C。
You have two options: 您有两种选择:
Create an Objective-C class with the category name, example
UIView+Powerups
, then manually change the interface to match the one of category. 使用类别名称创建一个Objective-C类,例如UIView+Powerups
,然后手动更改接口以匹配类别。 Note that the snippet for the category interface and implementation is still working, so that's extra easy: type@interface-category
and@implementation-category
. 请注意,类别接口和实现的代码段仍然有效,因此非常简单:键入@interface-category
和@implementation-category
。Import it from Xcode 5! 从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. 关闭并重新打开Xcode 6,您将在向导中找到新文件的“Objective-C Category”。
#4楼
你可以从旧版本的Xcode中复制你想要的模板,我为此制作了一个shell脚本: https : //github.com/cDigger/AddMissingTemplates
#5楼
They didn't forget. 他们没有忘记。 They just moved it without telling anyone. 他们只是移动它而不告诉任何人。
Click
File
->New
->File
单击File
- >New
- >File
Select
Objective-C file
underSources
iniOS
orMac OS
respectively and Click Next 分别在iOS
或Mac OS
Sources
下选择Objective-C file
,然后单击NextNow under
File Type:
choose eitherCategory
,Protocol
, orExtension
现在在File Type:
选择Category
,Protocol
或Extension
PS. PS。 Under File Name:
whatever you type here will be either the Category
, Protocol
, or Extension
Name. 在File Name:
您在此处键入的任何内容都将是Category
, Protocol
或Extension
名。
#6楼
To create CategoryBaseClass+CategoryName.m/.h : 要创建CategoryBaseClass + CategoryName.m / .h :
- File → New → File... or use ⌘N . 文件→新建→文件...或使用⌘N 。
- Select Objective-C File . 选择Objective-C文件 。
- Type in category name, select File Type: Category , and then select the base class. 键入类别名称,选择“ 文件类型:类别” ,然后选择基类。
- Complete the flow to create the category. 完成流程以创建类别。