版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2017.06.02 |
前言
前面我簡單的寫了些NSString的初始化,寫了幾篇,都不難,但是可以對新手有一定的小幫助,對于大神級人物可以略過這幾篇,NSString本來就沒有難的,都是細枝末節(jié),忘記了查一下就會了,沒有技術(shù)難點,下面我們繼續(xù)~~~
1. NSString簡單細說(一)—— NSString整體架構(gòu)
2. NSString簡單細說(二)—— NSString的初始化
3. NSString簡單細說(三)—— NSString初始化
4. NSString簡單細說(四)—— 從URL初始化
5. NSString簡單細說(五)—— 向文件或者URL寫入
6. NSString簡單細說(六)—— 字符串的長度
7. NSString簡單細說(七)—— 與C字符串的轉(zhuǎn)化
8. NSString簡單細說(八)—— 識別和比較字符串
9. NSString簡單細說(九)—— 字符串的合并
10. NSString簡單細說(十)—— 字符串的分解
11. NSString簡單細說(十一)—— 字符串的查找
12. NSString簡單細說(十二)—— 字符串的替換
13. NSString簡單細說(十三)—— 字符串的分行和分段
14. NSString簡單細說(十四)—— 字符串位置的計算
字符串轉(zhuǎn)化為propertyList
一、- (id)propertyList;
這個方法的作用就是將字符串轉(zhuǎn)化為propertyList文件,返回的類型是NSString、NSData、NSArray、NSDictionary,這個返回的類型由最頂層的元素決定。如果轉(zhuǎn)化失敗則報錯NSParseErrorException。
下面直接看代碼
/**
* 1. - (id)propertyList;
*
* @return: A property list representation of returning an NSString, NSData, NSArray, or NSDictionary object, according to the topmost element.
*/
NSString *str =
@"<dict>"
@"<key>CFBundleDevelopmentRegion</key>"
@"<string>en</string>"
@"<key>CFBundleExecutable</key>"
@"<string>$(EXECUTABLE_NAME)</string>"
@"<key>CFBundleIdentifier</key>"
@"<string>com.$(PRODUCT_NAME:rfc1034identifier)</string>"
@"<key>CFBundleInfoDictionaryVersion</key>"
@"<string>6.0</string>"
@"<key>CFBundleName</key>"
@"<string>$(PRODUCT_NAME)</string>"
@"<key>CFBundlePackageType</key>"
@"<string>APPL</string>"
@"<key>CFBundleShortVersionString</key>"
@"<string>1.0</string>"
@"<key>CFBundleSignature</key>"
@"<string>????</string>"
@"<key>CFBundleVersion</key>"
@"<string>1</string>"
@"<key>LSRequiresIPhoneOS</key>"
@"<true/>"
@"<key>UILaunchStoryboardName</key>"
@"<string>LaunchScreen</string>"
@"<key>UIMainStoryboardFile</key>"
@"<string>Main</string>"
@"<key>UIRequiredDeviceCapabilities</key>"
@"<array>"
@"<string>armv7</string>"
@"</array>"
@"<key>UISupportedInterfaceOrientations</key>"
@"<array>"
@"<string>UIInterfaceOrientationPortrait</string>"
@"<string>UIInterfaceOrientationLandscapeLeft</string>"
@"<string>UIInterfaceOrientationLandscapeRight</string>"
@"</array>"
@"</dict>";
id p = [str propertyList];
NSLog(@"[str propertyList] = %@",p);
下面看輸出結(jié)果
2017-06-02 23:06:46.705 NSString你會用嗎?[1454:38056] [str propertyList] = {
CFBundleDevelopmentRegion = en;
CFBundleExecutable = "$(EXECUTABLE_NAME)";
CFBundleIdentifier = "com.$(PRODUCT_NAME:rfc1034identifier)";
CFBundleInfoDictionaryVersion = "6.0";
CFBundleName = "$(PRODUCT_NAME)";
CFBundlePackageType = APPL;
CFBundleShortVersionString = "1.0";
CFBundleSignature = "????";
CFBundleVersion = 1;
LSRequiresIPhoneOS = 1;
UILaunchStoryboardName = LaunchScreen;
UIMainStoryboardFile = Main;
UIRequiredDeviceCapabilities = (
armv7
);
UISupportedInterfaceOrientations = (
UIInterfaceOrientationPortrait,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight
);
}
(lldb) po [p class]
__NSCFDictionary
結(jié)論:這個很簡單,最外層是字典,所以返回結(jié)果p的class還是字典。
二、- (NSDictionary *)propertyListFromStringsFileFormat;
與上一個方法相比,這個只能返回字典類型,要轉(zhuǎn)化的字符串必須滿足下邊幾個條件:
- 鍵值必須以等號連接;
- 每一個鍵值對都是以分號結(jié)尾;
- 值不顯示時可以省略,同時等號也是可以省略的;
- 鍵值都分別用分號隔開;
- 也可以包含注釋,注釋用/* 和 */分割。
下面還是直接看代碼
/**
* 2. - (NSDictionary *)propertyListFromStringsFileFormat;
*
* @return: Returns a dictionary object initialized with the keys and values found in the receiver.
*/
NSString *str =
@"/* Question in confirmation panel for quitting. */"
@"\"Confirm Quit\" = \"Are you sure you want to quit?\";"
@""
@"/* Message when user tries to close unsaved document */"
@"\"Close or Save\" = \"Save changes before closing?\";"
@""
@"/* Word for Cancel */"
@"\"Cancel\";";
id p = [str propertyListFromStringsFileFormat];
NSLog(@"[str propertyList] = %@",p);
下面看輸出結(jié)果
2017-06-02 23:27:26.195 NSString你會用嗎?[1811:52386] [str propertyList] = {
Cancel = Cancel;
"Close or Save" = "Save changes before closing?";
"Confirm Quit" = "Are you sure you want to quit?";
}
(lldb) po [p class];
__NSCFDictionary
結(jié)論:可見這個返回的只是字典類型。
后記
明天又放假了,未完,待續(xù)~~~
