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 Pointsstuck on a piece of code to check empty text field
hey all,
I am trying to implement a check to see if a text field is empty, but i am having no luck. Basically what is happening is i can add an empty string to my table view, and i do not want that so i tried to avoid that from happening with this:
@IBAction func addItem(sender: AnyObject) {
if taskInput.text != nil {
taskList.append((taskInput.text!))
taskInput.text = ""
NSUserDefaults.standardUserDefaults().setObject(taskList, forKey: "taskList")
}else{
print("no")
}
}
Alexander Certosimo
1,774 Pointshey Jhoan ,
Thanks for the reply! So what i am doing here is using a button to take a user inputted string from UItextfield (taskInput) to then add that string to the array named taskList. The empty string is there to reset the Uitextfield back to nothing so the user inputted text disappears. The else statement was just for testing to see if the code worked. My main goal is to avoid an empty Uitextfield from being submitted to the array, which is then input into the tableview. I want the button to not allow just white space to be submitted
1 Answer
Jhoan Arango
14,575 PointsGot it.. How about if you declare a constant and append it to the array..?
@IBAction func addItem(sender: AnyObject) {
if taskInput.text != nil {
let thisConstant = taskInput.text
taskList.append(thisConstant)
taskInput.text = ""
NSUserDefaults.standardUserDefaults().setObject(taskList, forKey: "taskList")
}else{
print("no")
}
}
Let me know if that works.. if not I will get in Xcode and help you out.
Alexander Certosimo
1,774 Pointshey Jhoan
So i tried that out and had not luck. The if statement still wont work. If i hit my button with nothing in the uitextfield, it does not print "no" to the console and still submits the empty space to my table view. I am pretty stuck with this.
Jhoan Arango
14,575 PointsHello Alex, Good news :)
It's a very very simple fix.
Instead of using nil, use "".
if taskInput.text != "" {
// actions
}
So if the text field is empty then it will not execute your action and will print the else action.
Please let me know if that works
Alexander Certosimo
1,774 PointsJhoan
Thank you so much! that simple fix worked! I was stuck on this for so long. Would this same approach work for a uitextview as well?
Jhoan Arango
14,575 PointsI don't see why not.
Glad it worked, if you encounter anything else let me know..
Good luck
Alexander Certosimo
1,774 PointsThanks Jhoan. I have a question related to this i figured i would ask you. With this project i am working on, i am setting up a table view with 4 sections. I am creating custom cells that have a uitextview in each of them along with a button so that the user can add, edit, and delete their data easily (it seemed like the best solution). The data for each section is going to come from a different array. How do i specify cells in each different section to get the data from the different arrays? Can i make a seperate cellForRowAtIndexPath function for each section?
The overview of what i am trying to do is have a user input questions that would be stored in an array, and have the tableview be the area that the user can do all the adding and deleting of their questions within the uitextview.
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsHello :
If you can explain a bit better I may be able to help you with this. But I am not understanding what you want to do at the end.