将文件写入plist和NSFilemanager之间的区别

时间:2021-07-18 13:23:02

Is there any difference between writing an objects(ex array types and NSObject types) to plist and NSFileManager.

将对象(ex数组类型和NSObject类型)写入plist和NSFileManager之间是否有任何区别?

if so can i write 1000's of images data and mp3 songs to plist.

如果可以,我可以写1000张图像数据和mp3歌曲到plist。

2 个解决方案

#1


1  

NSFileManager is used to do things like copy a file, remove a file, and move a file. You wouldn't use NSFileManager to write out a new file. It's only good for working with existing files. It isn't comparable to a plist in the sense of using a plist vs. NSFileManager.

NSFileManager用于执行复制文件,删除文件和移动文件等操作。您不会使用NSFileManager写出新文件。它只适用于处理现有文件。在使用plist和NSFileManager的意义上,它与plist不具有可比性。

However, if the question you're trying to answer is should I store all my data in a plist or in separate files then that depends. If you're going to store 1000s of images and mp3s, then you definitely do not want to store them all in a single plist. Plists are an inefficient format for storing and updating large amounts of information, both in terms of speed and memory. For example, if you wanted to update a single string in your plist, you have to read the entire plist into memory, update it, and then write the entire plist back to disk. You cannot update just a portion of the plist using the standard plist functions provided by Foundation. If your plist contains all your image and mp3 data, it's going to be really slow.

但是,如果您要回答的问题是我应该将所有数据存储在plist中还是存储在单独的文件中,那么这取决于。如果您要存储1000个图像和mp3,那么您绝对不希望将它们全部存储在一个plist中。 Plists是一种低效的格式,用于存储和更新大量信息,包括速度和内存。例如,如果要更新plist中的单个字符串,则必须将整个plist读入内存,更新它,然后将整个plist写回磁盘。您无法使用Foundation提供的标准plist函数更新plist的一部分。如果你的plist包含你的所有图像和mp3数据,它将会非常慢。

You may be able to get away with using a plist as a manifest for your images and mp3s which are stored as separate files on disk, but even that can get slow. I'd recommend using SQLite or Core Data instead for the manifest of files and then keeping each image and mp3 as a separate file in the file system. Or, if you don't need to store any metadata with each item, you don't need the manifest at all. If you do end up going with a plist for your manifest, make sure to save the plist as a binary plist using the NSPropertyListBinaryFormat_v1_0 option. This will make the plist take up less space on disk and de-serialize faster when you read it again.

您可以使用plist作为图像和mp3的清单,它们作为单独的文件存储在磁盘上,但即使这样也会变慢。我建议使用SQLite或Core Data代替文件清单,然后将每个图像和mp3保存为文件系统中的单独文件。或者,如果您不需要为每个项目存储任何元数据,则根本不需要清单。如果最终使用plist作为清单,请确保使用NSPropertyListBinaryFormat_v1_0选项将plist保存为二进制plist。这将使得plist在磁盘上占用更少的空间,并在再次读取时更快地反序列化。

#2


2  

As a plist file is an XML (mostly text) file you must archive your data before writing and unarchive after reading (see NSKeyedArchiver here).

由于plist文件是XML(主要是文本)文件,因此您必须在写入之前归档数据,并在读取之后将其取消归档(请参阅此处的NSKeyedArchiver)。

NSFileManager, however is a wrapper around generic filesystem operations and there is no need to marshall the data into text in order to store it. The stored data will therefore be much smaller, much quicker to read/write and is the obvious choice.

然而,NSFileManager是通用文件系统操作的包装器,并且不需要将数据编组为文本以便存储它。因此,存储的数据将更小,读取/写入更快,并且是显而易见的选择。

#1


1  

NSFileManager is used to do things like copy a file, remove a file, and move a file. You wouldn't use NSFileManager to write out a new file. It's only good for working with existing files. It isn't comparable to a plist in the sense of using a plist vs. NSFileManager.

NSFileManager用于执行复制文件,删除文件和移动文件等操作。您不会使用NSFileManager写出新文件。它只适用于处理现有文件。在使用plist和NSFileManager的意义上,它与plist不具有可比性。

However, if the question you're trying to answer is should I store all my data in a plist or in separate files then that depends. If you're going to store 1000s of images and mp3s, then you definitely do not want to store them all in a single plist. Plists are an inefficient format for storing and updating large amounts of information, both in terms of speed and memory. For example, if you wanted to update a single string in your plist, you have to read the entire plist into memory, update it, and then write the entire plist back to disk. You cannot update just a portion of the plist using the standard plist functions provided by Foundation. If your plist contains all your image and mp3 data, it's going to be really slow.

但是,如果您要回答的问题是我应该将所有数据存储在plist中还是存储在单独的文件中,那么这取决于。如果您要存储1000个图像和mp3,那么您绝对不希望将它们全部存储在一个plist中。 Plists是一种低效的格式,用于存储和更新大量信息,包括速度和内存。例如,如果要更新plist中的单个字符串,则必须将整个plist读入内存,更新它,然后将整个plist写回磁盘。您无法使用Foundation提供的标准plist函数更新plist的一部分。如果你的plist包含你的所有图像和mp3数据,它将会非常慢。

You may be able to get away with using a plist as a manifest for your images and mp3s which are stored as separate files on disk, but even that can get slow. I'd recommend using SQLite or Core Data instead for the manifest of files and then keeping each image and mp3 as a separate file in the file system. Or, if you don't need to store any metadata with each item, you don't need the manifest at all. If you do end up going with a plist for your manifest, make sure to save the plist as a binary plist using the NSPropertyListBinaryFormat_v1_0 option. This will make the plist take up less space on disk and de-serialize faster when you read it again.

您可以使用plist作为图像和mp3的清单,它们作为单独的文件存储在磁盘上,但即使这样也会变慢。我建议使用SQLite或Core Data代替文件清单,然后将每个图像和mp3保存为文件系统中的单独文件。或者,如果您不需要为每个项目存储任何元数据,则根本不需要清单。如果最终使用plist作为清单,请确保使用NSPropertyListBinaryFormat_v1_0选项将plist保存为二进制plist。这将使得plist在磁盘上占用更少的空间,并在再次读取时更快地反序列化。

#2


2  

As a plist file is an XML (mostly text) file you must archive your data before writing and unarchive after reading (see NSKeyedArchiver here).

由于plist文件是XML(主要是文本)文件,因此您必须在写入之前归档数据,并在读取之后将其取消归档(请参阅此处的NSKeyedArchiver)。

NSFileManager, however is a wrapper around generic filesystem operations and there is no need to marshall the data into text in order to store it. The stored data will therefore be much smaller, much quicker to read/write and is the obvious choice.

然而,NSFileManager是通用文件系统操作的包装器,并且不需要将数据编组为文本以便存储它。因此,存储的数据将更小,读取/写入更快,并且是显而易见的选择。