我想用Xcode 6在我的应用程序中创建一个UIColor类别。但问题是,在Xcode 6中没有Objective-C分类文件模板。

在Xcode 6中有创建类别的选项吗?


在Xcode 6 beta(暂时)中没有预定义的模板来创建分类,他们可能会在以后添加这个选项。作为一个工作周围,你可以创建一个可可触摸类(它不是适当的我知道,但没有其他方式)命名为UIImage+添加(ClassName+CategoryName)和覆盖它的接口和实现一些东西

用户界面图像+ Additions.h

#import <UIKit/UIKit.h>

@interface UIImage(Additions)

+(void)testMethod;

@end 

用户界面图像+ Additions.m

#import "UIImage+Additions.h"

@implementation UIImage (Additions)

+(void)testMethod
{

}

@end

编辑 这个答案是在Xcode 6 beta中找到创建类别的方法之前写的。查看unmircea的答案,了解创建类别的正确方法

Xcode6-Beta5更新

界面现在已经改变,可以直接从新建>文件窗口添加一个类别。

请看unmircea的回答。


我自己也很惊讶,我猜是因为Swift,他们忘记了好的老Objective-C。

你有两个选择:

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

你可以从旧版本的Xcode中复制你想要的模板,我为此做了一个shell脚本:https://github.com/cDigger/AddMissingTemplates

他们没有忘记。他们没告诉任何人就把它搬走了。

单击“文件->新建->文件” 在iOS或Mac OS下分别选择“Sources”下的“Objective-C”文件,单击“Next” 现在在文件类型下:选择类别,协议或扩展

PS.在文件名称下:无论你在这里输入什么,都将是类别,协议或扩展名。

创建CategoryBaseClass+CategoryName.m/.h:

文件→新建→文件…或使用⌘N。 选择Objective-C File。

输入类别名称,选择文件类型:类别,然后选择基类。

完成流以创建类别。

扩展unmircea的精彩回答re:如何创建一个自定义类别来实现自定义UIColor调色板,您可以创建一个类别。

一旦您创建了您的类别(在本例中,它是一个名为类UIColor的ColorPalette的类别),您将有一个头文件和一个实现文件。

UIColor + ColorPalette。h

#import <UIKit/UIKit.h>

@interface UIColor (ColorPalette)

// Your custom colors

+ (UIColor *) customRedButtonColor;
+ (UIColor *) customGreenButtonColor;

@end

UIColor + ColorPalette。m

#import "UIColor+ColorPalette.h"

@implementation UIColor (ColorPalette)

// Button Colors

+ (UIColor *) customRedButtonColor {
    return [UIColor colorWithRed:178.0/255.0 green:25.0/255.0 blue:0.0/255.0 alpha:1.0];
}

+ (UIColor *) customGreenButtonColor {
    return [UIColor colorWithRed:20.0/255.0 green:158.0/255.0 blue:96.0/255.0 alpha:1.0];
}

要使用自定义调色板,只需将头文件导入到你想实现自定义颜色的类中:

#import "UIColor+ColorPalette.h"

并将该颜色称为标准颜色,如redColor, greenColor或blueColor。

这里有一个链接,指向创建自定义调色板的稍微深入的讨论。

此外,这里有一个工具可以帮助您选择自定义颜色值

下面是一个视觉演示:

你可以创建“扩展”文件,如NSString+Helper:

1: File → New → File... or use ⌘N.

2: Name NSString+Helper (For example)

3: create the file

4: Remove the code from file

5: add 

extension NSString {


}

完成了。享受编码