版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.06.02 |
前言
前面我简单的写了些NSString的初始化,写了几篇,都不难,但是可以对新手有一定的小帮助,对于大神级人物可以略过这几篇,NSString本来就没有难的,都是细枝末节,忘记了查一下就会了,没有技术难点,下面我们继续~~~
1. NSString简单细说(一)—— NSString整体架构
2. NSString简单细说(二)—— NSString的初始化
3. NSString简单细说(三)—— NSString初始化
4. NSString简单细说(四)—— 从URL初始化
5. NSString简单细说(五)—— 向文件或者URL写入
6. NSString简单细说(六)—— 字符串的长度
7. NSString简单细说(七)—— 与C字符串的转化
8. NSString简单细说(八)—— 识别和比较字符串
9. NSString简单细说(九)—— 字符串的合并
10. NSString简单细说(十)—— 字符串的分解
11. NSString简单细说(十一)—— 字符串的查找
12. NSString简单细说(十二)—— 字符串的替换
13. NSString简单细说(十三)—— 字符串的分行和分段
14. NSString简单细说(十四)—— 字符串位置的计算
字符串转化为propertyList
一、- (id)propertyList;
这个方法的作用就是将字符串转化为propertyList文件,返回的类型是NSString、NSData、NSArray、NSDictionary,这个返回的类型由最顶层的元素决定。如果转化失败则报错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);
下面看输出结果
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
结论:这个很简单,最外层是字典,所以返回结果p的class还是字典。
二、- (NSDictionary *)propertyListFromStringsFileFormat;
与上一个方法相比,这个只能返回字典类型,要转化的字符串必须满足下边几个条件:
- 键值必须以等号连接;
- 每一个键值对都是以分号结尾;
- 值不显示时可以省略,同时等号也是可以省略的;
- 键值都分别用分号隔开;
- 也可以包含注释,注释用/* 和 */分割。
下面还是直接看代码
/**
* 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);
下面看输出结果
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
结论:可见这个返回的只是字典类型。
后记
明天又放假了,未完,待续~~~