菜鸟教程小白 发表于 2022-12-13 14:10:35

ios - 如何在 Realm 中存储 NSObject 类?


                                            <p><p>我正在将 <code>Realm</code> 集成到我的应用程序中。我需要知道如何将 <code>custom class</code> 对象存储在 <code>RLMObject 子类</code> 中。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Realm 中唯一允许保存的属性是,对应 <a href="https://realm.io/docs/objc/latest/#supported-types" rel="noreferrer noopener nofollow">the Documentation</a> :</p>

<blockquote>
<p>Realm supports the following property types: BOOL, bool, int,
NSInteger, long, long long, float, double, NSString, NSDate, NSData,
and NSNumber tagged with a specific type.</p>
</blockquote>

<p>如果您需要存储另一个对象(您提到的是自定义类的实例),官方支持的方法是创建 RLMObject 的子类(假设您使用的是 Objective-C),并且在父对象中的属性,创建引用,就像在文档中提到的示例一样:</p>

<pre><code>#import &lt;Realm/Realm.h&gt;

@class Person;

// Dog model
@interface Dog : RLMObject
@property NSString *name;
@property Person   *owner;
@end
RLM_ARRAY_TYPE(Dog) // define RLMArray&lt;Dog&gt;

// Person model
@interface Person : RLMObject
@property NSString             *name;
@property NSDate               *birthdate;
@property RLMArray&lt;Dog *&gt;&lt;Dog&gt; *dogs;
@end
RLM_ARRAY_TYPE(Person) // define RLMArray&lt;Person&gt;
</code></pre>

<p>在这个例子中,我们在 Dog 模型中有一个属性 <code>owner</code>。我想这就是你要找的。</​​p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Realm 中存储 NSObject 类?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39591506/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39591506/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Realm 中存储 NSObject 类?