如何在Xcode 6或更高版本中创建类别?

本文翻译自: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: 您有两种选择:

  1. 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

  2. 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. 他们只是移动它而不告诉任何人。

  1. Click File -> New -> File 单击File - > New - > File

  2. Select Objective-C file under Sources in iOS or Mac OS respectively and Click Next 分别在iOSMac OS Sources下选择Objective-C file ,然后单击Next

  3. Now under File Type: choose either Category , Protocol , or Extension 现在在File Type:选择CategoryProtocolExtension

PS. PS。 Under File Name: whatever you type here will be either the Category , Protocol , or Extension Name. File Name:您在此处键入的任何内容都将是CategoryProtocolExtension名。


#6楼

To create CategoryBaseClass+CategoryName.m/.h : 要创建CategoryBaseClass + CategoryName.m / .h

  1. File → New → File... or use ⌘N . 文件→新建→文件...或使用⌘N
  2. Select Objective-C File . 选择Objective-C文件

在此输入图像描述

  1. Type in category name, select File Type: Category , and then select the base class. 键入类别名称,选择“ 文件类型:类别” ,然后选择基类。

在此输入图像描述

  1. Complete the flow to create the category. 完成流程以创建类别。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值