[Swift]

iOSoo·2021년 10월 7일
0
post-thumbnail

1. struct 생성후 프로퍼티 선언과 init

struct Car {
    var handle: Int
    var wheel: Int
    
    init(progress: Int, targetMoney: Int) {
        self.progress = progress
        self.targetMoney = targetMoney
    }
}

위의 코드를 보면 car이라는 구조체를 만들고 car라는 구조체 안에있는 handle, wheel이라는 프로퍼티를 생성했다. 쉽게 말하면 자동차의 설계도를 만들고 그 내부에 있는 핸들과 바퀴를 Int형으로 선언해주었다.

그후 car라는 구조체를 초기화 해주었다.
초기화를 해준이유: 같은 구조체로 여러개의 결과물(?)을 만들기 위해서 (하나의 설계도의 변수를 삭제해서 여러개의 건물을 짓기 위해서)

2. 만들어준 struct를 사용해서 인스턴스를 생성해준다.

import UIKit

struct Car {
    var handle: Int
    var wheel: Int
    
    init(handle: Int, wheel: Int) {
        self.handle = handle
        self.wheel = wheel
    }
}

var car1: Car = Car(handle: 1, wheel: 2)
var car2: Car = Car(handle: 2, wheel: 4)
var car3: Car = Car(handle: 3, wheel: 8)

3. 인스턴스를 사용해본다.

import UIKit

struct Car {
    var handle: Int
    var wheel: Int
    
    init(handle: Int, wheel: Int) {
        self.handle = handle
        self.wheel = wheel
    }
}

var car1: Car = Car(handle: 1, wheel: 2)
var car2: Car = Car(handle: 2, wheel: 4)
var car3: Car = Car(handle: 3, wheel: 8)

print(car1.handle) //1
print(car1.wheel) //2
print(car2.handle) //2
print(car2.wheel) //4
print(car3.handle) //3
print(car3.wheel) //8
profile
애플을 좋아하는 비전공생

0개의 댓글

관련 채용 정보