ios中能不能将ios 创建plist文件件code转成省市区

当前位置: →
→ 在iOS开发中调用本地plist文件并封装为NSDictionary字典类型
在iOS开发中调用本地plist文件并封装为NSDictionary字典类型
& 作者及来源: Chen_Yilong - 博客园 &
&收藏到→_→:
摘要: 在iOS开发中调用本地plist文件并封装为NSDictionary字典类型
"在iOS开发中调用本地plist文件并封装为NSDictionary字典类型"::
现将在ios开发中调用本地plist文件并封装为nsdictionary字典类型做成图片。例子均是来自《ios5开发基础教程》中的章节,分别为《表视图--08 - sections2》、《加速计---19 - shakeandbreak》 、《标签栏和选取器&07-mypicker》 &,例子下载地址:点击打开链接&
nsstring *path= [[nsbundle mainbundle] pathforresource:@&sortednames& oftype:@&plist&];
nsdictionary *dict =[[nsdictionary alloc] initwithcontentsoffile:path];&方法二:
nsurl *plisturl = [[nsbundle mainbundle] urlforresource:@&stateddictionary& withextension:@&plist&];
nsdictionary *dict = [nsdictionary dictionarywithcontentsofurl:plisturl];或者将方法一与方法二综合: &
nsstring *path= [[nsbundle mainbundle] pathforresource:@&sortednames& oftype:@&plist&];
nsurl *plisturl = [nsurl fileurlwithpath:path];
nsdictionary *dict = [nsdictionary dictionarywithcontentsofurl:plisturl];
搜索此文相关文章:此文来自: 马开东博客
网址: 站长QQ
在iOS开发中调用本地plist文件并封装为NSDictionary字典类型_博客园相关文章
博客园_总排行榜
博客园_最新
博客园_月排行榜
博客园_周排行榜
博客园_日排行榜上文中的图片浏览器的实例,在数据的使用上做的很不好,由于将图片数据写到了代码里,不是一种合理的做法。如果数据经常改,就要经常翻开对应的代码进行修改,造成代码扩展性低,因此,可以考虑将经常变的数据放在文件中进行存储,程序启动后从文件中读取最新的数据。如果要变动数据,直接修改数据文件即可,不用修改代码。而存放数据的文件陈伟plist文件。
二. Plist文件
1. 什么是plist的文件?
plist全称为Property List也称为属性列表文件,它提供了一个方便的方法来存放简单的结构数据,通常为XML格式,Property List存放的数据类型是有限的,包括NSArray、NSDictionary、& &NSString、&Boolean、NSNumber、NSData、NSDate。这种属性列表文件的扩展名是plist,因此也成为&Plist文件&.
注意:plist文件的根数据类型即解析plist文件得到的数据类型,只有两种NSArray或者NSDictionary。
2.创建plist文件
3. 创建plist文件中的数据
在xcode中打开即可点击+号添加数据并可选则数据类型如下图:
4.利用NSBundle解析plist文件:
1. 利用NSBundle可以访问某个资源包的内容
2. 如果访问软件中最主要资源包的内容,就应该利用mainBundle
NSBundle *bundle = [NSBundle mainBundle];
3. 利用NSBundle获取文件最主要的路径path,参数为文件名加上扩展名。
NSString *path = [bundle pathForResource:@"describ" ofType:@"plist"];
也可以在文件名种带上扩展名,将扩展名参数置为nil
NSString *path = [bundle pathForResource:@"describ.plist" ofType:nil];
4. 使用文件中的数组或字典
// 如果plist文件的根数据为数组
NSArray *imageArray = [NSArray arrayWithContentsOfFile:path];
// 如果plist文件的根数据为字典
NSDictionary *dict =[NSDictionary dictionaryWithContentsOfFile:path];
三. 使用Plist文件优化图片浏览器程序
1.将图片的名字和描述以字典数组的方式添加得到创建的plist文件中。创建好的文件如下图
2. 代码的修改.使用plist文件之后只需要修改上个实例的imageData方法
- (NSArray *)imageData
// 懒加载必须先判断是否已经加载了数据
if(_imageData == nil)
// 获取plist文件路径
//NSString *path = [[NSBundle mainBundle] pathForResource:@"imageData" ofType:@"plist"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"imageData.plist" ofType:nil];
// 从plist文件解析数组
_imageData = [NSArray arrayWithContentsOfFile:path];
return _imageD
使用plist文件之后是我们的程序更加灵活,将数据和代码分开,需要增删数据时不必再修改代码。
图片刘浏览器pilist
阅读(...) 评论()全国省市区信息,plist格式
查看次数:18886
下载次数:3536
上传时间:
大小:84 B
项目中需要用到全国的省市区信息。在网上找到的都不是很好用,就自己编辑了一个plist文件,根据2007年国标制作。
plist制作考虑了排序的问题,省和直辖市的顺序是直辖市在前,各省的排序按照国标顺序。各省内的市级排序也是按照国标排序。大家可以根据自己的需求修改。
cvpic('/albums/uu11/st_cruze/iOS6AC4F5E555FEB7167ldquo12903rdquo.png','','700','700')
cvpic('/albums/uu11/st_cruze/iOS6AC4F5E555FEB7167ldquo12925rdquo.png','','700','700')
您还没有登录!请或
下载过该代码的还下载了
本周热门下载
&2017 Chukong Technologies,Inc.
京公网安备89上一篇文章将plist文件中的中国省市区读取后,保存为json字符串打印出来后没有错误,现在再将其数据利用UIPickerView展示。
1,以前运用UIPickerView一般是年月日,基本上数据都是死数组组成,例如每年就12个月 ,一个月天数也就28,28,30或者31,这样的话写起来应该不会麻烦;
2,省市区对照表中,每个省对应的市不同,每个市对应的区也不同,这就导致在填充数据时会动态改变,这个时候要利用UIPickerView的代理方法和其接口方法来实现
首先添加一个uipickerview为子view,设置代理为self,这样才可以对其填充数据和相应操作,类似uitableview。
然后实现uipickerview的代理方法,在选中某一组的某一行时更新其后边的一组数据
-(void)addpickerview
& & mypiker=[[UIPickerView
alloc]initWithFrame:CGRectMake(0,
& & mypiker.delegate=self;
& & mypiker.dataSource=self;
& & mypiker.showsSelectionIndicator=YES;
& & [self.view
addSubview:mypiker]; &
& & carray=[[NSMutableArray
alloc]initWithArray:[Pdic
objectForKey:[parray
objectAtIndex:0]]];
& & if ([[carray
objectAtIndex:0]
isKindOfClass:[NSString
class]]) {
& & }else{
& & & & ddictionary=[NSMutableDictionary
dictionaryWithDictionary:[carray&
objectAtIndex:0]];
NSArray * array=[[ddictionary
allValues] objectAtIndex:0];
dearray=[[NSMutableArray
alloc]initWithArray:array];
NSLog(@&%d&,dearray.count);
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
if (component==0) {
return parray.count;
if(component==1){
return carray.count;
if(component==2){
NSLog(@&%d&,dearray.count);
return dearray.count;
& & return
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
return 44;
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component
if (component==0) {
return [parray
objectAtIndex:row];
if (component==1) {
&& & & if ([[carray
objectAtIndex:row] isKindOfClass:[NSString
class]]) {
&& & & & &
return [carray
objectAtIndex:row];
&& & & }else{
& & & & return& [[[carray
objectAtIndex:row] allKeys]
objectAtIndex:0];
return [dearray
objectAtIndex:row];
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
NSLog(@&%d----%d&,row,component);
if (component==0) {
& & & & carray=[[NSMutableArray
alloc]initWithArray:[Pdic
objectForKey:[parray
objectAtIndex:row]]];
& & & & if ([[carray
objectAtIndex:0]
isKindOfClass:[NSDictionary
class]]) {
& & & & & &
ddictionary=[carray
objectAtIndex:0];
& & & & & & dearray=[[NSMutableArray
alloc]initWithArray:[[ddictionary
allValues] objectAtIndex:0]];
& & & & & & [dearray
removeAllObjects];
& & & & & & [dearray
addObject:@&无&];
& & & & [mypiker
reloadComponent:1];
& & & & [mypiker
reloadComponent:2];
else if (component==1){
& & & & if ([[carray
objectAtIndex:row] isKindOfClass:[NSDictionary
class]]) {
& & & & & &
ddictionary=[carray
objectAtIndex:row];
& & & & & & dearray=[[ddictionary
allValues] objectAtIndex:0];
& & & & }else{
& & & & & & [dearray
removeAllObjects];
& & & & & & [dearray
addObject:@&无&];
& & & & & &
NSLog(@&没有区域选项&);
& & & & [mypiker
reloadComponent:2];
本文已收录于以下专栏:
相关文章推荐
1. UIPickerView 什么时候使用?通常在注册模块,当用户需要选择一些东西的时候使用,比如说城市,往往弹出一个 PickerView 给用户选择。2. UIPickerView 常见用法(1...
#import &HomeTableViewCell.h&
@interface HomeViewController :
UIViewControllerUITableViewDataSou...
实现效果如下图:
直接继承MOFSAddressPickerView类,初始化即可。方法func show()调出pickerView
var pickerView...
二话不说就上图,大家看看效果先
本篇文章主要涉及讲解表视图和集合视图和网络请求解析数据
以下的类的创建:
这里我给一个URL定位符...
他的最新文章
讲师:汪剑
讲师:刘道宽
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 iosplist文件 的文章

 

随机推荐