Iphone 检索存储在plist文件中的数组数据
如何检索存储在plist文件中的数组数据 plist文件和源代码如下所示Iphone 检索存储在plist文件中的数组数据,iphone,objective-c,plist,Iphone,Objective C,Plist,如何检索存储在plist文件中的数组数据 plist文件和源代码如下所示 ------------------------plist File------------------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
------------------------plist File-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Name</key>
<string>abcxxx</string>
<key>Phones</key>
<array/>
<key>item1</key>
<string>121327777</string>
<key>item2</key>
<string>333333333</string>
</dict>
</plist>
---------------------------------------------------------
-(id) init
{
NSString *errorDesc = nil;
NSPropertyListFormat format;
phoneNumbers =[[NSMutableArray alloc]init];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"testfile" ofType:@"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
if(!temp){
NSLog(errorDesc);
[errorDesc release];
}
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];
NSLog(@"Label executed %@",[phoneNumbers objectAtIndex:0]);
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(-180, 100, 100, 20)];
name.text = [phoneNumbers objectAtIndex:1];
[self addChild:name];
NSLog(@"Label executed %@",name);
---------------------------plist文件-------------------------
名称
abcxxx
电话
项目1
121327777
项目2
333333333
---------------------------------------------------------
-(id)init
{
NSString*errorDesc=nil;
NSPropertyListFormat格式;
PhoneNumber=[[NSMutableArray alloc]init];
NSString*plistPath=[[NSBundle mainBundle]pathForResource:@“testfile”,类型:@“plist”];
NSData*plistXML=[[NSFileManager defaultManager]contentsAtPath:plistPath];
NSDictionary*temp=(NSDictionary*)[NSPropertyListSerialization
propertyListFromData:plistXML
可变选项:NSPropertyListMutableContainers和Leaves
格式:&格式错误描述:&错误描述];
如果(!temp){
NSLog(errorDesc);
[错误描述发布];
}
self.personName=[temp objectForKey:@“Name”];
self.phoneNumbers=[NSMutableArray arrayWithArray:[temp objectForKey:@“Phones”];
NSLog(@“Label executed%@,[phoneNumbers objectAtIndex:0]);
UILabel*name=[[UILabel alloc]initWithFrame:CGRectMake(-180100100,20)];
name.text=[phoneNumbers objectAtIndex:1];
[self addChild:name];
NSLog(@“标签执行%@”,名称);
您的plist XML看起来有点可疑。您所要做的就是调用NSDictionary的initWithContentsOfFile:method。例如
NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [[temp objectForKey:@"Phones"] mutableCopy];
但是,如上所述,您的plist看起来确实有点可疑,请尝试将其更改为Andy White提供的plist
问候,
KyleUITextField的帧也很奇怪(取决于self的帧)CGRectMake(-18010010020)
可能在屏幕外。最有可能的是你根本无法读取plist,只需检查你的plist结构
NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
_myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
<plist version="1.0">
<dict>
<key>Root</key>
<array>
<string>Happy</string>
<string>Rain</string>
</array>
NSString*StringsFromPList=[[NSBundle mainBundle]bundlePath];
NSString*itemPositionPlistLocation=[StringsFromplistStringByAppendingPathComponent:@“Words.plist”];
_myDictionary=[[NSDictionary alloc]initWithContentsOfFile:itemPositionPlistLocation];
NSArray*items=[\u myDictionary objectForKey:@“Root”];
根
高兴的
雨
希望有帮助:)您没有说实际的问题是什么。问题是什么?这并不会使所有的容器和叶子都是可变的,而NSPropertyListSerialization技术会。
NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
_myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
<plist version="1.0">
<dict>
<key>Root</key>
<array>
<string>Happy</string>
<string>Rain</string>
</array>