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
shane reichefeld
Courses Plus Student 2,232 PointsXcode Swift and UITextField
In xcode, i have a button with IBOutlet and IBAction that when pressed takes my input in the text field and shows it in a label. the text field uses a decimal pad so only 0-9 and ".". My question is what would i do to have my input saved into an array as an int. So that way i may call that item out of the array for other uses.
2 Answers
Reed Carson
8,306 Pointsvar array: [String] = []
func pressTheButton() {
array.append(textfield.text)
}
then when you need to use it just cast it to a double. (Int wont work if youve got a decimal "float"ing around, haha pun)
let savedNumber = Double(array[index])
Anjali Pasupathy
28,883 PointsYou would have to cast your input as an Int and unwrap the Int, since the cast would make your Int an Optional value. After that, you can put it in your array.
Reed Carson
8,306 Pointsshane are you building a calculator, just curious. If you are I have a github project you could look at. But to answer your question, I'd have to see the code. If your calculations functions are done in a class, you can create an instance of that class in your VC to use it. But I'd have to see that code to truly answer. If the functions are just global then you can call them from an IBAction
shane reichefeld
Courses Plus Student 2,232 Pointsshane reichefeld
Courses Plus Student 2,232 PointsIm confused on another part? So i have the main ViewController.swift file, and i am have all my calculation equations coded in a swift file named Calculations.swift how do i call my textfield from ViewController.swift to be used in Calculations.swift file?