How to Create a Category in Xcode 6 or Higher

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

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.

How to create a category in Xcode 7.2?

  1. File > New > File…
  2. iOS > Source
  3. Objective-C File
  4. Next
  5. File Type: Category

Example:

creating a category file

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



Leave a reply



Submit