• Register
search
Log In
1 vote
31 views

Problem :

I am very new to Swift. Now I have started learning Swift. Recently I have written very simple class as shown below for your reference:

public class SwiftUser {
    let userid: Int
    let userfirstName: String
    let userlastName: String
    let useremail: String?

    init(userid: Int, userfirstName: String, userlastName: String) {
        self.userid = userid
        self.userfirstName = userfirstName
        self.userlastName = userlastName
    }
}

I was able to compile above code just fine in my previous Swift version. But in my latest Swift 1.2 I am facing the below compilation error.

“Return from initializer without initializing all stored properties”.

Now I am very confused. Please help me in resolving above Swift error.

6.6k points

1 Answer

1 vote
 
Best answer

Solution :

I can surely help you in your Swift related issue. If your property is of the type constant then in that case it is created with the help of let and so you should initialize it in some place or if possible in your init method even if it is of type Optional. Now if you are willing to set your email optionally then in that case you should try to change your let type to the var type. In other words I can tell you if you are currently not initializing your variable in either of your init method or in your class body in that case your variable should be both of the type  var and it should be the Optional. Please find below updated code.

public class SwiftUser {
    let userid: Int
    let userfirstName: String
    let userlastName: String
    var useremail: String?

    init(userid: Int, userfirstName: String, userlastName: String) {
        self.userid = userid
        self.userfirstName = userfirstName
        self.userlastName = userlastName

    }
}

 

34.5k points
selected by

Related questions

0 votes
1 answer 178 views
178 views
Problem : I am trying to present the view controller as the pop over if the user successfully resets their password. Basically, if all is ok with emailing the password reset instruction email then the below code will run. ... .Popover let mypopover = mynavController.popoverPresentationController popover?.delegate = self self.presentViewController(mynavController, animated: true, completion: nil)
asked Dec 27, 2019 alecxe 7.5k points
0 votes
1 answer 284 views
284 views
Problem : I got an error on Xcode saying that there was no information about the view controller as follows: Could not insert new outlet connection: Could not find any information for the class named Why am I getting this error?
asked Nov 28, 2019 alecxe 7.5k points
0 votes
1 answer 319 views
319 views
Problem : Facing following Swift issue must register a nib or a class for the identifier or connect a prototype cell in a storyboard
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 17 views
17 views
Problem : I have recently started to learn the Swift Programming. I have written one swift program. But my Swift program is always crashing with the below error. “Fatal error: Unexpectedly found nil while unwrapping an Optional value” I am unable to understand above error and I want to know how can I fix it?
asked Feb 22 mphil 2.3k points
1 vote
1 answer 27 views
27 views
Problem : My Swift program is always crashing with the EXC_BAD_INSTRUCTION and with the below error. What does this error mean in detail? And how can I fix this error? Fatal error: Unexpectedly found nil while unwrapping an Optional value
asked Dec 27, 2019 alecxe 7.5k points