Roggie
Roggie

Reputation: 1217

Console continues to output the string wrapped as an Optional in Swift, why?

Please help, I am really struggling with this, have read multiple threads and tuts on this but can't seem to find the issue. I have applied the same approach as another block of code on my app.

I am setting var dictionary with a value when I instantiate the ViewController from a source ViewController.

But the console continues to output the string wrapped as an Optional and therefore also on the UI, why?

sender.name: Liam, 01/13/1990, Optional("Actor")

Setting dictionary with a value:

var dictionary: [String : Any]!{

    didSet{
        print("inside InviteVC")


        inviteType = dictionary["type"] as? String
        inviteId = dictionary["complimentId"] as? String
        status = dictionary["status"] as? Bool
        timeStamp = dictionary["timeStamp"] as? Int

        sender = dictionary["fromUser"] as? User

        print("sender.name: \(sender!.firstName), \(sender!.birthday), \(String(describing: sender?.occupation))")

    }//end didSet

}//end var

Here is my User model:

struct User {

    let uid: String
    let name: String
    let email: String
    let profilePictureURL: String

    var occupation: String?

    let birthday: String
    let firstName: String
    let lastName: String
    let gender: String
    let discoverable: Bool
    let online: Bool

    let discoveryPrefs: [String : Any]

    var profileImages = [String]()

    init(uid: String, dictionary: [String: Any]) {

        self.uid = uid
        self.name = dictionary["name"] as? String ?? ""
        self.email = dictionary["email"] as? String ?? ""
        self.profilePictureURL = dictionary["profilePictureURL"] as? String ?? ""

        self.occupation = dictionary["occupation"] as? String ?? ""

        self.birthday = dictionary["birthday"] as? String ?? ""
        self.firstName = dictionary["firstName"] as? String ?? ""
        self.lastName = dictionary["lastName"] as? String ?? ""
        self.gender = dictionary["gender"] as? String ?? ""
        self.discoverable = dictionary["discoverable"] as? Bool ?? false
        self.online = dictionary["online"] as? Bool ?? false

        self.discoveryPrefs = dictionary["discoveryPrefs"] as? [String : Any] ?? [String : Any]()
        self.profileImages = dictionary["profileImages"] as! [String]


    }//end init

}//end class

And this is where I build my User object:

func getUserInfo(forUserId forId: String, handler: @escaping (User) -> ()) {

        REF_USERS.child(forId).observeSingleEvent(of: .value, with: { (snapshot) in

            //handle snapshot code here...

            var occupa: String?

            if let occupation = snapshot.childSnapshot(forPath: "occupation").value as? String {
                occupa = occupation
            } else {
                occupa = ""
            }



            let dictionary: [String : Any] = ["uid": uid, "name": name, "email": email, "profilePictureURL": profilePictureURL, "birthday": birthday, "firstName": firstName, "lastName": lastName, "gender": gender, "discoverable": discoverable, "online": online, "discoveryPrefs": discoveryPrefs, "profileImages": profileImages!, "occupation": occupa!]

            let user = User(uid: uid, dictionary: dictionary)

            handler(user)
        }, withCancel: { (error) in
            print(error)
        })


    }//end func

Upvotes: 1

Views: 90

Answers (2)

ebby94
ebby94

Reputation: 3152

If you look at the line where you're printing the values, you'll notice that you're trying to print the value of an optional.

print("sender.name: \(sender!.firstName), \(sender!.birthday), \(String(describing: sender?.occupation))")

In the last part, you haven't unwrapped the sender which is still an optional and the attribute variable occupation is also an optional. Replace the last part with

print(sender?.occupation ?? "")

Or

if let occupation = sender?.occupation {
    print(occupation)
}

Upvotes: 1

Ricky Mo
Ricky Mo

Reputation: 7658

Because your var occupation: String? is Optional. String(describing:object) is the same as "\(object)", if the object is Optional type the text will be wrapped by Optional(). You have to unwrap it to get rid of it.

Upvotes: 0

Related Questions