属性列表初涉(Xcode4.3.2与Xcode4.5)

从项目中读取

1、以NSArray形式

 

    NSString *pathStr = [[NSBundle mainBundle] pathForResource:@"Property" ofType:@"plist"];
    NSArray *arr = [[NSArray alloc] initWithContentsOfFile:pathStr];
 

 

2、 以NSDictionary形式:

 

    NSString *pathStr = [[NSBundle mainBundle] pathForResource:@"Property" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:pathStr];
 

 

在Xcode4.3.2中,建立属性列表时,默认是以NSDictionary形式的(我么有找到修改设置的方式,除非直接改XML的代码),如下代码:

 

<?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>New item0</key>
	<string>0</string>
	<key>New item1</key>
	<string>1</string>
	<key>New item2</key>
	<string>2</string>
</dict>
</plist>

改成:

 

<?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">
<array>
	<string>0</string>
	<string>1</string>
	<string>2</string>
</array>
</plist>

 

在Xcode4.5可以直接修改设置,将Root的Type改为Array就行了。

 

 

 

 

 

不在项目中,需要搜索文件路径的:

 

- (NSString *)dataFilePath
{
    //获得应用程序Documents的文件夹
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"============%@",paths);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    //获取文件的完整路径
    return [documentsDirectory stringByAppendingPathComponent:@"Property List.plist"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

  

    //找路径之后
    NSString *filePath = [self dataFilePath];
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        for(int i=0; i<[array count]; i++)
        {
            NSLog(@"--------%@",[array objectAtIndex:i]);
        }
    }
 

 

你可能感兴趣的:(ios,plist,Xcode版本区别之一,属性列表读取)