[ios] swift 체험하기 #7 프로퍼티
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)
'메뉴 일곱' 카테고리의 다른 글
[ios] swift 체험하기 #8 배열 (0) | 2019.04.12 |
---|---|
[ios] swift 체험하기 #6 클래스2 & 옵셔널 (0) | 2019.04.11 |
[ios] swift 체험하기 #5 클래스 (0) | 2019.04.10 |