ios developer tiny share-20160919
来源:互联网 发布:淘宝bt种子 编辑:程序博客网 时间:2024/06/05 03:28
本节讲Objective-C中使用category,来给现有类增加新的方法。
Categories Add Methods to Existing Classes
If you need to add a method to an existing class, perhaps to add functionality to make it easier to do something in your own application, the easiest way is to use a category.
The syntax to declare a category uses the @interface keyword, just like a standard Objective-C class description, but does not indicate any inheritance from a subclass. Instead, it specifies the name of the category in parentheses, like this:
@interface ClassName (CategoryName) @end
A category can be declared for any class, even if you don’t have the original implementation source code (such as for standard Cocoa or Cocoa Touch classes). Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class. At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.
Consider the XYZPerson class from the previous chapters, which has properties for a person’s first and last name. If you’re writing a record-keeping application, you may find that you frequently need to display a list of people by last name, like this:
Appleseed, JohnDoe, JaneSmith, BobWarwick, Kate
Rather than having to write code to generate a suitable lastName, firstName string each time you wanted to display it, you could add a category to the XYZPerson class, like this:
#import "XYZPerson.h" @interface XYZPerson (XYZPersonNameDisplayAdditions)- (NSString *)lastNameFirstNameString;@end
In this example, the XYZPersonNameDisplayAdditions category declares one additional method to return the necessary string.
A category is usually declared in a separate header file and implemented in a separate source code file. In the case of XYZPerson, you might declare the category in a header file called XYZPerson+XYZPersonNameDisplayAdditions.h.
Even though any methods added by a category are available to all instances of the class and its subclasses, you’ll need to import the category header file in any source code file where you wish to use the additional methods, otherwise you’ll run into compiler warnings and errors.
The category implementation might look like this:
#import "XYZPerson+XYZPersonNameDisplayAdditions.h" @implementation XYZPerson (XYZPersonNameDisplayAdditions)- (NSString *)lastNameFirstNameString { return [NSString stringWithFormat:@"%@, %@", self.lastName, self.firstName];}@end
Once you’ve declared a category and implemented the methods, you can use those methods from any instance of the class, as if they were part of the original class interface:
#import "XYZPerson+XYZPersonNameDisplayAdditions.h"@implementation SomeObject- (void)someMethod { XYZPerson *person = [[XYZPerson alloc] initWithFirstName:@"John" lastName:@"Doe"]; XYZShoutingPerson *shoutingPerson = [[XYZShoutingPerson alloc] initWithFirstName:@"Monica" lastName:@"Robinson"]; NSLog(@"The two people are %@ and %@", [person lastNameFirstNameString], [shoutingPerson lastNameFirstNameString]);}@end
As well as just adding methods to existing classes, you can also use categories to split the implementation of a complex class across multiple source code files. You might, for example, put the drawing code for a custom user interface element in a separate file to the rest of the implementation if the geometrical calculations, colors, and gradients, etc, are particularly complicated. Alternatively, you could provide different implementations for the category methods, depending on whether you were writing an app for OS X or iOS.
Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.
The only way to add a traditional property—backed by a new instance variable—to an existing class is to use a class extension, as described inClass Extensions Extend the Internal Implementation.
Note: Cocoa and Cocoa Touch include a variety of categories for some of the primary framework classes.
The string-drawing functionality mentioned in the introduction to this chapter is in fact already provided for NSString by the NSStringDrawing category for OS X, which includes the drawAtPoint:withAttributes: and drawInRect:withAttributes: methods. For iOS, the UIStringDrawing category includes methods such as drawAtPoint:withFont: and drawInRect:withFont:.
- ios developer tiny share-20160919
- ios developer tiny share-20160628
- ios developer tiny share-20160629
- ios developer tiny share-20160630
- ios developer tiny share-20160701
- ios developer tiny share-20160704
- ios developer tiny share-20160705
- ios developer tiny share-20160706
- ios developer tiny share-20160707
- ios developer tiny share-20160708
- ios developer tiny share-20160711
- ios developer tiny share-20160712
- ios developer tiny share-20160713
- ios developer tiny share-20160714
- ios developer tiny share-20160715
- ios developer tiny share-20160718
- ios developer tiny share-20160719
- ios developer tiny share-20160720
- 线程
- wrap_content和match_parent的区别
- Redis搭建
- 一个问题引发对Linux swap和内存的思考
- Java中判断字符串是否为数字的方法:
- ios developer tiny share-20160919
- springMVC入门
- 自己写的一个利用h5py保存hdf文件的例子,留存!
- 使用C++实现WebSocket服务的一个测试
- 《java多线程编程核心技术》读书笔记6:线程中的异常处理
- 机器学习实战(三)--朴素贝叶斯
- 走进AngularJs的路由机制
- JavaScript阻塞
- DispatcherServlet详解