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 2.0 Enumerations and Optionals Introduction to Optionals Initializing Optional Values

shane reichefeld
PLUS
shane reichefeld
Courses Plus Student 2,232 Points

Can someone help solve and explain what each line does?

Help? Also could someone explain with an example in what situation would be used in an actual iOS App

optionals.swift
struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?

}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Shane :

That's a struct, and each line is not doing anything in particular. You are simply declaring properties of a struct. A struct is a blueprint that defines an object.

Here is an example of creating an object with the struct named "Book".

struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?
}


let myBook = Book(title: "My First Book", author: "Shane", price: "45.00", pubDate: "10/10/16")

// Then you can access these properties with the dot notation. 


myBook.title // Will return "My First Book"


// or you can also print 

print(myBook.author) // will print "Shane"

I recommend starting from swift basics, and moving on to Swift Objective Oriented Program. It will explain everything about objects and how to work with them .

Good luck