求statedictionary.plist这个ios plist文件读写

1479人阅读
// 通过plist载入缓存
CCSpriteFrameCache::sharedSpriteFrameCache()-&addSpriteFramesWithFile(&test.plist&);
// 通过缓存载入sprite
CCSprite* sp = CCSprite::createWithSpriteFrameName(&test01.png&);
以上2句代码,可以通过plist创建一个sprite,简单分析下过程。
通过plist载入缓存
首先是通过plist载入缓存,跟踪到源码看到如下片段:
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist)
CCAssert(pszPlist, &plist filename should not be NULL&);
// 判断是否加载过该文件 如果没加载过,才做以下这些事情
if (m_pLoadedFileNames-&find(pszPlist) == m_pLoadedFileNames-&end())
// 获取完整路径名,创建dict
std::string fullPath = CCFileUtils::sharedFileUtils()-&fullPathForFilename(pszPlist);
CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(fullPath.c_str());
string texturePath(&&);
// 尝试获取textureFileName,如果需要指定,格式大概长这样:
// &key&metadata&/key&
// &key&textureFileName&/key&
// &string&tex.png&/string&
// &/dictt&
// 指定载入tex.png
CCDictionary* metadataDict = (CCDictionary*)dict-&objectForKey(&metadata&);
if (metadataDict)
// try to read
texture file name from meta data
texturePath = metadataDict-&valueForKey(&textureFileName&)-&getCString();
// 如果有指定tex文件,则直接查找完整路径
if (! texturePath.empty())
// build texture path relative to plist file
texturePath = CCFileUtils::sharedFileUtils()-&fullPathFromRelativeFile(texturePath.c_str(), pszPlist);
else // 没有指定tex文件,载入plist对应的png,比如plist文件名:abc.plist,则载入的png为:abc.png
// build texture path by replacing file extension
texturePath = pszP
// remove .xxx
size_t startPos = texturePath.find_last_of(&.&);
texturePath = texturePath.erase(startPos);
// append .png
texturePath = texturePath.append(&.png&);
CCLOG(&cocos2d: CCSpriteFrameCache: Trying to use file %s as texture&, texturePath.c_str());
// 载入图片
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()-&addImage(texturePath.c_str());
if (pTexture)
// 载入spriteFrames
addSpriteFramesWithDictionary(dict, pTexture);
m_pLoadedFileNames-&insert(pszPlist);
CCLOG(&cocos2d: CCSpriteFrameCache: Couldn't load texture&);
dict-&release();
然后到了这里:
void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary* dictionary, CCTexture2D *pobTexture)
// format 4种格式支持
Supported Zwoptex Formats:
ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version
ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b
ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1
ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+
// 分离2个dict
CCDictionary *metadataDict = (CCDictionary*)dictionary-&objectForKey(&metadata&);
CCDictionary *framesDict = (CCDictionary*)dictionary-&objectForKey(&frames&);
int format = 0;
// 从metadata获取格式 默认为0
if(metadataDict != NULL)
format = metadataDict-&valueForKey(&format&)-&intValue();
// 检测format 支持0-3
CCAssert(format &=0 && format &= 3, &format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:textureFilename:&);
// 遍历frames
CCDictElement* pElement = NULL;
CCDICT_FOREACH(framesDict, pElement)
CCDictionary* frameDict = (CCDictionary*)pElement-&getObject();
std::string spriteFrameName = pElement-&getStrKey(); // 取key作为sprite名
// 如果已经加载过 不再处理
CCSpriteFrame* spriteFrame = (CCSpriteFrame*)m_pSpriteFrames-&objectForKey(spriteFrameName);
if (spriteFrame)
// 根据format处理
if(format == 0)
float x = frameDict-&valueForKey(&x&)-&floatValue();
float y = frameDict-&valueForKey(&y&)-&floatValue();
float w = frameDict-&valueForKey(&width&)-&floatValue();
float h = frameDict-&valueForKey(&height&)-&floatValue();
float ox = frameDict-&valueForKey(&offsetX&)-&floatValue();
float oy = frameDict-&valueForKey(&offsetY&)-&floatValue();
int ow = frameDict-&valueForKey(&originalWidth&)-&intValue();
int oh = frameDict-&valueForKey(&originalHeight&)-&intValue();
// check ow/oh
if(!ow || !oh)
CCLOGWARN(&cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist&);
// abs ow/oh
ow = abs(ow);
oh = abs(oh);
// create frame
spriteFrame = new CCSpriteFrame();
// 对照一下,大概就明白了
// initWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
spriteFrame-&initWithTexture(pobTexture,
CCRectMake(x, y, w, h),
CCPointMake(ox, oy),
CCSizeMake((float)ow, (float)oh)
...// 以下类似
// 加入缓存,名字用spriteFrameName &span style=&font-family: Arial, Helvetica, sans-&& m_pSpriteFrames是一个dict&/span&
m_pSpriteFrames-&setObject(spriteFrame, spriteFrameName);
spriteFrame-&release();
于是,这部分的大概流程就是:
载入plist,载入texture,根据plist选择texture的区域,载入spriteFrame,加入到缓存。
通过缓存载入sprite
CCSprite* CCSprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
// 从缓存里取对应名字的spriteFrame
CCSpriteFrame *pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()-&spriteFrameByName(pszSpriteFrameName);
#if COCOS2D_DEBUG & 0
char msg[256] = {0};
sprintf(msg, &Invalid spriteFrameName: %s&, pszSpriteFrameName);
CCAssert(pFrame != NULL, msg);
return createWithSpriteFrame(pFrame);
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:62299次
积分:1376
积分:1376
排名:第16550名
原创:76篇
转载:14篇
评论:17条
(8)(3)(6)(2)(1)(3)(4)(2)(4)(3)(2)(1)(5)(5)(3)(1)(2)(1)(4)(2)(1)(5)(10)(3)(1)(4)(3)(1)后使用快捷导航没有帐号?
只需一步,快速开始
查看: 1093|回复: 5
最后登录阅读权限10注册时间积分152精华0帖子威望0 PP豆12 活跃度67
, 积分 152, 距离下一级还需 148 积分
TA的每日心情开心 07:38签到天数: 6 天连续签到: 4 天[LV.2]偶尔看看I帖子威望0 PP豆12 活跃度67 设备iPhone4s
PP助手官方微信
感激不尽各位,麻烦有的朋友帮帮忙路径/System/Library/Frameworks/MediaToolbox.framework/N94/SystemSoundMaximumVolume.plist
最后登录阅读权限10注册时间积分243精华0帖子威望0 PP豆135 活跃度174
, 积分 243, 距离下一级还需 57 积分
该用户从未签到帖子威望0 PP豆135 活跃度174 设备iPhone 5s
给你....................................
10:31 上传
点击文件名下载附件
下载积分: PP豆 -1
2.07 KB, 下载次数: 38, 下载积分: PP豆 -1
最后登录阅读权限10注册时间积分152精华0帖子威望0 PP豆12 活跃度67
, 积分 152, 距离下一级还需 148 积分
TA的每日心情开心 07:38签到天数: 6 天连续签到: 4 天[LV.2]偶尔看看I帖子威望0 PP豆12 活跃度67 设备iPhone4s
幺崖 发表于
给你....................................
有4S的吗?
最后登录阅读权限10注册时间积分243精华0帖子威望0 PP豆135 活跃度174
, 积分 243, 距离下一级还需 57 积分
该用户从未签到帖子威望0 PP豆135 活跃度174 设备iPhone 5s
就是4S的...................
最后登录阅读权限10注册时间积分152精华0帖子威望0 PP豆12 活跃度67
, 积分 152, 距离下一级还需 148 积分
TA的每日心情开心 07:38签到天数: 6 天连续签到: 4 天[LV.2]偶尔看看I帖子威望0 PP豆12 活跃度67 设备iPhone4s
幺崖 发表于
就是4S的...................
& && && && && && &&&谢谢,非常感谢
最后登录阅读权限20注册时间积分829精华0帖子威望0 PP豆18 活跃度270
, 积分 829, 距离下一级还需 771 积分
TA的每日心情怒昨天&19:20签到天数: 18 天连续签到: 1 天[LV.4]偶尔看看III帖子威望0 PP豆18 活跃度270
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Powered by
Copyright&
Aihe Internet Technology Co.,Ltd. All Rights Reserved.广州爱禾网络技术有限公司 版权所有&&5.1 拾取器5.2 第一视图控制器5.3 第二视图控制器5.4 第三视图控制器5.5 第四视图控制器5.1 拾取器1 创建一个Tab Bar Application初始化工程由于我们要自己创建视图控制器,包括第一个试图控制器,所以我们删除:FirstViewController.h& FirstViewController.m& SecondView.xib修改MainWindow.xib文件删除MainWindow.xib文件中自带的View指定nib文件和视图控制器指定Tab栏按钮图标和title5.2 第一视图控制器DatePickerViewController.h日期拾取器,要想显示日期必须定义一个输出口:@property (nonatomic, retain)& UIDatePicker *dateP定义点击选择按钮的动作事件: -(IBAction)onClickButton:(id)DatePickerViewController.monClickButton:是按钮事件。SDate *selected = [datePicker date];可以获取日期拾取器时间。NSString *message = [[NSString alloc] initWithFormat:@"你选择的日期: %@", selected];可以把日期格式化输出。初始化和释放资源在事件viewDidLoad:初始化日期拾取器。NSDate *now = [NSDate date]; 获得当前的系统时间。[datePicker setDate:now animated:YES];设定日期拾取器的时间,animated:YES是实现动画效果,在初始化时候滚轮滚动到当前日期。IB中链接输出口和动作日期拾取器作为输出口,需要通过File's Owner链接到日期拾取器。还要链接按钮点击时间,从按钮链接到File's Owner。5.3 第二视图控制器SinglePickerViewController.h拾取器,要想显示必须定义一个输出口: @property (nonatomic, retain)& UIPickerView *定义点击选择按钮的动作事件: (IBAction)onClickButton:(id)pickerData保持拾取器的数据。拾取器的委托委托是中的方法是事件触发时候回调的方法。拾取器的委托是实现协议UIPickerViewDelegate。UIPickerViewDelegate回调方法:-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component&是当选择了拾取器时候,根据选择的行返回拾取器的title。拾取器的数据源通过UIPickerViewDataSource协议为拾取器提供数据源,其中包括拾取器的行和列的数据,下面的是数据源要求的方法:- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView&该方法提供拾取器列的个数,本例子中是1个。- (NSInteger)pickerView:(UIPickerView *)pickerView& numberOfRowsInComponent:(NSInteger)component该方法提供了拾取器中行数,这里的数组pickerData的长度。m文件中实现协议初始化和按钮事件处理NSInteger row = [picker selectedRowInComponent:0]; 获得第一列的选择中的行号。NSString *selected = [pickerData objectAtIndex:row]; 通过行号获得选中的数据。释放资源&IB中链接输出口和动作拾取器的输出口,需要File&s Owner与委托和数据源链接。拾取器的委托和数据源输出口,需要File&s Owner与委托和数据源链接。按钮动作onClickButton需要链接到File&s Owner。5.4 第三视图控制器DoublePickerViewController.h拾取器,要想显示必须定义一个输出口:@property (nonatomic, retain)& UIPickerView *定义点击选择按钮的动作事件:-(IBAction)onClick:(id)@property (nonatomic, retain)& NSArray& *pickerData1;@property (nonatomic, retain)& NSArray& *pickerData2;保持拾取器两个列中的数据。拾取器的委托委托是中的方法是事件触发时候回调的方法。拾取器的委托是实现协议UIPickerViewDelegate。UIPickerViewDelegate回调方法:-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component&是当选择了拾取器时候,根据选择的行返回拾取器的title。拾取器的数据源通过UIPickerViewDataSource协议为拾取器提供数据源,其中包括拾取器的行和列的数据,下面的是数据源要求的方法:- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView&该方法提供拾取器列的个数,本例子中是2个。- (NSInteger)pickerView:(UIPickerView *)pickerView& numberOfRowsInComponent:(NSInteger)component该方法提供了拾取器中行数,这里的数组pickerData的长度。m文件中实现协议component == 0代表选择的第一列即&洲&。如果component == 1代表选择的是第二列即&体育项目&。这两个列直接没有关联关系,即你选择了前面和选择了后面没有关系,不会联动。初始化处理按钮事件处理释放资源IB中链接输出口和动作拾取器的输出口,需要File&s Owner与委托和数据源链接。拾取器的委托和数据源输出口,需要File&s Owner与委托和数据源链接。按钮动作onClickButton需要链接到File&s Owner。5.5 第四视图控制器DependentViewController.h拾取器,要想显示必须定义一个输出口: @property (nonatomic, retain)& UIPickerView *pickerV定义点击选择按钮的动作事件: -(IBAction)onClickButton:(id)@property (nonatomic, retain)& NSDictionary& * 保存所有数据@property (nonatomic, retain)& NSArray& *pickerData1; 保存第一列的数据@property (nonatomic, retain)& NSArray& *pickerData2; 保持第二列的数据m文件中的初始化方法NSBundle *bundle = [NSBundle mainBundle];NSString *plistPath = [bundle pathForResource:@"足球队dictionary" ofType:@"plist"];NSDictionary *dict = [[NSDictionary alloc] nitWithContentsOfFile:plistPath];这几行代码是从statedictionary.plist属性列表文件中读取到NSDictionary对象中。NSArray *sorted = [col1 sortedArrayUsingSelector:@selector(compare:)]; 对数据排序。在Xcode中创建属性列表文件 编辑属性列表文件 m中的按钮点击事件 实现委托方法委托方法是实现拾取器控件两个轮互动关键: - (void)pickerView:(UIPickerView *)pickerView& didSelectRow:(NSInteger)row inComponent:(NSInteger)component 在该方法中通过下面语句重新加载拾取器: [self.pickerView reloadComponent:1]; 实现数据源方法 释放资源 IB中链接输出口和动作拾取器的输出口,需要File&s Owner与委托和数据源链接。 拾取器的委托和数据源输出口,需要File&s Owner与委托和数据源链接。 按钮动作onClickButton需要链接到File&s Owner。 注:1 本教程是基于关东升老师的教程2 基于黑苹果10.6.8和xcode4.23 本人初学,有什么不对的望指教4 教程会随着本人学习,持续更新5 教程是本人从word笔记中拷贝出来了,所以格式请见谅主要操作:
1.//获得plist路径 & &-(NSString*)getPlistPath;
2.//判断沙盒中名为plistname的文件是否存在 & &-(BOOL) isPlistFileExists;
3.//读取沙盒中Document文件夹下的BookList.plist文件
& &&[NSMutableDictionarydictionaryWithContentsOfFile:plistPath];
4.//写入文件 & &&if ([plistDictionary writeToFile:plistPath atomically:YES])
WBBooksManager.m文件:
#import "WBBooksManager.h"
@implementation WBBooksManager
static WBBooksManager *g_instance =
+ (WBBooksManager *)sharedInstance
@synchronized(self) {
if ( g_instance == nil ) {
g_instance = [[self alloc] init];
//获得plist路径
-(NSString*)getPlistPath{
//沙盒中的文件路径
NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];
NSString *plistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"WBBooks.plist"];
//根据需要更改文件名
return plistP
//判断沙盒中名为plistname的文件是否存在
-(BOOL) isPlistFileExists{
NSString *plistPath =[[WBBooksManager sharedInstance]getPlistPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if( [fileManager fileExistsAtPath:plistPath]== NO ) {
NSLog(@"not exists");
return NO;
return YES;
-(void)initPlist{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
//如果plist文件不存在,将工程中已建起的plist文件写入沙盒中
if (! [[WBBooksManager sharedInstance] isPlistFileExists]) {
//从自己建立的plist文件 复制到沙盒中 ,方法一
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"WBBooks" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
NSString *path = [[NSBundle mainBundle] pathForResource:@"WBBooks"ofType:@"plist"];
NSMutableDictionary *activityDics = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
[activityDics writeToFile:plistPath atomically:YES];
//判断key的书是否存在
-(BOOL)isBookExistsForKey:(NSString*)key{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
NSMutableDictionary *WBBooksDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//根目录下存在名为bookname的字典
if ([WBBooksDictionary objectForKey:key]) {
return YES;
return NO;
//根据key值删除对应书籍
-(void)removeBookWithKey:(NSString *)key{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
NSMutableDictionary *WBBooksDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
[WBBooksDictionary removeObjectForKey:key];
[WBBooksDictionary writeToFile:plistPath atomically:YES]; //删除后重新写入
//删除plistPath路径对应的文件
-(void)deletePlist{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
[fileManager removeItemAtPath:plistPath error:nil];
//将dictionary写入plist文件,前提:dictionary已经准备好
-(void)writePlist:(NSMutableDictionary*)dictionary forKey:(NSString *)key{
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init];
//如果已存在则读取现有数据
if ([[WBBooksManager sharedInstance]isPlistFileExists]) {
plistDictionary = [[WBBooksManager sharedInstance]readPlist];
//增加一个数据
[plistDictionary setValue:dictionary forKey:key]; //在plistDictionary增加一个key为...的value
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
if([plistDictionary writeToFile:plistPath atomically:YES]){
NSLog(@"write ok!");
NSLog(@"ddd");
-(NSMutableDictionary*)readPlist{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
return resultD
//读取plist文件内容复制给dictionary
-(void)readPlist:(NSMutableDictionary **)dictionary{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];
*dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//更改一条数据,就是把dictionary内key重写
-(void)replaceDictionary:(NSMutableDictionary *)newDictionary withDictionaryKey:(NSString *)key{
[[WBBooksManager sharedInstance]removeBookWithKey:key];
[[WBBooksManager sharedInstance]writePlist:newDictionary forKey:key];
-(NSInteger)getBooksCount{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary = [[WBBooksManager sharedInstance] readPlist];
return [dictionary count];
调用方法:
//导入头文件
#import "WBBooksManager.h"
-(void)viewDidLoad中添加以下代码,是刚写的时候测试时用的,狠时繁杂。
将就着看好了、
WBBooksManager *sss = [[WBBooksManager alloc] init ];
NSString *plistPath =[[WBBooksManager sharedInstance] getPlistPath];
if( [sss isPlistFileExists]== NO ) {//不存在
NSLog(@"WBBooks.plist not exists ,build it.");
NSMutableDictionary *addDictionary1 = [[NSMutableDictionary alloc] init];
NSString *addName1 = [NSString stringWithFormat:@"www"];
NSNumber *addNumber1 = [[NSNumber alloc] initWithInt:13223];
[addDictionary1 setValue:addName1 forKey:@"name"];
[addDictionary1 setValue:addNumber1 forKey:@"list"];
[[WBBooksManager sharedInstance]writePlist:addDictionary1 forKey:@"Add1"];
NSMutableDictionary *addDictionary2 = [[NSMutableDictionary alloc] init];
NSString *addName2 = [NSString stringWithFormat:@"aaas"];
NSNumber *addNumber2 = [[NSNumber alloc] initWithInt:123];
[addDictionary2 setValue:addName2 forKey:@"name"];
[addDictionary2 setValue:addNumber2 forKey:@"list"];
[[WBBooksManager sharedInstance]writePlist:addDictionary2 forKey:@"Add2"];
NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init];
[[WBBooksManager sharedInstance] readPlist:&resultDictionary];
NSLog(@"add1的结果%@", resultDictionary);
NSArray *array = [resultDictionary allKeys];
//所有的Book
[[WBBooksManager sharedInstance] getBooksCount]; //总数
NSLog(@"array%@ %d",array,num);
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init];
plistDictionary = [[WBBooksManager sharedInstance]readPlist];
NSMutableDictionary *addDictionary2 = [[NSMutableDictionary alloc] init];
NSString *addName2 = [NSString stringWithFormat:@"dafd"];
NSNumber *addNumber2 = [[NSNumber alloc] initWithInt:321];
[addDictionary2 setValue:addName2 forKey:@"name"];
[addDictionary2 setValue:addNumber2 forKey:@"list"];
[plistDictionary setValue:addDictionary2 forKey:@"Add2"];
[plistDictionary writeToFile:plistPath atomically:YES];
resultDictionary = [[NSMutableDictionary alloc] init];
[[WBBooksManager sharedInstance] readPlist:&resultDictionary];
NSLog(@"add1的结果%@", resultDictionary);
array = [resultDictionary allKeys];
//所有的Book
[[WBBooksManager sharedInstance] getBooksCount]; //总数
NSLog(@"array%@ %d",array,num);
NSMutableDictionary *addDictionary1 = [[NSMutableDictionary alloc] init];
NSString *addName1 = [NSString stringWithFormat:@"wmmm"];
NSNumber *addNumber1 = [[NSNumber alloc] initWithInt:123];
[addDictionary1 setValue:addName1 forKey:@"name"];
[addDictionary1 setValue:addNumber1 forKey:@"list"];
//判断给出的Key对应的数据是否存在
if ([[WBBooksManager sharedInstance] isBookExistsForKey:@"Add1"]) {
//存在,则替换之
NSLog(@"存在,则替换之");
[[WBBooksManager sharedInstance] replaceDictionary:addDictionary1 withDictionaryKey:@"Add1"];
}else{//不存在,则写入
NSLog(@"不存在,则写入");
[[WBBooksManager sharedInstance] writePlist:addDictionary1 forKey:@"Add1"];
resultDictionary = [[NSMutableDictionary alloc] init];
[[WBBooksManager sharedInstance] readPlist:&resultDictionary];
NSLog(@"add1的结果%@", resultDictionary);
array = [resultDictionary allKeys];
//所有的Book
[[WBBooksManager sharedInstance] getBooksCount]; //总数
NSLog(@"array%@ %d",array,num);
addName1 = [NSString stringWithFormat:@"wmmm"];
addNumber1 = [[NSNumber alloc] initWithInt:123];
[addDictionary1 setValue:addName1 forKey:@"name"];
[addDictionary1 setValue:addNumber1 forKey:@"list"];
//更改key对应的数据
if ([[WBBooksManager sharedInstance] isBookExistsForKey:@"Add1"]) {
[[WBBooksManager sharedInstance] replaceDictionary:addDictionary1 withDictionaryKey:@"Add1"];
resultDictionary = [[WBBooksManager sharedInstance]readPlist];
NSLog(@"add1111的结果%@", resultDictionary);
//删除给出key对应的数据
if ([[WBBooksManager sharedInstance] isBookExistsForKey:@"Add1"]) {
[[WBBooksManager sharedInstance] removeBookWithKey:@"Add1"];
resultDictionary = [[WBBooksManager sharedInstance]readPlist];
NSLog(@"add1111的结果%@", resultDictionary);
//删除整个plist文件
if ([[WBBooksManager sharedInstance]isPlistFileExists]) {
[[WBBooksManager sharedInstance] deletePlist];
resultDictionary = [[WBBooksManager sharedInstance]readPlist];
NSLog(@"add1111的结果%@", resultDictionary);
//附上主要操作/*
1.NSMutableDictionary/NSDictionary
NSMutableDictionary *plistDictionary = [NSMutableDictionary dictionaryWithCapacity:0];
//将新的dictionary添加到olddictionary之后,若两dictionary具有相同的key,则更新
[oldDictionary addEntriesFromDictionary:[NSDictionary dictionaryWithDictionary: newDictionary]];
//给dictionary赋值
[plistDictionary setObject:oldDictionary forKey:key];
//移除某值
[plistDictionary removeObjectForKey:bookID];
//读取plistPath路径下的文件
NSMutableDictionary *nsmutableDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
2.NSMutableArray/NSArray
//判断数组中是否包含某值
[bookIDArray containsObject:bookID]
//移除某值
[contentArray replaceObjectAtIndex:j withObject:contentDictionary];
//写入数据
[plistDictionary writeToFile:plistPath atomically:YES];
阅读(...) 评论()

我要回帖

更多关于 ios读取plist文件 的文章

 

随机推荐