gpt4 book ai didi

ios - 从初始化程序返回而不初始化所有存储的属性 Xcode 8

转载 作者:可可西里 更新时间:2023-11-01 02:14:11 25 4
gpt4 key购买 nike

我升级到 Xcode 8,我的应用程序停止工作,我已经能够修复除这个错误之外的所有问题。我一直在网上寻找,但没有找到解决此错误的方法。任何帮助,将不胜感激。这是代码:

struct Party {
let itemRef:FIRDatabaseReference?
//
let userID:String!
let name:String!
let title:String!
let body:String!

init (userID:String, name:String, title:String = "", body:String) {
self.userID = userID
self.name = name
self.title = title
self.body = body
self.itemRef = nil
}

init (snapshot:FIRDataSnapshot) {
userID = snapshot.key
itemRef = snapshot.ref

if let titl = snapshot.value as? [String:AnyObject] {
for child in titl{
let shotKey = snapshot.children.nextObject() as! FIRDataSnapshot
if let title = child.value as? [String:AnyObject]{
let title = title["title"]
print(title)
}
}
}else{
title = "Failed To Display Title"
}

if let user = snapshot.value as? [String:AnyObject] {
for child in user{
let shotKey = snapshot.children.nextObject() as! FIRDataSnapshot
if let name = child.value as? [String:AnyObject]{
let name = name["name"]
print(name)
}
}
}else{
name = "Failed To Display Name"
}

if let partyBody = snapshot.value as? [String:AnyObject]{
for child in partyBody{
let shotKey = snapshot.children.nextObject() as! FIRDataSnapshot
if let body = child.value as? [String:AnyObject]{
let body = body["body"]
print (body)
}
}
}else{
body = "Failed To Display Time"
}
}

func toAnyObject() -> Any {
return ["title":title, "name":name, "body":body]
}
}

最佳答案

您的第二个 init(snapshot:) 函数没有设置 nametitlebody特定条件下的特性。

title 有以下代码:

if let titl = snapshot.value as? [String:AnyObject] {
for child in titl{
let shotKey = snapshot.children.nextObject() as! FIRDataSnapshot
if let title = child.value as? [String:AnyObject]{
let title = title["title"]
print(title)
}
}
}else{
title = "Failed To Display Title"
}

此代码仅在 else 子句中设置 title 属性。 if 部分中对 title 的四个引用是对名为 title 的局部变量的引用,而不是名为 title 的属性.因此编译器会提示您从未设置 title 属性,因为可能存在未设置该属性的代码路径。

namebody 也有同样的问题。

关于ios - 从初始化程序返回而不初始化所有存储的属性 Xcode 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39716239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com