写入后删除类的可选属性

时间:2017-01-31 23:48:00

标签: ios swift realm

我正在开发一个应用程序,用Swift 3.0.2和Realm 2.4.2编写,目标是iOS 10.0。

我有这个数据模型:

    function Person(name, friends) {
        // friends is a list of strings 
        var say = function (sentence) {
            console.log(name + ' says: ' + sentence);
        };

        Person.prototype.sayHello = function (otherName) {
            this.say('hello ' + otherName + '!');

        };

        this.saluteFriends = function () {
            friends.forEach(function (friend) {
                sayHello(friend);
            });
        };

        this.sayHelloLater = function (delay, otherName) {
            setTimeout(function () {
                this.sayHello(otherName);
            }, delay);
        };
    }

    var frnds = ["sam", "mathew"];
    var fcall = new Person("alan", frnds);
    fcall.saluteFriends();

为了保存用户的数据,我使用以下代码:

class User: Object {

    var company: Company? = Company()
    dynamic var password = ""
    dynamic var email = ""

    func persist() {
        let realm = try! Realm()
        try! realm.write {
            realm.add(self) 
        } 
        print(company)
    }
}

class Company: Object {
    dynamic var id = ""
    dynamic var name = ""
}

有人可以解释为什么会这样做吗?

1 个答案:

答案 0 :(得分:0)

必须使用For Each curr In wb.Worksheets(1).UsedRange If InStr(1, curr.Value, searchString) > 0 Then MsgBox ("searchString appears once") End If Next 修饰符声明您的company属性,以使Realm Swift能够提供从底层Realm文件中检索值的getter。如果没有dynamic修饰符,Swift编译器将生成对底层实例变量的访问,该变量不包含从Realm文件加载的对象的任何数据。