Destini Review
이제 self review를 해보도록하자.
- Story.swift 비교.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Foundation
struct Story {
let title : String
let choice1 : String
let choice1Destination : Int
let choice2 : String
let choice2Destination : Int
init (title : String, choice1: String, choice1Destination : Int, choice2: String, choice2Destination : Int) {
self.title = title
self.choice1 = choice1
self.choice1Destination = choice1Destination
self.choice2 = choice2
self.choice2Destination = choice2Destination
}
}
//
import Foundation
struct Story {
let title: String
let choice1: String
let choice1Destination: Int
let choice2: String
let choice2Destination: Int
}
위에있는게 내가쓴것, 아래가 강의코드 이렇게 해두고 비교를 해볼까 한다.
우선 init을 사용하여 작업을 했다.
뭐 이거 말곤 차이가 없어서 pass…
- StoryBrain ```swift var destination = 0
func getTitle () -> String {
1
2
return story[destination].title
}
func getChoice () -> Story {
1
2
3
4
let choice = story[destination]
return choice
}
func getChoice1 () -> String { return story[destination].choice1 }
func getChoice2 () -> String { return story[destination].choice2 }
mutating func selectDestination (_ choice : String) -> Int {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if choice == getChoice().choice1 {
destination = getChoice().choice1Destination
return destination
} else {
destination = getChoice().choice2Destination
return destination
}
}
func getDestination () -> Int {
1
2
return destination
}
// var storyNumber = 0
func getStoryTitle() -> String { return stories[storyNumber].title }
func getChoice1() -> String { return stories[storyNumber].choice1 }
func getChoice2() -> String { return stories[storyNumber].choice2 }
mutating func nextStory(userChoice: String) {
1
2
3
4
5
6
7
let currentStory = stories[storyNumber]
if userChoice == currentStory.choice1 {
storyNumber = currentStory.choice1Destination
} else if userChoice == currentStory.choice2 {
storyNumber = currentStory.choice2Destination
}
} ``` 뭔가 내가 더 많이 작성을 하였따.. nextStory에 내가 각각의 세분화로 나누었던걸 모두 담아낸것처럼 보인다.
- viewController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
var storyBrain = StoryBrain() override func viewDidLoad() { super.viewDidLoad() updateUI() // As soon as we launch this app, this trigger will be operated } @IBAction func buttonPressed(_ sender: UIButton) { let userChoice = sender.currentTitle! storyBrain.selectDestination(userChoice) updateUI() } func updateUI () { storyLabel.text = storyBrain.getTitle() let getchoice1 = storyBrain.getChoice1() let getchoice2 = storyBrain.getChoice2() choice1Button.setTitle(getchoice1, for: .normal) choice2Button.setTitle(getchoice2, for: .normal) } // var storyBrain = StoryBrain() override func viewDidLoad() { super.viewDidLoad() updateUI() } @IBAction func choiceMade(_ sender: UIButton) { storyBrain.nextStory(userChoice: sender.currentTitle!) updateUI() } func updateUI() { storyLabel.text = storyBrain.getStoryTitle() choice1Button.setTitle(storyBrain.getChoice1(), for: .normal) choice2Button.setTitle(storyBrain.getChoice2(), for: .normal) }
뭔가 비슷하면서 묘하게 달랐다 하지만 Ibaction에 updateUI가 들어간건 같다.
하지만 변수선언 없이 했는데 되었다. 어제는 안되었는데 차이가 뭘까…
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.