When I create a variable without String() to initialize the variable an error shows up alerting, "variable not initialize", same error shows up when I directly assign
var username: String = usernameTextfieldSigup.text!
And when I initialize the variable using the code below,
var username: String = String()
username = usernameTextfieldSignup.text!
warning comes up, variable 'username' was written but never used.
I know I could just ignore these warnings but I'd like a cleaner code. I'm using Xcode7 beta5 release.
You can declare the username without a value first:
and then assign a value to it in your initializer:
However, I'm sure that
usernameTextfieldSignup.textwon't have a value until the user ha actually provided a value. So in that case I would recommend to makeusernamean optional so you can set it's value when you need to:and then when setting it:
and when accessing it:
Also, the
variable 'username' was written but never usedwarning you're seeing is probably because you write to / initialiseusernamebut there's no code actually reading it, so it's kind of a useless variable according to the compiler then.