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

后端 未结 8 1763
太阳男子
太阳男子 2020-11-27 09:53

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 the

相关标签:
8条回答
  • 2020-11-27 09:56

    Here's a visual demonstration:

    0 讨论(0)
  • 2020-11-27 09:59

    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

    0 讨论(0)
  • 2020-11-27 10:03

    You can create "extension" file like 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

    0 讨论(0)
  • 2020-11-27 10:04

    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

    UIImage+Additions.h

    #import <UIKit/UIKit.h>
    
    @interface UIImage(Additions)
    
    +(void)testMethod;
    
    @end 
    

    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

    0 讨论(0)
  • 2020-11-27 10:05

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

    1. Click File -> New -> File

    2. Select Objective-C file under Sources in iOS or Mac OS respectively and Click Next

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

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

    0 讨论(0)
  • 2020-11-27 10:15

    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.
    0 讨论(0)
提交回复
热议问题