How do I create a category in Xcode 6 or higher?
Solution 1
They didn't forget. They just moved it without telling anyone.
Click
File
->New
->File
Select
Objective-C file
underSources
iniOS
orMac OS
respectively and Click NextNow under
File Type:
choose eitherCategory
,Protocol
, orExtension
PS. Under File Name:
whatever you type here will be either the Category
, Protocol
, or Extension
Name.
Solution 2
To create CategoryBaseClass+CategoryName.m/.h:
- File → New → File... or use ⌘N.
- Select Objective-C File.
- Type in category name, select File Type: Category, and then select the base class.
- Complete the flow to create the category.
Solution 3
Here's a visual demonstration:
Solution 4
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:
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.
Solution 5
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
Yalamandarao
IOS & Xamarin Forms Application Developer. FB Page : https://www.facebook.com/IPhoneSdkForums/?ref=bookmarks
Updated on July 08, 2022Comments
-
Yalamandarao almost 2 years
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?
-
Yalamandarao almost 10 yearsApple forgot to add Objective-c Category, Objective-c class extension, Objective-c Protocols templates....
-
David Pettigrew almost 10 yearsI created a bug report #17627118 if you want to duplicate it.
-
Alex the Ukrainian almost 10 yearsFYI: 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.
-
Jay almost 10 yearsWrong! Check out the answer by unmircea below for the correct answer - it's not gone, it has just moved..
-
pronebird over 9 yearsThis is so confusing.
-
unom over 9 yearsThey 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
-
Benjohn over 9 yearsHeh, 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! -
mbm29414 over 9 years@unmircea Thanks for posting such useful information. To Apple: This is stupid!
-
Duck over 9 yearsThey 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.
-
unom over 9 yearsWhat 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.
-
SefTarbell over 9 yearsThanks! I was stumped on this and could not find where they moved it.
-
Zorayr over 9 yearsAlthough 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 over 9 yearsNo need for the workaround now since final version of XCode 6 has category support built-in.
-
Anil Varghese over 9 years@Zorayr It was answered before the final Xcode 6 version and the work around is not wrong also. Don't simply down vote.
-
Matthieu Riegler over 9 yearsStack Exchange should change theirs algorithms to bring these kind of answers to the top !!
-
alexzg over 9 yearsThis should be the preferred answer
-
Jasper almost 9 yearsWhilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
-
Iulian Onofrei over 8 years@unmircea, I don't remember now, but I did stumble upon public methods not documented too.