如何更新 iPhone 应用程序中的数据
我创建了一个应用程序,数据存储在 plist 中。现在我可以通过哪些方式更新数据?也许如果我创建一个网页并从中检索数据而不是存储在 plist 中?或者有什么方法可以在插入 iTunes 时更新我的数据?或者还有其他建议吗?我想要实现的是在用户下载应用程序后更新我的数据的方法。
I create an app, with data store in plist. Now what are the ways I can update the data? Maybe if I create a webpage and retrieve my data from it instead of storing in plist? Or is there a way I can update my data when I plug into iTunes? Or any other suggestion? What I want to achieve is a way of updating my data once user have download the app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将尝试从
http://server.com/
获取my.plist
的副本,成功后会将其写入磁盘并指向localDictionary
,出错时它将尝试从磁盘读取(例如,在有连接时提前下载),如果磁盘上没有文件,它将创建一个空的NSMutableDictionary
。无论哪种方式,localDictionary
最终都会指向一个NSMutableDictionary
。这非常简单,每次运行代码时都会请求该文件。例如,您可以使用
NSFileModificationDate
(通过NSFileManger attributeOfItemAtPath:error:
获得)和NSDate
来确定是否需要更新。或者对于大文件版本控制也很有意义,拥有一个包含较大文件版本的小文件 - 从服务器获取小文件(例如使用NSString
),检查缓存的版本文件是否包含较低的版本号,如果是这样,则获取大文件。这始终取决于内容的刷新频率。查看完整文档: NSDictionary< /a>, NSString< /a>, NSFileManager< /a>, NSDate< /a>
This will try to obtain a copy of
my.plist
fromhttp://server.com/
, on success it will write it to the disk and pointlocalDictionary
to it, on error it will try to read from disk (e.g. downloaded earlier when there was a connection), if there is no file on disk it will create a emptyNSMutableDictionary
. Either waylocalDictionary
will point to aNSMutableDictionary
in the end.This is very simple and will request the file every time the code is run. You could e.g. use
NSFileModificationDate
(obtained byNSFileManger attributesOfItemAtPath:error:
) andNSDate
to determine if a update is necessary. Or for big files versioning would make sense as well, having a small file containing the version of the bigger file - getting the small file from the server (e.g. withNSString
), checking whether the cached version file contains a lower version number, if so get the big file. It always depends on how often the content is refreshed.See full documentation: NSDictionary, NSString, NSFileManager, NSDate
Plist 不容易修改,因为您必须将它们读回内存,进行更改并将它们写回文件。
要编写 plist,请查看 NSDictionary 或 NSArray 的 writeToFile:atomically: 方法。请记住,您的 plist 基本上是由 NSArrays、NSDictionaries、NSStrings 和 NSData 组成的 XML,并且您总是希望写出根节点。
或者寻找其他存储数据的方式:用户默认值或 sqlite 数据库。仅在某些情况下将数据存储在“云中”才有意义,例如您想在不同设备之间共享数据或想要远程更新数据。如果可能的话最好避免它。
Plists are not easily modifiable since you have to read them back into memory, make the changes and write them back to the file.
For writing the plists look at NSDictionary's or NSArray's
writeToFile:atomically:
method. Remember your plist is basically an XML made up from NSArrays, NSDictionaries, NSStrings and NSData, and you'll always want to write out the root node.Alternatively look for other ways of storing your data: user defaults or sqlite databases. Storing data 'in the cloud' makes sense only in some cases, like if you want to share it between different devices or want to update it remotely. It's best to avoid it if possible.