Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将零分配给RealmOptional<;Int>;_Ios_Swift_Realm - Fatal编程技术网

Ios 如何将零分配给RealmOptional<;Int>;

Ios 如何将零分配给RealmOptional<;Int>;,ios,swift,realm,Ios,Swift,Realm,我有类似这样的模型类,不知道如何将swift可选值分配给RealmoOptional,这迫使我打开可选值。为什么我需要在分配给RealmOptional之前展开可选项 import Foundation import RealmSwift class Menu:Object { var id = RealmOptional<Int>() func initWithJSON(json: Any) { let jsonResult = json a

我有类似这样的模型类,不知道如何将swift可选值分配给RealmoOptional,这迫使我打开可选值。为什么我需要在分配给RealmOptional之前展开可选项

import Foundation
import RealmSwift

class Menu:Object {

    var id = RealmOptional<Int>()

    func initWithJSON(json: Any) {

        let jsonResult = json as! [String: Any]

        id              = jsonResult["id"] as? RealmOptional<Int>

    }
}
将swift选项分配给RealmoOptional的最佳选项是什么

import Foundation
import RealmSwift

class Menu:Object {

    var id = RealmOptional<Int>()

    func initWithJSON(json: Any) {

        let jsonResult = json as! [String: Any]

        id              = jsonResult["id"] as? RealmOptional<Int>

    }
}
<代码>导入基础 导入RealmSwift 类菜单:对象{ var id=realmopational() func initWithJSON(json:Any){ 让jsonResult=json为![String:Any] id=jsonResult[“id”]作为真实选项 } } 下面是迫使我打开可选文件的错误消息

您可以尝试

id.value = (jsonResult["id"] as? Int  ?? 0 ) 
宣布

var id = RealmOptional<Int>()

您不应该直接修改
RealmOptional
属性,您应该始终修改其
属性,该属性包含基础
可选
值。因此,您应该始终将
RealmOptional
s声明为不可变,因为
RealmOptional
是引用类型,因此您仍然可以修改其
属性,而无需修改实际引用

class Menu:Object {

    let id = RealmOptional<Int>()

    func initWithJSON(json: Any) {
        let jsonResult = json as! [String: Any]
        id.value = jsonResult["id"] as? Int
    }
}

您可以使用解析的
JSON
初始化
Realm.Object
,根本不需要实现自定义方法。此外,您应该设置
RealmOptional
的属性,而不是用新值替换它。因为这不起作用。@Sh_Khan,这会起作用,但如果我已经将属性定义为可选的,为什么我总是需要给出一个值,它可以是nil(这就是为什么存在可选项)
RealmOptional
是允许核心
领域
代码(用ObjC编写)的hack类型访问
对象
子类型的可选变量。它本身不是
Swift.Optional
。@user28434,那么分配值的最佳选项是什么。>
您应该设置RealmOptional的.value属性
此属性是
Swift.Optional
class Menu:Object {

    let id = RealmOptional<Int>()

    func initWithJSON(json: Any) {
        let jsonResult = json as! [String: Any]
        id.value = jsonResult["id"] as? Int
    }
}
extension RealmOptional: Codable where Value:Codable {
    public convenience init(from decoder: Decoder) throws {
        do {
            let value = try decoder.singleValueContainer().decode(Value.self)
            self.init(value)
        } catch {
            if case DecodingError.valueNotFound(_, _) = error {
                self.init(nil)
            } else {
                throw error
            }
        }
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(value)
    }
}

class Menu: Codable {
    let id = RealmOptional<Int>()
}