版本記錄
| 版本號(hào) | 時(shí)間 |
|---|---|
| V1.0 | 2017.09.07 |
前言
很多時(shí)候ios系統(tǒng)提供的類不能滿足我們使用的需要,這個(gè)時(shí)候就可以自己寫(xiě)一個(gè)分類,為該系統(tǒng)類提供新的方法,以解決實(shí)際問(wèn)題,所以接下來(lái)幾篇我們就說(shuō)一下和分類相關(guān)的內(nèi)容,還是老規(guī)矩,由簡(jiǎn)單到復(fù)雜,由大范圍到小點(diǎn)解析這樣的思路,希望對(duì)大家有所幫助。
什么時(shí)候需要分類?
當(dāng)系統(tǒng)的類無(wú)法滿足我們功能上的要求的時(shí)候,我們就可以寫(xiě)一個(gè)該類的分類,為該類添加新的方法,滿足自己的功能需求。
蘋(píng)果系統(tǒng)內(nèi)部就使用了很多分類,比如下邊
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
這個(gè)就是NSString的一個(gè)分類,這里本類名字是寫(xiě)在括號(hào)外面的,分類名字寫(xiě)在括號(hào)里面,大家要注意一下寫(xiě)法。
分類建立過(guò)程
下面我們就看一下分類的建立過(guò)程,可以直接用xcode建立一個(gè)分類。


這樣NSString的一個(gè)分類就建立完成了,下面我們就寫(xiě)代碼測(cè)試。
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的一個(gè)分類,內(nèi)部什么都沒(méi)做");
}
@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
下面看輸出結(jié)果
2017-09-07 12:00:00.477220+0800 JJOC[2537:1529133] 只是NSString的一個(gè)分類,內(nèi)部什么都沒(méi)做
可見(jiàn),分類的方法可以調(diào)用并起作用了,是不是很簡(jiǎn)單吧。
分類使用幾種情形
- 擴(kuò)展已有的類。
- 引用父類未公開(kāi)方法。
- 實(shí)現(xiàn)簡(jiǎn)單協(xié)議。
分類使用注意情況
- 只能添加方法,不能添加屬性。在類別中聲明的屬性和成員變量,將無(wú)法存取。
- 類別中的方法,會(huì)覆蓋父類中的同名方法,無(wú)法再調(diào)用父類中的方法(因?yàn)轭悇e中無(wú)法使用super),為防止意外覆蓋,總是應(yīng)該給類別加上前綴。
- 不同文件中的同名類別,同名方法,不會(huì)報(bào)錯(cuò),實(shí)際執(zhí)行的方法以最后一個(gè)加載的文件為準(zhǔn),因此使用前綴防止類別人互相覆蓋。
- 分類里也可以聲明屬性,但是分類無(wú)法合成與屬性相關(guān)的實(shí)例變量,所以開(kāi)發(fā)者需要在分類中為該屬性實(shí)現(xiàn)存取方法。
- 解決1:在
@implementation中把存取方法聲明為@dynamic,告訴編譯器這些方法運(yùn)行時(shí)再提供,并且需要在消息轉(zhuǎn)發(fā)機(jī)制在運(yùn)行期攔截調(diào)用時(shí)提供實(shí)現(xiàn)。 - 解決2:通過(guò)關(guān)聯(lián)對(duì)象方式將屬性與特質(zhì)關(guān)聯(lián),讓系統(tǒng)為之提供方法。
- 解決3:盡量避免在分類中聲明屬性。
- 解決1:在
參考文章
1. iOS開(kāi)發(fā)基礎(chǔ):如何使用類別(Category)
后記
未完,待續(xù)~~~
