-1

Hi am new to swift i am string to store user credential in user default from the struct object in a LoginViewController. Below is my code,

class User: NSObject {
    //MARK: Properties
    var name: String
    var email: String
    var loginData: LoginData

    //MARK: Initialization
    init(name: String, email:String, loginData: LoginData) {
        self.name = name
        self.email = email
        self.loginData = loginData
    }
}

Here LoginData is a structure

struct LoginData {
    var username: String
    var password: String
}

while submitting register data am assigning values as

let logindata = LoginData (username: username, password: password)        
let v = User(name: nameval, email: emailval, loginData:logindata)

In LoginViewContoller I have a switch button to save user credential to NSUserdefaults I created

var logindataValue: LoginData

And storing in user defaults as follows

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(logindataValue.username, forKey: "username")
defaults.setObject(logindataValue.password, forKey: "password")
defaults.synchronize()

But in LoginViewController class showing an error as “LoginViewController has no initializers”

How to prevent this error? Is this the right approach to do in this scenario?

James
  • 1,036
  • 1
  • 8
  • 24
cinvin
  • 13
  • 4
  • You provided a lot of irrelevant data about your various model objects, but didn't include almost anything for the one class that is actually giving you the error, the `LoginViewController`. I'm guessing that it's missing because it's probably "too big", which means you didn't even attempt making an [MCVE](http://stackoverflow.com/help/mcve). But if you want some related reading regarding Swift initializers, I suggest you [start here](http://stackoverflow.com/a/32108404/2792531). – nhgrif Apr 29 '16 at 19:56

1 Answers1

1

Swift is complaining that you aren't initializing the var logindataValue: LoginData in your LoginViewContoller. You can solve this a couple of ways.

  1. Make logindataValue an Optional:

In your LoginViewController change the definition of logindataValue to this:

var logindataValue: LoginData?

It will be given a default initial value of nil. You'll have to unwrap it when you use it, but that's okay.

  1. Give logindataValue a different default value:

In your LoginViewController change the definition of logindataValue to this:

var logindataValue = LoginData(username: "", password: "")

I don't recommend this approach. It doesn't seem right to create an empty LoginData object with meaningless values. It would be better to use an Optional value. But if there is some other meaningful default value that you could give, then this might work.

  1. Add an initializer for LoginViewController:

This is the most complicated, as it is not easy to create custom initializers for subclasses of UIViewController. You will have to add an initializer that includes a logindataValue parameter that you can use to initialize logindataValue. But you will also have to override init?(coder: NSCoder) which is a pain in the you-know-what and probably not what you need.

So my advice is to make logindataValue an Optional.

Aaron Rasmussen
  • 13,082
  • 3
  • 42
  • 43