mavericklab mavericklab - 1 month ago 10x
Swift Question

Swift 3: Return from initializer error

I'm getting and error that reads: Return from initializer without initializing all stored properties.

I'm using swift 3 and xcode 8 also firebase as my backend.

import Foundation
import FirebaseDatabase

struct Post {

var ref: FIRDatabaseReference
var key: String!
var username: String!
var postId: String!
var postText: String!

init(username: String, postId: String, postText: String, key: String = ""){

self.username = username
self.postId = postId
self.postText = postText

}//<--- im getting the error right here

init(snapshot: FIRDataSnapshot) {

let values = snapshot.value as! Dictionary<String,String>

self.username = values["username"]
self.postText = values["postText"]
self.postId = values["postId"]
self.ref = snapshot.ref
self.key = snapshot.key


func toAnyObject() -> [String: AnyObject]{

return ["username":username as AnyObject, "postText":postText as AnyObject,"postId":postId as AnyObject]



Any idea on how i can fix this?


The stored property ref is a non-optional type.

According the rules all non-optional properties must be initialized in each provided initializer.

That means you have to assign an initial value to the property or you make the property optional.