版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.09.07 |
前言
很多时候ios系统提供的类不能满足我们使用的需要,这个时候就可以自己写一个分类,为该系统类提供新的方法,以解决实际问题,所以接下来几篇我们就说一下和分类相关的内容,还是老规矩,由简单到复杂,由大范围到小点解析这样的思路,希望对大家有所帮助。
什么时候需要分类?
当系统的类无法满足我们功能上的要求的时候,我们就可以写一个该类的分类,为该类添加新的方法,满足自己的功能需求。
苹果系统内部就使用了很多分类,比如下边
typedef NSString * NSStringEncodingDetectionOptionsKey NS_STRING_ENUM;
@interface NSString (NSStringEncodingDetection)
#pragma mark *** Encoding detection ***
/* This API is used to detect the string encoding of a given raw data. It can also do lossy string conversion. It converts the data to a string in the detected string encoding. The data object contains the raw bytes, and the option dictionary contains the hints and parameters for the analysis. The opts dictionary can be nil. If the string parameter is not NULL, the string created by the detected string encoding is returned. The lossy substitution string is emitted in the output string for characters that could not be converted when lossy conversion is enabled. The usedLossyConversion indicates if there is any lossy conversion in the resulted string. If no encoding can be detected, 0 is returned.
The possible items for the dictionary are:
1) an array of suggested string encodings (without specifying the 3rd option in this list, all string encodings are considered but the ones in the array will have a higher preference; moreover, the order of the encodings in the array is important: the first encoding has a higher preference than the second one in the array)
2) an array of string encodings not to use (the string encodings in this list will not be considered at all)
3) a boolean option indicating whether only the suggested string encodings are considered
4) a boolean option indicating whether lossy is allowed
5) an option that gives a specific string to substitude for mystery bytes
6) the current user's language
7) a boolean option indicating whether the data is generated by Windows
If the values in the dictionary have wrong types (for example, the value of NSStringEncodingDetectionSuggestedEncodingsKey is not an array), an exception is thrown.
If the values in the dictionary are unknown (for example, the value in the array of suggested string encodings is not a valid encoding), the values will be ignored.
*/
+ (NSStringEncoding)stringEncodingForData:(NSData *)data
encodingOptions:(nullable NSDictionary<NSStringEncodingDetectionOptionsKey, id> *)opts
convertedString:(NSString * _Nullable * _Nullable)string
usedLossyConversion:(nullable BOOL *)usedLossyConversion NS_AVAILABLE(10_10, 8_0);
/* The following keys are for the option dictionary for the string encoding detection API.
*/
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionSuggestedEncodingsKey NS_AVAILABLE(10_10, 8_0); // NSArray of NSNumbers which contain NSStringEncoding values; if this key is not present in the dictionary, all encodings are weighted the same
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionDisallowedEncodingsKey NS_AVAILABLE(10_10, 8_0); // NSArray of NSNumbers which contain NSStringEncoding values; if this key is not present in the dictionary, all encodings are considered
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionUseOnlySuggestedEncodingsKey NS_AVAILABLE(10_10, 8_0); // NSNumber boolean value; if this key is not present in the dictionary, the default value is NO
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionAllowLossyKey NS_AVAILABLE(10_10, 8_0); // NSNumber boolean value; if this key is not present in the dictionary, the default value is YES
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionFromWindowsKey NS_AVAILABLE(10_10, 8_0); // NSNumber boolean value; if this key is not present in the dictionary, the default value is NO
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionLossySubstitutionKey NS_AVAILABLE(10_10, 8_0); // NSString value; if this key is not present in the dictionary, the default value is U+FFFD
FOUNDATION_EXPORT NSStringEncodingDetectionOptionsKey const NSStringEncodingDetectionLikelyLanguageKey NS_AVAILABLE(10_10, 8_0); // NSString value; ISO language code; if this key is not present in the dictionary, no such information is considered
@end
这个就是NSString的一个分类,这里本类名字是写在括号外面的,分类名字写在括号里面,大家要注意一下写法。
分类建立过程
下面我们就看一下分类的建立过程,可以直接用xcode建立一个分类。
这样NSString的一个分类就建立完成了,下面我们就写代码测试。
1. NSString+JJNSStringCategory.h
#import <Foundation/Foundation.h>
@interface NSString (JJNSStringCategory)
- (void)demoCategory;
@end
2. NSString+JJNSStringCategory.m
#import "NSString+JJNSStringCategory.h"
@implementation NSString (JJNSStringCategory)
- (void)demoCategory
{
NSLog(@"只是NSString的一个分类,内部什么都没做");
}
@end
3. JJNSStringVC.h
#import <UIKit/UIKit.h>
@interface JJNSStringVC : UIViewController
@end
4. JJNSStringVC.m
#import "JJNSStringVC.h"
#import "NSString+JJNSStringCategory.h"
@interface JJNSStringVC ()
@end
@implementation JJNSStringVC
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str = @"hello world";
[str demoCategory];
}
@end
下面看输出结果
2017-09-07 12:00:00.477220+0800 JJOC[2537:1529133] 只是NSString的一个分类,内部什么都没做
可见,分类的方法可以调用并起作用了,是不是很简单吧。
分类使用几种情形
- 扩展已有的类。
- 引用父类未公开方法。
- 实现简单协议。
分类使用注意情况
- 只能添加方法,不能添加属性。在类别中声明的属性和成员变量,将无法存取。
- 类别中的方法,会覆盖父类中的同名方法,无法再调用父类中的方法(因为类别中无法使用super),为防止意外覆盖,总是应该给类别加上前缀。
- 不同文件中的同名类别,同名方法,不会报错,实际执行的方法以最后一个加载的文件为准,因此使用前缀防止类别人互相覆盖。
- 分类里也可以声明属性,但是分类无法合成与属性相关的实例变量,所以开发者需要在分类中为该属性实现存取方法。
- 解决1:在
@implementation
中把存取方法声明为@dynamic
,告诉编译器这些方法运行时再提供,并且需要在消息转发机制在运行期拦截调用时提供实现。 - 解决2:通过关联对象方式将属性与特质关联,让系统为之提供方法。
- 解决3:尽量避免在分类中声明属性。
- 解决1:在
参考文章
后记
未完,待续~~~