首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用远程.plist覆盖本地.plist文件

使用远程.plist覆盖本地.plist文件
EN

Stack Overflow用户
提问于 2012-06-09 03:43:10
回答 2查看 1.8K关注 0票数 1

我的应用程序在启动时加载一个本地list.plist文件。

然后它有一个从我的网站获取.plist文件的远程版本的refreshTable按钮。

代码语言:javascript
复制
App Launch
    local list.plist loads
       user hits refreshList button
          local list.plist is overwritten by remote list.plist
             local list.plist updated until remote list.plist updates again

初始化数据的方法:

代码语言:javascript
复制
    //Remote data 
    list = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com/list.plist"]];
    NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
    sortedList = [[list sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain];

    //Local data
    NSString *localPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
   localList = [[NSMutableArray alloc] initWithContentsOfFile:localPath];
    NSSortDescriptor *localDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
    localSortedList = [[localList sortedArrayUsingDescriptors:[NSArray arrayWithObject:localDescriptor]] retain];

这是刷新的方法:

代码语言:javascript
复制
- (void) refreshTable:(id)sender

{  
   //Remote .plist 
   list = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com/list.plist"]];
    NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
   sortedList = [[list sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain];
    [self.tableView reloadData];
    //now write remote plist to local plist
}

下载完远程plist后,如何重写本地plist?

我正在考虑清空包含本地plist的本地数组,并用远程数组填充它,我是这样做的:

我以我认为的方式解决了问题:

代码语言:javascript
复制
 //Remote .plist 
    list = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://phillipapps.com/mntlion/list.plist"]];
    NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
    sortedList = [[list sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain];

    NSLog(@"list: %@",list);
    [localList removeAllObjects];
    [localList addObjectsFromArray:list];
    localSortedList = [[localList sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain];
    NSLog(@"locallist: %@",localList);
    [self.tableView reloadData];

它可以工作,但是我如何用list的内容重写localList

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-11 07:41:18

所以…经过几个小时的闲聊,我们把问题解决了。

代码语言:javascript
复制
- (NSArray*)readPlist
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"localFile.plist"];

    NSFileManager *fMgr = [NSFileManager defaultManager];
    if (![fMgr fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"plist"];
    }
    return [NSArray arrayWithContentsOfFile:plistPath];
}

- (void)writePlist:(NSArray*)arr
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"localFile.plist"];

    NSFileManager *fMgr = [NSFileManager defaultManager];
    if (![fMgr fileExistsAtPath:plistPath])
        [fMgr removeItemAtPath:plistPath error:nil];

    [arr writeToFile:plistPath atomically:YES];
}

初始化ivar:

代码语言:javascript
复制
self.plistArray = [self readPlist];

从服务器加载新的plist后,您必须调用以下代码:

代码语言:javascript
复制
self.plistArray = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://mywebsite.com/list.plist"]];
[self writePlist:plistArray]; // e.g. for caching
[self.tableView reloadData];
票数 4
EN

Stack Overflow用户

发布于 2012-06-09 07:38:57

我做的几乎就是你在我的应用程序中要做的事情。您不能写入NSBundle,因此步骤如下所示:

  1. 尝试从缓存目录加载plist,如果成功,请转到3.
  2. 从捆绑包加载plist
  3. 检查加载的plist是否为最新(或者,通过按下新的plist
  4. 触发进一步的步骤保存到缓存目录

代码看起来像这样:

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cache = [paths objectAtIndex:0];

ratios = [NSDictionary dictionaryWithContentsOfFile:[cache stringByAppendingPathComponent:@"devices.plist"]];
if (ratios == nil) {
    ratios = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"devices" ofType:@"plist"]];
}

NSString *device = [[UIDevice currentDevice] machine];
NSDictionary *d = [ratios objectForKey:device];
if (d!=nil) {
    pixelInchRatio = [[d objectForKey:@"pixelInchRatio"] doubleValue];
    bezelWidth = [[d objectForKey:@"bezelWidth"] doubleValue];
    bezelHeight = [[d objectForKey:@"bezelHeight"] doubleValue];
} else if (fetch) {
    [[[[RulerDimensionDownload alloc] init] autorelease] startDownload];
}

然后在下载器中保存如下所示:

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cache = [paths objectAtIndex:0];

[[NSFileManager defaultManager] createDirectoryAtPath:cache withIntermediateDirectories:YES attributes:nil error:nil];
[ratios writeToFile:[cache stringByAppendingPathComponent:@"devices.plist"] atomically:YES];

以下是一些解释:

每个示例中的前几行、路径和缓存定义只是获取应用程序的缓存目录的位置。我用它来存储最新的版本我是我的plist。

因此,在加载代码中,我首先尝试从缓存目录加载plist。如果失败(ratios为空),我就从我的应用程序包(这是我随应用程序附带的plist的版本)中加载它。

在此之后,我检查plist是否具有所需的信息。( plist对每种设备类型都有一个定义。如果设备不在plist中,那么我知道我需要尝试更新plist)

如果plist已过期,我将使用我编写的类RulerDimensionDownload开始下载。下载完成后,我将文件保存到缓存目录中。然后,下一次需要加载plist时,它将首先被加载,而不会再查看已发送的plist。(我还发送了带有新plist的通知)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10955130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档