nghialv / MaterialKit

Material design components for iOS written in Swift
MIT License
2.5k stars 290 forks source link

Converting MKTextField to a string #33

Closed craigpearce5 closed 9 years ago

craigpearce5 commented 9 years ago

I'm still new to swift so this may be an easy answer.

Is there a way to convert an MKTextField into a string? I'm using alamofire to create HTTP requests for login, however it doesn't support MKTextFields.

powerje commented 9 years ago

@craigpearce5 what code are you writing?

MKTextField is a subclass of UITextField, so it should have a text property which is a NSString* which you can use.

craigpearce5 commented 9 years ago

@IBOutlet weak var login: MKTextField! @IBOutlet weak var password: MKTextField!

Alamofire.request(Alamofire.Method.GET, "https://*******staging.herokuapp.com/operators/programs.json?email=(login)&password=(password)", parameters: nil, encoding: .JSON)

I've tried to first downcast as Strings by trying,

let userMK = self.login as! String

but it still doesn't work.

When I compile and run, this error is written to the console. fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

When I try to downcast, I get an xcode warning saying NSString is not a subtype of MKTextField.

powerje commented 9 years ago

what if you try using the text property on your login object?

i.e.:

let username = self.login.text

craigpearce5 commented 9 years ago

Yes - that worked!!!

I never even thought of that. Great idea.

Just for anyone else reading this, you can access the text property directly in the alamo request like so.

Alamofire.request(Alamofire.Method.GET, "https://*******staging.herokuapp.com/operators/programs.json?email=(login.text)&password=(password.text)", parameters: nil, encoding: .JSON)