ios - 使用 realm swift 简单的数据插入

标签 ios swift realm

我是 realm-swift 的新手。尝试在 Realm 数据库中进行简单的数据插入。 我收到以下警告:-

WARNING: An RLMRealm instance was deallocated during a write transaction and
all pending changes have been rolled back. Make sure to retain a reference
to the RLMRealm for the duration of the write transaction. 

这是我的代码:-

    //
//  Dog.swift
//  RealmDemo
//
//  Created by RIPA SAHA on 20/07/16.
//  Copyright © 2016 RIPA SAHA. All rights reserved.
//

import Foundation

class Dog {

    dynamic var name = ""

    dynamic var age = ""

}



//
//  ViewController.swift
//  RealmDemo
//
//  Created by RIPA SAHA on 19/07/16.
//  Copyright © 2016 RIPA SAHA. All rights reserved.
//

import UIKit
import RealmSwift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



        let myDog = Dog()
        myDog.name = "puppies"
        myDog.age = "5"


        // Get the default Realm
        let realm = try! Realm()

        // Add to the Realm inside a transaction
        realm.beginWrite()


             realm.add(myDog)


        /*realm.add(myDog)
        realm.commitWrite()*/



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

任何帮助将不胜感激...

最佳答案

按照这个例子:

import Foundation
import RealmSwift

class Dog : Object {
  dynamic var name = ""
  dynamic var age = ""
}

关于ios - 使用 realm swift 简单的数据插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38495749/

相关文章:

ios - 验证后未从所有 View 中删除 View

ios - iOS8 中的多行标签

c# - 删除托 pipe 列表的最佳方法。一对多/父子关系

ios - tableView 和 Realm 数据库之间的差异

objective-c - 带有 NSString 数组的 RLMObject

ios - UITableView 没有滚动

ios - 来自 plist iOS 的数组

swift - 如何使用 Firebase 和 Swift 检索所有用户的数据

Swift 从输入坐标添加引脚位置

json - 如何将 JSON 解析为 UITableview