领域数据库对象似乎为空,但随后不是 [英] Realm database object seems empty, but then isn't

查看:61
本文介绍了领域数据库对象似乎为空,但随后不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将领域用于一个简单的小型项目.我使用的是框架的最新版本(昨天从Github编译)以及带有Swift 2.1的当前AppStore版本的Xcode.

我正在通过prepareForSegue()中的segue将Realm数据库对象从一个视图控制器移交给另一个视图控制器,以便对其进行编辑(并填写当前值).

以某种方式,目标视图控制器中的对象似乎有问题.当我在目标视图控制器中简单地print()对象(在viewDidLoad()或我打算在其中使用的任何方法中)时,该对象将在控制台中正确显示,如下所示:

Object {
name = asdf
value = 8
}

以此类推.

如果我尝试访问这些值(甚至在上一行或下一行中)并将它们分配给另一个变量,例如myValue = objectToEdit.value,我将得到0.

通过使用断点进入代码来调试代码,将显示调试器中的对象,根据类型的不同,所有值都为空(")或0.尽管如此,出于某种原因还是出于某种原因而将字符串从中抓出来,我可以使用myString = objectToEdit.name访问并打印它们,而调试器显示name = (String) ""并声称它为空.

当我在第一个视图中从数据库中获取数据库对象时,已经发生了相同的行为.立即想到的是,值确实为0,并且只保存了字符串,但print()方法则相反.

有人可以解释这种奇怪的行为,也许告诉我在什么地方我忽略了什么吗?我现在怀疑这是框架的一些内部问题,但是我不确定.如果需要,我当然会更新更多代码或回答问题.

解决方案

当我发现值奇怪地是我在模型定义中输入的默认"值时,我发现了问题.

由于某些原因,我没有在某些属性上使用dynamic声明,例如:

class myObject: Object {
    dynamic var name = ""
    var value = 0
}

这导致在视图控制器中创建的新对象具有这些标准值,并且我始终为0,但另一方面可以获取正确的字符串.

在变量声明中添加dynamic可以解决我的问题.

感谢您的回答.

I am using Realm for a small, simple project. I'm using the latest version of the framework (compiled from Github yesterday) and the current AppStore version of Xcode with Swift 2.1.

I am handing a Realm database object over from one view controller to another via a segue in prepareForSegue(), in order to edit it (and fill out the current values).

Somehow, there seems to be a problem with the object in the target view controller. When I simply print() the object in the target view controller (either in viewDidLoad() or any of the methods I plan to use it in), the object is properly shown in the console like so:

Object {
name = asdf
value = 8
}

and so on.

If I try to access the values (even in the previous or next line) and assign them to another variable like myValue = objectToEdit.value I get 0.

Debugging the code by stepping into it with breakpoints shows the object in the debugger with all values empty ("") or 0, depending on the type. Still, somehow Strings are grabbed out of it for some reason, I can access and print them with myString = objectToEdit.name, while the debugger shows name = (String) "", claiming it's empty.

The same behavior already occurs when I get the database object out of the database in the first view. The immediate thought would be, that the values are indeed 0 and only the strings were saved, but the print() method says otherwise.

Can anybody explain this odd behavior and maybe show me where I overlooked something? I now suspect it's some internal problem of the framework, but I'm not sure. I'll of course update with more code or answer questions, if needed.

解决方案

I have found the problem when I noticed that the values, oddly enough, were the "default" values I put in the definition of the model.

For some reason, I did not use the dynamic declaration on some of the properties, like so:

class myObject: Object {
    dynamic var name = ""
    var value = 0
}

This caused the new Object that was created in the view controller to have these standard values and I always got the 0, but on the other hand could get the correct Strings.

Adding dynamic to the variable declaration fixed my problems.

Thanks for your answers.

这篇关于领域数据库对象似乎为空,但随后不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆