如何将类对象数组保存到Plist中(Iphone开发) [英] How can I save Array of Class Objects in to a Plist(Iphone Development)
问题描述
我有一个问题涉及将一个Class对象数组保存到一个plist然后再将它取回。
I have a question related to save an array of Class objects to a plist And then get it back.
例如我有一个带有两个的类A Nsstring和一些int值。
For example i have a class "A" with two Nsstring and some int values.
我有一个A类对象数组。
I have an array of Class A objects.
我怎么能保存这个数组到plist。
How can i save this array in to plist.
我可以完成将一个简单的数组(字符串数组)存入Plist。没有错误。
I can Done store a simple Array(Array of Strings) in to Plist.There is No Error.
但是当我存储这个对象数组时,它无法完成。
But When I store this "Array of Objects" It cant be done.
在此先感谢。
推荐答案
首先将您的NSArray转换为NSDictionary,如此处。
First convert your NSArray to NSDictionary as given here.
然后使用 NSDictionary 的 writeToFile:atomically:
将其存储为Plist。
Then use NSDictionary's writeToFile:atomically:
to store that as Plist.
为此,您的课程必须符合 NSCoding 协议。
For this, your class must be conform to NSCoding protocol.
或者,您可以将NSArray本身直接写入plist而不转换为NSDictionary。但无论如何,该类应该符合NSCoding协议。
Or otherwise, you can write your NSArray itself directly to plist without converting to NSDictionary. But anyhow, the class should conform to NSCoding protocol.
这篇关于如何将类对象数组保存到Plist中(Iphone开发)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!