1

plists と xml ファイルを使用する理由 いくつかの値のみを保存したい場合、プレーン テキスト ファイルを使用しても問題ありませんか? それとも、Objective-C のベスト プラクティスに反しますか?

- - - -編集 - - - -

別記事にするか迷ったので、ここに載せておきます…

ユーザーがカップケーキをデザインし、好み (色、味、サイズ) で保存できるアプリを作成している場合、どの方法を使用すればよいですか? ユーザーが何百ものデザインを作成することはないと思いますが、必然的に多数のデザインを作成するユーザーもいます。

4

3 に答える 3

3

私はそれでいいと思いますが、plistファイルを読むのがどれほど簡単かを考えると、私の質問はなぜわざわざ?

于 2012-06-01T21:07:05.920 に答える
2

The main reason that plists are commonly used is because the native APIs can handle these easily. You can load a NSArray/NSDictionary directly from a plist with one command.

SQL databases are used when you are going to have many occurrences of similar data. For example, if you need to record contacts for a social app, you would use a database that could contain the id, name, age, gender, phone number, email, etc.

Other than these, there are custom binary formats, but these are specialized for whatever project is being worked on. Depending on what you need to accomplish, a text file could work for you, but there may be better answers. There is nothing wrong with using text files, but these are not commonly used as you would have to write your own methods to parse them. Really, I would need to know more about what type of data you will be storing before I could tell you which option would be best.

An edit to answer your edit:

For this, the best thing to do would be to use a SQL database, as every cupcake is going to have various properties, such as name, type of cake mix used, type of frosting, color of frosting, sprinkles yes/no, etc. This is perfect for a SQL database, because dbs have named colums (ie "name", "mixType", etc), and each row in the table will have different values for each of these columns.

This could also be implemented with a plist, but it wouldn't be as efficient, and it would use more disk space (although not much if youre just using it for cupcakes). I assume that you have a Cupcake class, so you could just implement a load function like this:

+(id)cupcakeWithContentsOfFile:(NSString*)file {
    if((self = [super init])) {
        NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:file];
        self.flavor = [plist objectForKey:@"flavor"];
        // Etc.
    }
    return self;
}
于 2012-06-01T21:11:22.890 に答える
2

これは興味深い質問です。私は実際に、アプリが使用するデータに単純なテキストファイルを実際に使用するアプリを持っています。元のコードは Windows / Mac プログラムからのものであり、データ ファイルの一貫性を維持したかったため、Windows は pfile タイプの操作を提供しません。ファイルを読み込むためにすべてのコードを Windows で書きましたが、すべて ANSI-C だったので、MacOS X に非常にうまく転送されました。

iPhone や iPad への移植に関して言えば、コードを移植してから、データ ファイルを PList 形式に書き直すだけでなく、PList 読み取りコードも簡単に書き直すことができました。

ウィンドウがバインドされていないゼロから始めたすべてのアプリでは、PLists を使用しました。

これは実際に私が行き来する問題です。PList および/または XML の読み取りと処理は、適切に設計されたテキスト ファイル形式ほどパフォーマンスが高くないことは確かです。必要である場合とそうでない場合があるすべての余分なタグが原因で両方が肥大化するからです。多くの種類のデータを含む 1 つのデータ ファイルを開発しようとしている場合は、これよりも単純なアプローチになる可能性がありますが、データサイズを減らし、実行速度を上げたい場合は、独自のファイルをフォーマットする方がはるかに優れている可能性があります。これが決定的な答えではないことはわかっています。

結局のところ、これは「ベスト プラクティス」のようなものではなく、実際には好みのタイプのものです。

于 2012-06-01T21:18:21.790 に答える