Destini review

Harold's velog·2024년 2월 28일
0

Udemy

목록 보기
24/25

이제 self review를 해보도록하자.

  1. Story.swift 비교.
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...


  1. StoryBrain
 var destination = 0
    
func getTitle () -> String {
        
        return story[destination].title
    }
    
func getChoice () -> Story {
        
        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 {
                
        if choice == getChoice().choice1 {
            
            destination = getChoice().choice1Destination
            
            return destination
            
        } else {
            
            destination = getChoice().choice2Destination
            
            return  destination
            
        }
    }
    
    
    
func getDestination () -> Int {
        
        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) {
            
            let currentStory = stories[storyNumber]
            if userChoice == currentStory.choice1 {
                storyNumber = currentStory.choice1Destination
            } else if userChoice == currentStory.choice2 {
                storyNumber = currentStory.choice2Destination
            }
        }

뭔가 내가 더 많이 작성을 하였따..
nextStory에 내가 각각의 세분화로 나누었던걸 모두 담아낸것처럼 보인다.


  1. viewController
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가 들어간건 같다.

하지만 변수선언 없이 했는데 되었다.
어제는 안되었는데 차이가 뭘까...

profile
데일리 정리, 하루에 최소 하나의 글은 적도록 하자.

0개의 댓글

관련 채용 정보