Tutorial by Examples

Interface and implementation of a simple category on NSArray, named Filter, with a single method that filters numbers. It is good practice to add a prefix (PF) to the method to ensure we don't overwrite any future NSArray methods. @interface NSArray (PFFilter) - (NSArray *)pf_filterSmaller:(dou...
Header file UIColor+XYZPalette.h: @interface UIColor (XYZPalette) +(UIColor *)xyz_indigoColor; @end and implementation UIColor+XYZPalette.m: @implementation UIColor (XYZPalette) +(UIColor *)xyz_indigoColor { return [UIColor colorWithRed:75/255.0f green:0/255.0f blue:130/255.0f al...
Properties can be added with categories using associated objects, a feature of the Objective-C runtime. Note that the property declaration of retain, nonatomic matches the last argument to objc_setAssociatedObject. See Attach object to another existing object for explanations. #import <objc/run...
You can add protocols to standard classes to extends their functionality: @protocol EncodableToString <NSObject> - (NSString *)toString; @end @interface NSDictionary (XYZExtended) <EncodableToString> @end @implementation NSDictionary (XYZExtended) - (NSString *)toString { ...
Categories provide the ability to add some extra functionality to an object without subclassing or changing the actual object. For example we want to set some custom fonts. Let's create a category that add functionality to UIFont class. Open your XCode project, click on File -> New -> File a...

Page 1 of 1