Vladyslav Semenchenko
Vladyslav Semenchenko

Reputation: 625

Xcode warning - variable was never mutated

I understand that this is newbie question but I can not find answer on stackoverflow or google.

I am starting writing project with Swift 3 language. Here is my model class:

class VKUserProfile: NSObject {
    var userId: NSNumber?
    var userName: String?
    var userEmail: String?
    var userMobilePhone: String?
    var userPictureUrl: URL?
}

Then I use it in another class:

private func convert(user: Dictionary<String, Any>) -> VKUserProfile {
    var currentUser: VKUserProfile = VKUserProfile()
    currentUser.userId = user["id"] as? NSNumber
    return currentUser
}

Xcode warns me on line "var currentUser: VKUserProfile = VKUserProfile()". Ok, but when I change it to "let currentUser: VKUserProfile = VKUserProfile()" - I can not set any properties (object is empty). So can someone describe me this process, why Xcode warns and how I can fix this.

UPDATE: Here is screenshot of currentUser variable when currentUser is let:

let

And this is screenshot of currentUser variable when currentUser is var:

enter image description here Thank you in advance!

Upvotes: 2

Views: 1379

Answers (1)

rmaddy
rmaddy

Reputation: 318774

It seems that when you use:

var currentUser = VKUserProfile()

the rest of your code properly fills in currentUser and you can see the values when you print currentUser.

But using var gives a warning since you never actually reassign currentUser.

So you rightfully change var to let and make no other changes. But now when you print currentUser, you don't see the same output as you did when you use var as shown in the screenshots you posted in your question.

It seems this is a problem with Xcode's debugger.

Printing individual properties of currentUser shows the expected output.

So in the end, this is nothing but a bug in Xcode's debugger. Your code is fine even after changing var to let.

Upvotes: 4

Related Questions