Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Enums and Structs Structs Stored Properties

Can Swift Structs have optional stored properties?

Is there a way to define an optional stored property on a swift struct?

Something like:

struct Person {
    var firstName : String
    var lastName : String // not sure what the syntax would be
}

var john = Person(firstName: "John") // notice I'm passing only the firstName

6 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Fabricio Quagliariello,

The reason you got this error is because by default the constructor or init method as it's called requires all stored properties to have an assigned value thus making them required even if they're optional, to get around this you simply need to create your own init method which accepts only the parameters you need it to.

See the following.

struct Person {
    var firstName: String
    var lastName: String?

    init(firstName: String) {
        self.firstName = firstName
    }
}

var john = Person(firstName: "John")

if let lastName = john.lastName {
    println("\(john.firstName)' last name is \(john.lastName)")
} else {
    println("\(john.firstName) doesn't have a last name")
}

Happy coding!

Hikarus Guijarro-Fayard
Hikarus Guijarro-Fayard
86 Points

for me, just including the default values in the struct's init worked. Here's an example I'm working on:

struct DummyMatch {
   var gender: Bool
   var name: String
   var location: String
   var status: MatchStatus
   var scheduled: String
   init(gender: Bool = false, name: String, location: String = "San Francisco", status: MatchStatus = .listed, scheduled: String = "15/05/1@17:30") {
      self.gender = gender
      self.name = name
      self.location = location
      self.status = status
      self.scheduled = scheduled
   }
}
var match = DummyMatch(name: "Mary")
Michael Hulet
Michael Hulet
47,913 Points

Yes, they can, but you must set an initial value on them, or make an explicit designated initializer. I think what you'd want is something like this:

struct Person {
    var firstName: String
    //Notice the ? below
    var lastName: String? = nil
}
Chris Shaw
Chris Shaw
26,676 Points

You don't actually need to assign nil as an explicit value as ? implies a nil value by default thus the following will suffice.

var lastName: String?

It seems that even declaring the stored parameter as optional, and/or setting a default value, you need to declare all the stored parameters when initialising an instance.

See this playground screenshot: syntax error

And the same for the version suggested by Chris Upjohn syntax error version 2

and the fix for the error: fixed

fixed v 2

Thanks anyway guys!

Chris Upjohn, please post your last answer as top-level comment on the thread, so I can choose it as "Best Answer". You seriously rock, my friend!

Well, at the end the way to declare certain stored values when initialising an struct, and make the other optional is to declare custom initialisers and overload them

struct Person {
    var firstName : String
    var lastName : String? = nil

    init(firstName: String) {
        self.firstName = firstName
    }

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }
}

var john = Person(firstName: "John", lastName: "Smith")
var jane = Person(firstName: "Jane")