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

iOS

Peter Fuhrey
Peter Fuhrey
2,146 Points

Help with text fields in Swift!

Hi everybody! So I'm trying to practice my use of xcode with Swift so I figured I would try to make some basic apps. Today I've been trying to work on app where you enter one of the four compass points (North, East, South, West) and out would come what state you were heading towards (New York, Carolinas, Florida, California). I'm using a label for the output of which state you were heading towards, a text field for entering your direction and finally a button just for the submit action. I was wondering what would be the best way for assigning the state to the compass direction? I'm currently using an enum for North, East, South, West and then trying to use a switch statement in my method with each case printing out the corresponding state. Also if someone could kind of give me guidance as to how I would set up my UITextField to accept these compass directions and then print out the corresponding state I would greatly appreciate it.

Peter Fuhrey
Peter Fuhrey
2,146 Points

Unfortunately, no.

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

Ok, no problem. Could you post the code you already have, so I don't write anything that confuses you? ;-)

Peter Fuhrey
Peter Fuhrey
2,146 Points

import Foundation

enum CompassDirections { case North case East case South case West

init() {
    self = .North
}

func stateBound() -> String {
    switch self {
    case .North:
        return "New York"
    case .East:
        return "Carolinas"
    case .South:
        return "Flordia"
    case .West:
        return "California"
    }
}

}

Heres my code to correspond direction with state.

import UIKit

class ViewController: UIViewController { @IBOutlet weak var StateBound: UILabel! @IBOutlet weak var Direction: UITextField!

let directions = CompassDirections()

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

@IBAction func Sumbit(sender: AnyObject) {
}

}

and here's my ViewController As you can tell I don't have anything in the function for the button as to thats where I'm confused.

1 Answer

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

Alright, I tried everything to get it working like I think you wanted it to, but unfortunately I didn't manage to. BUT I made a few changes and got two versions working like I would probably do it. I made one version with four buttons, each for the corresponding compass direction, and one with a text field that only takes valid compass directions. Check them out here:

https://github.com/thedan84/Compass-TextField

https://github.com/thedan84/Compass-Enum

I hope I could help you at least a bit. Remember that enums normally aren't used for the case you were trying. Most of the time enums are used for drop down menus or any other way a user can make a choice between different options. ;-)

Cheers

Peter Fuhrey
Peter Fuhrey
2,146 Points

Thanks this really helps me grasp an understanding! I appreciate it a lot!