簡體   English   中英

如何創建自己的領域模型?

[英]How do I create my own realm model?

我正在嘗試使用目標C學習Realm模型。我想知道如何創建我們自己的.realm文件,以及如何在領域瀏覽器中查看領域文件。 以下是我的代碼。

-在標本中

#import <Foundation/Foundation.h>
#import <Realm/Realm.h>

@interface Specimen : RLMObject//: NSObject
    @property NSString *name;
    @property NSString *specDescription;
    @property NSInteger latitude;
    @property NSInteger longitude;
    @property NSDate *date;
@end

-在UIViewController.m中

#import "ViewController.h"
#import "Specimen.h"


@interface ViewController ()
{
    Specimen *first;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    first = [[Specimen alloc] init];

    first.name = @"first specimen";
    first.specDescription = @"some description";
    first.latitude = 12;
    first.longitude = 15;
    first.date = [NSDate date];

    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        [realm addObject:first];
    }];

    NSLog(@"Object added in realm");
}

構建成功。 控制台上也顯示了最后一個日志。但是我不知道在哪里可以看到“標本”對象,因為默認領域始終只有人和狗對象。 因此,我需要知道如何創建自己的領域,然后添加對象並通過領域瀏覽器訪問它。

來自Realm的官方文件

通過RLMRealmConfiguration完成配置諸如Realm文件存儲位置之類的操作。

例:

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

// Use the default directory, but replace the filename with the username
config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
                      URLByAppendingPathComponent:username]
                      URLByAppendingPathExtension:@"realm"];

// Set this as the configuration used for the default Realm
[RLMRealmConfiguration setDefaultConfiguration:config];

您可以具有多個配置對象,因此可以獨立控制每個Realm的版本,架構和位置:

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

// Get the URL to the bundled file
config.fileURL = [[NSBundle mainBundle] URLForResource:@"MyBundledData" withExtension:@"realm"];

// Open the file in read-only mode as application bundles are not writeable
config.readOnly = YES;

// Open the Realm with the configuration
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];

// Read some data from the bundled Realm
RLMResults<Dog *> *dogs = [Dog objectsInRealm:realm where:@"age > 5"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM