๋ฐ์ํ
๐ ๋ชฉ์ฐจ
- ๊ตฌ์กฐ์ฒด
- ํด๋์ค
๐ ๊ตฌ์กฐ์ฒด
๊ตฌ์กฐ์ฒด๋ value ํ์ ์ ๋๋ค. ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์๊ฑด ๋ถ๊ฐ๋ฅ ํฉ๋๋ค.
struct Cat {
var name: String
}
let cat = Cat("helllo")
cat.name = "sam" // Error!
๊ธฐ๋ณธ ๋ฌธ๋ฒ
struct ๊ตฌ์ ์ฒด์ด๋ฆ {
ํ๋กํผํฐ์ ๋ฉ์๋
}
์์
struct User {
var nickname: String
var age: Int
var information2: String {
get {
return "\(nickname) \(age)"
}
set(newNickname) {
nickname = newNickname
}
}
func information() {
print("\(nickname) \(age)")
}
}
var user = User(nickname: "Eddie", age: 11)
user.nickname // Eddie
user.age // 11
user.age = 100
user.age // 100
user.information() //Eddie 100
๐ ํด๋์ค
ํด๋์ค๋ ์ฐธ์กฐ ํ์ ์ ๋๋ค.
๊ธฐ๋ณธ ๋ฌธ๋ฒ
class ํด๋์ค ์ด๋ฆ {
ํ๋กํผํฐ์ ๋ฉ์๋
}
์์
class Dog {
static var breed: Bool = true // ํด๋์ค ์์ฑ์ ์์ด ์ ๊ทผ ๊ฐ๋ฅ
var name: String = ""
var age: Int = 0
init() {}
init(name: String, age: Int) {
self.name = name
self.age = age
}
func introduce() {
print("name: \(name), age: \(age)")
}
// ํด๋์ค๊ฐ ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋ ๋ ํธ์ถ
deinit {
print("deinit dog")
}
var gender: String = "Male" {
// Observable
willSet {
print("\(newValue) !! ")
}
didSet {
print("\(oldValue)!!")
}
}
}
var dog = Dog(name: "Kadon", age: 100)
dog.name // Kadon
dog.age // 100
dog.name = "Coco"
dog.age = 3
dog.name // Coco
dog.age // 3
dog.introdue() // name: Coco, age: 3
var dog2: Dog? = Dog()
dog2 = nil // deinit dog
๋ฐ์ํ
'Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift asset ์ guard (0) | 2021.11.28 |
---|---|
Swift ํ์ ์บ์คํ (0) | 2021.11.28 |
Swift ์ต์ ๋ (0) | 2021.11.20 |
Swift ํจ์ (0) | 2021.11.20 |
Swift ์ปฌ๋ ์ ํ์ (0) | 2021.11.20 |