菜鸟教程小白 发表于 2022-12-11 17:54:40

ios - 将自定义对象存储为 NSDictionary 与 NSData


                                            <p><p>我正在阅读一些关于在 NSUserDefaults 中存储自定义对象的教程。我不知道在将模型转换回 <code>NSDictionary</code> 并存储字典与使用 <code>NSKeyedArchiver</code> 存储对象后,存储模型的更好方法和/或好处是什么在 <code>NSUserDefaults</code> 中。根据我的理解,您必须设置 <code>encodeWithCoder</code> 和 <code>initWithCoder</code> 方法,这将使您能够设置各种键的值并将所有内容基本上转换为 <code>NSData</code>。将模型转换回 <code>NSDictionary</code> 也将遵循相同的步骤。 </p>

<p>那么使用一种方法比另一种方法有什么好处,或者如果一种方法可能导致某些地方出现问题?</p>

<p>使用 <code>NSKeyedArchiver</code> 将执行以下操作:</p>

<pre><code>- (void)encodeWithCoder:(NSCoder *)encoder {
    ;
    ;
}
</code></pre>

<p>并将模型转换回 <code>NSDictioanry</code>:</p>

<pre><code>-(NSDictionary *)dictionaryWithModel:(Model *)model{

    NSDictionary *dictionary = @{
                                 @&#34;dict&#34;:,
                                 @&#34;string&#34;:self.test
                                 };

    return dictionary;
}
</code></pre>

<p>在 <code>NSUserDefaults</code> 中存储对象时,基本上哪个会更好? </p>

<p><strong>编辑:</strong>所以根据答案中链接的文档,我们应该使用 <code>NSDictionary</code> 将对象存储在 <code>NSUserDefaults</code> 中(性能更好),那为什么苹果推荐使用<code>NSKeyedArchiver</code>?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p> <a href="https://stackoverflow.com/a/38324693/2284065" rel="noreferrer noopener nofollow">Here</a>是我对另一个类似问题的回答。</p>

<p>你可以这样设置对象:</p>

<pre><code>NSUserDefaults *currentDefaults = ;
NSData *data = ;
;
;
</code></pre>

<p>对于像这样获取对象:</p>

<pre><code>NSData *data = ;
SavingBean *token = ;
</code></pre>

<p>对于自定义类,您必须在 bean 类中编辑此方法:</p>

<pre><code>- (void)encodeWithCoder:(NSCoder *)encoder
{
    ;
    ;
}

-(id)initWithCoder:(NSCoder *)decoder
{
    self = ;
    if(self)
    {
      self.userName = ;
      self.passWord = ;
    }
    return self;
}
</code></pre>

<p> <a href="https://stackoverflow.com/a/2315972/2284065" rel="noreferrer noopener nofollow">Here</a>是投票最多的类似答案。你也可以从那里得到好东西。</p>

<p>您也可以使用 <a href="https://itunes.apple.com/in/app/json-accelerator/id511324989?mt=12" rel="noreferrer noopener nofollow">JSON Accelerator</a>创建 bean 类。这是一个非常简单而强大的工具。</p>

<p><strong>编辑:</strong></p>

<blockquote>
<p>The NSUserDefaults class provides convenience methods for accessing
common types such as floats, doubles, integers, Booleans, and URLs. A
default object must be a property list, that is, an instance of (or
for collections a combination of instances of): NSData, NSString,
NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any
other type of object, you should typically archive it to create an
instance of NSData.</p>

<p>Values returned from NSUserDefaults are immutable, even if you set a
mutable object as the value. For example, if you set a mutable string
as the value for &#34;MyStringDefault&#34;, the string you later retrieve
using stringForKey: will be immutable.</p>

<p>Note: The user defaults system, which you programmatically access
through the NSUserDefaults class, uses property lists to store objects
representing user preferences. This limitation would seem to exclude
many kinds of objects, such as NSColor and NSFont objects, from the
user default system. But if objects conform to the NSCoding protocol
they can be archived to NSData objects, which are property
list–compatible objects. For information on how to do this, see
““Storing NSColor in User Defaults”“; although this article focuses on
NSColor objects, the procedure can be applied to any object that can
be archived.</p>
</blockquote>

<p> <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html</a> </p>

<p><strong>编辑:</strong></p>

<blockquote>
<p>When defining your app’s preferences, it is better to use simple
values and data types whenever possible. The preferences system is
built around property-list data types such as strings, numbers, and
dates. Although you can use an NSData object to store arbitrary
objects in preferences, doing so is not recommended in most cases.</p>

<p>Storing objects persistently means that your app has to decode that
object at some point. In the case of preferences, a stored object
means decoding the object every time you access the preference. It
also means that a newer version of your app has to ensure that it is
able to decode objects created and written to disk using an earlier
version of your app, which is potentially error prone.</p>

<p>A better approach for preferences is to store simple strings and
values and use them to create the objects your app needs. Storing
simple values means that your app can always access the value. The
only thing that changes from release to release is the interpretation
of the simple value and the objects your app creates in response.</p>
</blockquote>

<p> <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html#//apple_ref/doc/uid/10000059i-CH2-SW2" rel="noreferrer noopener nofollow">https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html#//apple_ref/doc/uid/10000059i-CH2-SW2</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将自定义对象存储为 NSDictionary 与 NSData,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40189115/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40189115/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将自定义对象存储为 NSDictionary 与 NSData