简体   繁体   English

为什么这个带有可选值的结构什么都不返回?

[英]Why this struct with optional values returns nothing?

I visited this thread to know about optionals and I saw a quote from the docs, which I quote again. 我访问了该线程以了解可选内容,并且看到了文档中的引用,我再次引用了该引用。

If your custom type has a stored property that is logically allowed to have “no value”—perhaps because its value cannot be set during initialization, or because it is allowed to have “no value” at some later point—declare the property with an optional type. 如果您的自定义类型的存储属性在逻辑上被允许为“无值”(可能是因为初始化期间无法设置其值,或者因为稍后某个时候允许其无值),请使用可选类型。 Properties of optional type are automatically initialized with a value of nil, indicating that the property is deliberately intended to have “no value yet” during initialization. 可选类型的属性会自动使用nil值进行初始化,这表明该属性在初始化过程中故意有“无值”的含义。

So I have the same condition. 所以我有同样的条件。 I have some variables that may have some values or not. 我有一些可能有一些值或没有值的变量。 So here's my struct 这是我的结构

struct Notification {
    var type : String?
    var dp : String?
    var name : String?
    var postImage : String?
    var whomenc : String?
}

So I'm trying to create a struct array but whenever the struct is initialized, I get nothing in return 所以我试图创建一个结构体数组,但是每当结构体被初始化时,我一无所获

obj.forEach {
    guard let type = $0["type"] as? String else {return}
    print("type = \(type)")
    guard let dp = $0["dp"] as? String else {return}
    print("dp = \(dp)")
    guard let name = $0["name"] as? String else {return}
    print("name = \(name)")
    guard let postimg = $0["postimg"] as? String else {return}
    print("postimg = \(postimg)")
    guard let whomenc:String = $0["whomenc"] as? String else {return}
    print("whomenc = \(whomenc)")
    let notification = Notification(type: type, dp: dp, name: name, postImage: postimg, whomenc: whomenc)
    self.notifiArray.append(notification)
    print("notifiArray.count = \(self.notifiArray.count)") // this satement doesn't gets executed.
}

And this is what I get in the log 这就是我在日志中得到的

type = commentCommented
dp = default.jpg
name = testt
postimg = 69663rocketleague32bitdx914102018180636.mp4
type = commentCommented
dp = default.jpg
name = testt
postimg = 69663rocketleague32bitdx914102018180636.mp4
type = followed
dp = default.jpg
name = bott1
type = followed
dp = default.jpg
name = bott2

I tried printing the notification variable and it is empty. 我尝试打印通知变量,它为空。 I tried printing the notifiArray which returns as [] 我尝试打印notifiArray返回为[]

The array is declared as 该数组声明为

var notifiArray = [Notification]()

Any idea why this behavior? 任何想法为什么这种行为?

This statement is not succeeding: 该语句不成功:

guard let whomenc:String = $0["whomenc"] as? String else {return}

either because the key "whomenc" doesn't exist or the type isn't a String . 或者是因为键"whomenc"不存在,或者类型不是String The guard statement then return s from the closure and does nothing more for that dictionary. 然后, guard语句从闭包return s,并且对该字典不执行任何操作。

Since you can create a Notification even if some values are nil , you could remove the guard statements and pass the (possibly nil ) values to the Notification initializer: 由于即使某些值为nil ,您也可以创建Notification ,因此可以删除guard语句,并将(可能为nil )值传递给Notification初始化器:

obj.forEach {
    let type = $0["type"] as? String
    print("type = \(type ?? "nil")")
    let dp = $0["dp"] as? String
    print("dp = \(dp ?? "nil")")
    let name = $0["name"] as? String
    print("name = \(name ?? "nil")")
    let postimg = $0["postimg"] as? String
    print("postimg = \(postimg ?? "nil")")
    let whomenc = $0["whomenc"] as? String
    print("whomenc = \(whomenc ?? "nil")")
    let notification = Notification(type: type, dp: dp, name: name, postImage: postimg, whomenc: whomenc)
    self.notifiArray.append(notification)
    print("notifiArray.count = \(self.notifiArray.count)") // this satement doesn't gets executed.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM