一句話簡介:最著名的OC開源網絡庫。
Github: 傳送門
PS: 本拾遺系列文章只專注於代碼以及工程層面知識點拾遺,架構層面作者文章已經進行了詳細的講解。
1. @import和modulemap
首先OC中的@import以及Swift中的import其實都是基於modulemap實現的。
@import的使用可以參考:@import vs #import
關於modulemap相關的內容可以參考:Modular framework, creating and using them
2. 各種標記
__unused: 避免因變量聲明未使用造成的警告。
ARC相關(如__autoreleasing)參考: iOS開發ARC內存管理技術要點
NS_DESIGNATED_INITIALIZER 聲明了designated初始化方法後,其余初始化方法如果沒有調用designated初始化會有警告提示,具體可以參考: Xcode 6 Objective-C Modernization
DEPRECATED_ATTRIBUTE(過期)、NS_SWIFT_NOTHROW(一些便於OC遷移Swift的標記)等
3. 宏
FOUNDATION_EXPORT: 對於extern的兼容性封裝,根據不同的平台,轉化為對應的extern形式。
NS_ASSUME_NONNULL_BEGIN、NS_ASSUME_NONNULL_END: 在宏范圍內的變量、參數、返回值等都默認添加nonnull。
4. queryString轉換的經典代碼
經常被摘出來放到自己項目中使用。
/**
Returns a percent-escaped string following RFC 3986 for a query string key or value.
RFC 3986 states that the following characters are "reserved" characters.
- General Delimiters: ":", "#", "[", "]", "@", "?", "/"
- Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
should be percent-escaped in the query string.
- parameter string: The string to be percent-escaped.
- returns: The percent-escaped string.
*/
NSString * AFPercentEscapedStringFromString(NSString *string) {
static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@";
// does not include "?" or "/" due to RFC 3986 - Section 3.4
static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
[allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];
// FIXME:
// return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
// 以下為針對非單字節字符的處理
static NSUInteger const batchSize = 50;
NSUInteger index = 0;
NSMutableString *escaped = @"".mutableCopy;
while (index < string.length) {
NSUInteger length = MIN(string.length - index, batchSize);
NSRange range = NSMakeRange(index, length);
// To avoid breaking up character sequences such as ????????????????
range = [string rangeOfComposedCharacterSequencesForRange:range];
NSString *substring = [string substringWithRange:range];
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
[escaped appendString:encoded];
index += range.length;
}
return escaped;
}5. 架構簡圖
