풍's토리

반응형

import UIKit

 

var str = "Hello, playground"

 

class BookInfo {

    var title = "" {

        willSet(newTitle) { //before change

            print("Title change : \(self.title) -> \(newTitle)")

        }

        didSet(oldTitle) { //after change

            print("Title changed : \(oldTitle) -> \(self.title)")

        }

    }

    var author = ""

    

    //only get

    var bookTitle:String {

        return title + " by " + author

    }

    

    //get and set

    var bookTitle2:String {

        get {

            return "This book's title : " + title + " by " + author

        }

        set(subtitle) {

            title = title + "(" + subtitle + ")"

        }

    }

}

 

let book = BookInfo()

book.title = "Swift"

book.author = "Apple"

 

print(book.bookTitle)

 

book.bookTitle2 = "second"

print(book.bookTitle2)