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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24324021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:59:49  来源:igfitidea点击:

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

iosobjective-ccategoriesxcode6

提问by Yalamandarao

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

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

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

是否有任何选项可以在 Xcode 6 中创建类别?

回答by unom

They didn't forget. They just moved it without telling anyone.

他们没有忘记。他们只是在没有告诉任何人的情况下移动了它。

  1. Click File-> New-> File

  2. Select Objective-C fileunder Sourcesin iOSor Mac OSrespectively and Click Next

  3. Now under File Type:choose either Category, Protocol, or Extension

  1. 点击File-> New->File

  2. 分别选择in或Objective-C fileunder并单击 NextSourcesiOSMac OS

  3. 现在File Type:选择Category, Protocol, 或Extension

PS. Under File Name:whatever you type here will be either the Category, Protocol, or ExtensionName.

附注。在File Name:您在此处键入的任何内容下Category,将是Protocol、 或Extension名称。

回答by Zorayr

To create CategoryBaseClass+CategoryName.m/.h:

创建CategoryBaseClass+CategoryName.m/.h

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

enter image description here

在此处输入图片说明

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

enter image description here

在此处输入图片说明

  1. Complete the flow to create the category.
  1. 完成流程以创建类别。

回答by rob mayoff

Here's a visual demonstration:

这是一个视觉演示:

creating a category file

创建类别文件

回答by mokagio

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 forgotabout good old Objective-C.

我自己也很惊讶,我猜是因为 Swift 他们忘记了老式的 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-categoryand @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.

  1. 使用类别名称 example 创建一个 Objective-C 类UIView+Powerups,然后手动更改接口以匹配类别之一。请注意,类别接口和实现的代码段仍然有效,因此非常简单:键入@interface-category@implementation-category

  2. 从 Xcode 5 导入它!使用这个命令:

    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/
    

    关闭并重新打开 Xcode 6,您将在新文件的向导中找到“Objective-C Category”。

回答by Anil Varghese

There is no predefined template to create category in Xcode 6 beta(for time being),they may add this option later. 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

Xcode 6 beta中没有预定义的模板来创建类别(暂时),他们可能会在以后添加这个选项。作为一种解决方法,您可以创建一个Cocoa Touch Class(我知道这是不正确的,但没有其他方式)命名的UIImage+Additions(ClassName+CategoryName)并覆盖其接口和实现,例如

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. Check unmircea's answer for the right way of creating category

编辑
此答案是在找到一种在 Xcode 6 beta 中创建类别的方法之前编写的。检查 unmircea 的答案以了解创建类别的正确方法

回答by Adrian

Extending unmircea's fantastic answerre: how to create a custom category to implement a custom UIColorpalette, you could create a category.

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

Once you've created your category (in this example, it's a categorycalled ColorPaletteof classUIColor), you'll have a header and an implementation file.

一旦你创建你的类别(在这个例子中,它是一个ColorPaletteUIColor),你就会有一个标题和一个实现文件。

UIColor+ColorPalette.h

UIColor+ColorPalette.h

#import <UIKit/UIKit.h>

@interface UIColor (ColorPalette)

// Your custom colors

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

@end

UIColor+ColorPalette.m

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];
}

To use your custom color palette, just import the header into the class where you'd like to implement your custom colors:

要使用自定义调色板,只需将标题导入到您想要实现自定义颜色的类中:

#import "UIColor+ColorPalette.h"

and call the color as you would a standard color like redColor, greenColor, or blueColor.

并调用颜色你将一个标准色样redColorgreenColorblueColor

Here's a link to a slightly more in-depth discussionof creating a custom palette.

这是一个链接,指向关于创建自定义调色板的更深入的讨论

Additionally, here is a tool to help you select the custom color values

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

回答by NSFish

You could just copy the templates you want from an older version of Xcode, I made a shell script for this:https://github.com/cDigger/AddMissingTemplates

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

回答by Rohit Sisodia

You can create "extension" file like NSString+Helper:

您可以创建像 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 {


}

Done. enjoy coding

完毕。享受编码