1

As you can see from. the title, i am trying to turn the content of a text field into a variable. I have used this code:

@IBOutlet weak var TextField: UITextField!
let variable = TextField.text

UPDATE: I created a new project with new code, which I have inserted above. The only errors I got was this: Cannot use instance member 'TextField' within property initializer; property initializers run before 'self' is available.

QiwiCode
  • 15
  • 1
  • 6
  • Is there a typo in `textfeild.text` in the question or you also have the typo in your code? Should be `textfield.text` – denis_lor Mar 24 '20 at 17:48
  • 1
    Basically `String(textfield.text)` (create a string from a string) is redundant apart from the *optional* issue. – vadian Mar 24 '20 at 17:48
  • The original code did not have textfeild.text. Instead, the actual one is o.text. The variable was repton. I just made my variable into things that i know i could spell. I changed it to not confuse you guys. – QiwiCode Mar 25 '20 at 10:07

1 Answers1

1

UPDATE: I think you are having different issues than the one you mentioned, after your answer has changed a few times, I understood that you want to use the value of a UITextField to use it for your WebView URL to load and also to check the prefix sometimes in a different state later on. But you are actually loading the webview on viewDidLoad which won't work for you as you didn't have yet the chance to change your UITextField value manually from your phone/simulator.

That means there can be different work-case scenarios and flows, that you might want to think and implement which one best works for your business needs. For now, if you want the value of a UITextField to be stored in a variable and access the latest value or the first value of it, you can follow my original answer:

If you want variable to be a stored property, here you store the value of textfield.text in the exact moment you instantiate variable, then the issue in your code is that textfield.text can be nil, so you would have better handled that as:

let variable = textfield.text ?? ""

If you want variable to be a computed property, in this case everytime you try to access variable and get its value it will calculate it based on the code in its closure, so it will fetch the latest textfield.text value everytime you try to get variable value:

var variable: String? { // optional, remember textfield.text can return nil
    return textfield.text
}

If you always want to return something you could instead declare it this way, and return an empty string in case textfield.text is nil (remember, textfield.text can be nil):

var variable: String { // not optional, you give a "" default value if nil
    return textfield.text ?? ""
}
denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • Sorry, it didn't work. The first one made a random error with a variable in the line beneath it, the second one just had errors EVERYWHERE and the third had no errors but when i ran it, it gave me a blank white screen and gave me the error Thread 1: signal SIGABRT. By the way, the variable i am trying to store is a URL. Would that effect anything? – QiwiCode Mar 25 '20 at 09:21
  • Maybe you are having other code issues, this code is really independent of anything, and works exactly as shown. Can you post some screenshots to see how your error are now appearing or more code so we can try something concrete? – denis_lor Mar 25 '20 at 09:25
  • Well, i was trying to make a web browser, i used the code of the first answer in https://www.stackoverflow.com/questions/36231061/wkwebview-open-links-from-certain-domain-in-safari and then replaced all the google.coms in the code as the variable variable and i created a text field that you entered a URL in. That text field's content then got turned into variable but i got these 4 errors above. – QiwiCode Mar 25 '20 at 10:02
  • @QiwiCode Can you post your code in the question, edit the question and post more code so I can see exactly what you are still missing and help you out – denis_lor Mar 25 '20 at 10:07
  • I thought you just used the code on that answer and replace `google.com` with textfield values, then post the code at least till the first 2-3 errors you get from top to bottom. – denis_lor Mar 25 '20 at 10:11
  • Right. I used the exact same code, replaced google.com with a variable, got a textfield on main.storyboard, control-dragged it into the assistant editor and failed to turn it's content into the variable that replaces google.com. – QiwiCode Mar 25 '20 at 10:21
  • I can't post 2 screenshots in one go sorry! – QiwiCode Mar 25 '20 at 10:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210287/discussion-between-denis-lor-and-qiwicode). – denis_lor Mar 25 '20 at 10:23
  • @QiwiCode did my answer helped you with your initial problem? If yes - please mark it as accepted – denis_lor Apr 01 '20 at 08:04
  • 1
    In the new project, for some reason, it happened to work! Thank you denis_lor – QiwiCode Apr 07 '20 at 13:41