320

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 there any option to create a category in Xcode 6?

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
Yalamandarao
  • 3,852
  • 3
  • 20
  • 27

8 Answers8

746

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.

Pavan
  • 17,840
  • 8
  • 59
  • 100
unom
  • 11,438
  • 4
  • 34
  • 54
  • 15
    This is so confusing. – pronebird Aug 20 '14 at 12:20
  • 6
    They just accommodated to adding Swift and hidden everything under Objective-C or I think that they did. I personally don't get why Swift was added, Objective-C is such a beautiful and expressive language once you get to know it... It's Objects on top of C... Perrrfect – unom Aug 27 '14 at 18:07
  • 8
    Heh, the description given for `Objective-C File` by XCode 6 is "An Empty Objective-C file.", rather than my description which would be, "one of the old, not at all empty, Objective-C files that you're used to", which threw me off fairly nicely! – Benjohn Sep 17 '14 at 10:49
  • @unmircea Thanks for posting such useful information. To Apple: This is stupid! – mbm29414 Sep 23 '14 at 20:25
  • 1
    **They didn't forget. They just moved it without telling anyone.** what is typical, considering the crappy level of Apple documentations in general and most frequently the total lack of documentation. – Duck Oct 01 '14 at 20:18
  • What are you missing on the Documentation level? It's true they always change things but the documentation is very good, although written in a style that is a little different from what other software companies. – unom Oct 03 '14 at 05:20
  • Thanks! I was stumped on this and could not find where they moved it. – SefTarbell Oct 24 '14 at 13:47
  • Stack Exchange should change theirs algorithms to bring these kind of answers to the top !! – Matthieu Riegler Jan 28 '15 at 19:49
  • This should be the preferred answer – alexzg Jan 29 '15 at 07:56
  • @unmircea, I don't remember now, but I did stumble upon public methods not documented too. – Iulian Onofrei Jan 25 '16 at 15:05
78

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.
Zorayr
  • 23,770
  • 8
  • 136
  • 129
37

Here's a visual demonstration:

creating a category file

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
19

Xcode6-Beta5 update

The interface has now changed and it's possible to add a Category directly from the New > File window.

See unmircea's answer.


I was surprised myself, and I guess because of Swift they forgot about good old 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-category and @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.

Community
  • 1
  • 1
mokagio
  • 16,391
  • 3
  • 51
  • 58
  • 2
    FYI: if you're on a later beta, just change Xcode-beta.app in the line to Xcode6-betaX.app where X is the beta number. – Alex the Ukrainian Aug 08 '14 at 18:49
  • 1
    **Wrong!** Check out the answer by *unmircea* below for the correct answer - it's not gone, it has just moved.. – Jay Aug 16 '14 at 06:02
12

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

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
6

Extending unmircea's fantastic answer re: how to create a custom category to implement a custom UIColor palette, you could create a category.

Once you've created your category (in this example, it's a category called ColorPalette of class UIColor), you'll have a header and an implementation file.

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

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.

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

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

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Jasper Jul 28 '15 at 14:29
3

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

NSFish
  • 354
  • 3
  • 10
  • Although this might be a working solution, it's probably a better idea to use the category support built into Xcode - this way, the templates will always be up to date. – Zorayr Dec 22 '14 at 22:31
1

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

B. Desai
  • 16,414
  • 5
  • 26
  • 47
Rohit Sisodia
  • 895
  • 12
  • 13