2

I upgraded to Xcode 8 and my app stopped working and I have been able to fix everything but this one error. I have been looking online and I have not found a fix for this error. Any Help would be appreciated. Here is the code:

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]
    }
}

1 Answer 1

1

Your second init(snapshot:) function doesn't set the name, title, and body properties under certain conditions.

You have this code for the 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"
}

This code only sets the title property in the else clause. The four references to title inside the if part are references to local variables named title, not the property named title. So the compiler complains you never set the title property because there is a possible code path where it isn't set.

You have the same issue for name and body.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.