0

Whenever I try to take the input from a text field, I get this error. Could anyone please help?

2017-05-08 17:45:04.962124 MyPhotos[32987:246251] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/local/Library/Developer/CoreSimulator/Devices/05F02AB6-BDEA-43EA-9E7E-D547D3A0CC1A/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-05-08 17:45:04.962486 MyPhotos[32987:246251] [MC] Reading from private effective user settings.

Code:

class AddPhotoController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var photoTitle: UITextField!
    @IBOutlet weak var photoTags: UITextField!
    @IBOutlet weak var photoURL: UITextField!
    @IBOutlet weak var photoPreview: UIImageView!


    @IBAction func savePhoto(sender: AnyObject) {
        let title: String = photoTitle.text!
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ali
  • 141
  • 3
  • 12
  • when i run the code and try to get the value it always return nil and the application is crashed after that. – Ali May 08 '17 at 07:52
  • I don't think the above is the actual cause of your error. I'm seeing the "Reading from private effective user settings" from time to time as well (and no crashes :)) Have a look here for instance: http://stackoverflow.com/a/40458288/4063602, or here: http://stackoverflow.com/q/41416048/4063602 Seems like a bug in Xcode. So...could you show us some of your code so we can help you determine what causes the crash? – pbodsk May 08 '17 at 08:01
  • Yes i have. but lets say i have a button and i want to get the value of the textfield when i press it @IBAction func savePhoto(sender: AnyObject) { let title: String = photoTitle.text! } This always return nil and the application it stuck at this point. i did try to check with a break point but i always return nil – Ali May 08 '17 at 08:11
  • please have a look at the code. – Ali May 08 '17 at 08:20

1 Answers1

1

You say:

lets say i have a button and i want to get the value of the textfield when i press it

If for some reason you are not able to read the value from photoTitle then your app will crash in this line:

let title: String = photoTitle.text!

Here you are trying to force the value of photoTitle.text into a String regardless of it being nil or not, and Swift doesn't like that :)

So, a first guard of defence could be to check whether the value actually is nil before you use it, and only if it isn't nil, then you use it. Something like:

@IBAction func savePhoto(sender: AnyObject) {
    if photoTitle.text != nil {
        let title = photoTitle.text!
    }
}    

Now you know that the value of title is not nil, and can use it safely.

Next question...why is it nil in the first place? That seems to be a problem with your @IBOutlet. It seems that the photoTitle var is not properly connected to the UITextField in your storyboard. Can you verify that your UITextField in Interface Builder is properly connected to you photoTitle variable?

Hope this helps you.

Efren
  • 4,003
  • 4
  • 33
  • 75
pbodsk
  • 6,787
  • 3
  • 21
  • 51