How do I create a category in Xcode 6 or higher?
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.
How to create a category in Xcode 7.2?
- File > New > File…
- iOS > Source
- Objective-C File
- Next
- File Type: Category
Example:
How to create swift class for category?
In Swift, you can use Extensions
to add new functionality to existing classes, structs and enumeration types.
They differ from Objective-C categories in a few ways, mainly:
- They aren't named
- You don't need to import an Extension explicitly. If you define an extension to add new functionality to an existing type, the new functionality will be available on all existing instances of that type, even if they were created before the extension was defined.
- As stated above, they work not only with classes, but with other types as well.
As it stands today, Extensions can:
- Add computed properties and computed static properties
- Define instance methods and type methods
- Provide new initializers
- Define subscripts
- Define and use new nested types
- Make an existing type conform to a protocol
The basic syntax to declare an extension is as follows:
extension SomeType {
// new functionality to add to SomeType goes here
}
Check Apple's documentation for more information on how to use Extensions in Swift.
How to create a new templates category on Xcode 4 and use my own file templates there?
This blog post by borealkiss has detailed instructions. For starters:
If you add your own templates, the place you should use is under the following path:
~/Library/Developer/Xcode/Templates/
The problem is that it is quite hard to create a custom template.
For example, to copy and paste the
built-in template to your place will
not show up the template on Xcode 4
until you manually change its
identifier in TemplateInfo.plist.
How to create a category with a property for NSNumber objects
My first attempt to solve this tried to manage the association manually by exception when a tagged pointer object was detected. This works, but was not a practical solution, since you loose all the benefits of ARC for the associated value (they are not cleaned up when the tagged pointer object is no longer in use).
Instead, a viable solution makes the property readonly, then uses a separate accessor to transform the tagged pointer object into a normal object before applying the associated object:
@interface NSNumber (defaultNumberFormat)
@property (nonatomic,strong,readonly) NSNumberFormatter *defaultNumberFormat;
- (NSNumber *)applyDefaultNumberFormat:(NSNumberFormatter *)format;
@end
@implementation NSNumber (defaultNumberFormat)
@dynamic defaultNumberFormat;
- (NSNumber *)applyDefaultNumberFormat:(NSNumberFormatter *)format {
NSNumber *newNumber = self;
#ifdef WORKAROUND_IOS_TAGGED_POINTER_ISSUE
unsigned long ptrValue = (unsigned long)self;
if (ptrValue & 0x1) {
// We have a non-aligned pointer - ie a tagged short-cut object stored inside the pointer. objc_setAssociatedObject() is broken for these object types (it will cause memory access faults).
// Transform ourselves into a non-tagged object.
newNumber = [NSDecimalNumber decimalNumberWithDecimal:[self decimalValue]];
ptrValue = (unsigned long)newNumber;
if (ptrValue & 0x1 || ![newNumber isKindOfClass:[NSNumber class]]) {
NSLog(@"Failed to create a non-tagged NSNumber for number: %@ - hence default number format not set", [self description]);
return self;
}
}
#endif
[newNumber willChangeValueForKey:@"defaultNumberFormat"];
objc_setAssociatedObject(newNumber, @selector(defaultNumberFormat), format, OBJC_ASSOCIATION_COPY_NONATOMIC);
[newNumber didChangeValueForKey:@"defaultNumberFormat"];
return newNumber;
}
- (NSNumberFormatter *)defaultNumberFormat {
#ifdef WORKAROUND_IOS_TAGGED_POINTER_ISSUE
unsigned long ptrValue = (unsigned long)self;
if (ptrValue & 0x1)
return nil;
#endif
return objc_getAssociatedObject(self, @selector(defaultNumberFormat));
}
@end
Why Xcode allows create duplicate Objective-C Category?
Xcode and Objective-C do allow you to create duplicate class files. What it doesn't allow you to do is create duplicate classes. In Objective-C, it is traditional to name the file with the same name as the class, but this is neither universal, nor enforced.
The question I think you're asking is why does Objective-C allow multiple categories with the same name (regardless of what file they're in). The reason is because Objective-C doesn't really care very much what the name of a category is. Originally categories were a way to organize large classes into multiple files (which is why they're called "categories" rather than "extensions"). Eventually they came to be used to add new methods to existing classes. At no point along the way has the compiler really bothered very much with the category names (the part in parentheses). They're generally treated as a comment.
I wouldn't assume there was a specific moment when the language designers decided "it should be legal to have multiple categories with the same name." There's just never been checked for or prevented.
The more interesting and important collision is that two categories can add the same method name. This isn't prevented by the compiler but is actually a serious problem. It's not defined which method will execute and the compiler won't tell you you've made this mistake. This is why it's important to prefix category method names to prevent collisions. It's a good idea to prefix category names, too, for consistency.
iOS - How to create Category with private methods
If calculateStuff
is a private method that is only used in the implementation block of
the MKPolygon (MKPolygonStuff)
category then you need not declare that method in an interface at all.
Just remove @interface MKPolygon() ... @end
from "MKPolygon+MKPolygonStuff.m".
With current Xcode/clang versions, you even need not forward declare a method that is defined and used inside an implementation block.
Related Topics
In Swift, How to Avoid Both Optionals and Nil Object References
How to Create a Circular Progress Indicator for a Count Down Timer
In Swift, Array [String] Slicing Return Type Doesn't Seem to Be [String]
iOS 7 Sprite Kit Freeing Up Memory
Select Multiple Images (Uiimagepickercontroller or Photos.App Share Ui)
Why Do We Use Use_Frameworks! in Cocoapods
Drop-Down List in Uitableview in iOS
Sorting Nsarray of Dictionaries by Value of a Key in the Dictionaries
Uploading Image with Afnetworking 2.0
iPhone Upload Multipart File Using Afnetworking
Ios: Keep an App Running Like a Service
Framework Not Found Googletoolboxformac
Swift 3.0 Error: Escaping Closures Can Only Capture Inout Parameters Explicitly by Value
How to Display .Svg Image Using Swift
Enable Copy and Paste on Uitextfield Without Making It Editable