Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alexander Certosimo
1,774 Pointstrying to create local variable in IBAction for button, but keep getting warning
Hey all,
I am simply trying to assign textInput.text to a local variable in my button IBAction, but i keep getting the warning that says "initialization of variable "input" was never used, consider replacing with assignment '_ 'or removing it"
I have never had this warning before, what does this mean or what am i doing wrong? Thanks for the help
Alexander Certosimo
1,774 PointsHi Gabe!
Yup no problem, sorry about not posting!
class ViewController: UIViewController {
var qa = Questions()
@IBOutlet weak var textInput: UITextView!
@IBAction func submitText(sender: UIButton) {
var input = textInput.text
}
Basically all i want to do here is assign the UItextView to the variable input so i can do a little more with it. This is not the finished product, i just wanted to clear up what this warning was before i moved forward
Thanks as always
1 Answer
Gabe Nadel
Treehouse Guest TeacherSo, the error:
"initialization of variable "input" was never used, consider replacing with assignment '_ 'or removing it"
Means that you created something, but haven't used it for anything yet, so it's suggesting you remove it. The truth is, you get these types of warnings as you build programs all the time, since often you ned to create a variable and use it later on. If you still have these errors as your code grows towards completion then you should be careful to check that all your variables are really needed and being used in the way you intended. If not, perhaps they are loose ends or dead code and should be removed - but remember to save your projects before you go removing everything you think is extra, sometimes you'll discover it wasn't as unnecessary as you thought and will want it back.
Gabe Nadel
Treehouse Guest TeacherGabe Nadel
Treehouse Guest TeacherHi Alexander,
The Community is happy to help, but we'll be much better equipped to do that, if you give us a bit more context. Would you mind sharing the complete code?
Thanks!