2016년 5월 17일 화요일

Swift - struct 와 class 로 배워 보는 Call by Value / Call by Reference


// 스트럭트
struct Task {
    var title:String
    var time:Int?
}

var callTask = Task(title: "Call to randy", time: 10*60)
var reportTask = Task(title: "Report to boss", time: nil)

var todayTask : [Task] = []
todayTask += [callTask, reportTask]

todayTask[1].time = 15*60
callTask.title = "Call to toby"

print("today task = \(todayTask) \r\n callTask = \(callTask)")


// 클래스
class TaskClass {
    var title:String
    var time:Int
    
    init(title:String, time:Int){
        self.title = title
        self.time = time
    }
}

var callTask2 = TaskClass(title: "Call to randy", time: 10*60)
var reportTask2 = TaskClass(title: "Report to boss", time: 10*80)

var todayTask2 : [TaskClass] = []
todayTask2 += [callTask2, reportTask2]

todayTask2[1].time = 15*60
callTask2.title = "Call to toby"


print("today task = \(todayTask2[0].title) \r\n callTask = \(callTask2.title)")




실행 결과는

today task = [Task(title: "Call to randy", time: Optional(600)), Task(title: "Report to boss", time: Optional(900))] 
 callTask = Task(title: "Call to toby", time: Optional(600))
today task = Call to toby 
 callTask = Call to toby

댓글 없음:

댓글 쓰기