2017-10-05 69 views
0

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

-In Specimen.h

#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 

-In 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"); 
} 

構建得到成功。最後一個日誌也顯示在控制檯上。但我不明白在哪裏可以看到Specimen對象,因爲默認域總是隻有人和狗對象。 所以我需要知道如何創建我自己的領域,然後添加對象並通過領域瀏覽器訪問它。

回答

0

從領域official document

等,其中的境界文件存儲配置的東西是通過 RLMRealmConfiguration做 。

例子:

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]; 

你可以有多個配置對象,這樣你就可以控制各個領域獨立的 版本,架構和地點:

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"]; 
+0

謝謝!:)會試試這個。 – pz26

相關問題