ios개발 swift 생초보.. 질문 하나 드립니다. 빌드 오류ㅠㅜ

조회수 3105회

안녕하세요 저는 이번에 iOS 프로그래밍에 입문하는 사람 입니다.

몇일 전 설치한 xcode 8.2.1 버젼 사용 중 입니다

아론 힐리가스의 iOS 프로그래밍 책을 통해 공부하고 있습니다.

그 책의 챕터1에 있는 Quiz 어플리케이션 만들기에서 막히는 부분이 있습니다..

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        questionLabel.text = questions[currentQuestionIndex]

    }

    @IBOutlet var questionLabel: UILabel!
    @IBOutlet var answerLabel: UILabel!

    let questions: [String] = ["What's Yejin's phone number?(no-)",
                               "What's Yejin's is birth day?(yyyy,mm,dd)",
                               "What's Seung Hyun's birth day?(yyyy,mm,dd)"]
    let answers: [String] = ["01091967621",
                             "1993,05,25",
                             "1992,11,02"]
    var currentQuestionIndex: Int = 0
    @IBAction func showNextQuestion(sender: AnyObject) {
        currentQuestionIndex = (currentQuestionIndex++)%listOfQuestions.count
        if currentQuestionIndex == questions.count {
            currentQuestionIndex = 0
        }

        let question: String = questions[currentQuestionIndex]
        questionLabel.text = question
        answerLabel.text = "???"
    }

    @IBAction func showAnswer(sender: AnyObject) {
        let answer: String = answers[currentQuestionIndex]
        answerLabel.text = answer
        }
    }

여기서 currentQuestionIndex = (currentQuestionIndex++)%listOfQuestions.count 부분이 막힙니다.. 오류 내용은 Use of unresolved identifier 'listOfQuestions 라고 나옵니다..

맨 처음엔 책에 써있는 대로 ++currentQuestionIndex 라고만 썼었습니다. 그러면 swift3에선 ++이 사라졌다는 문구가 뜨면서 안됩니다.

그래서 인터넷 검색하다 Question 라벨을 계속 눌러도 나타나는 질문이 순환되도록 하는 코드가 저것이라고 하길래 혹시 될까 싶어서 넣어본 것 입니다. 저도 그런 기능을 원하고요.

기능보다도 일단 앱을 빌드해서 구동시켜보고 싶은데.. 이걸 어떻게 해결해야 할지.. 꼭 답변 부탁드립니다. 읽어주셔서 감사합니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • Use of unresolved identifier 'listOfQuestions'

    이부분은 일단 정의 되지 않은 변수를 사용 해서 그렇습니다. 코드상에 listOfQuestions 이라는 변수가 선언되어 있지 않습니다.

    단순 퀴즈가 순환되는 형식이면 이렇게 해주면 currentQuestionIndex의 값이 +1이 되어 순환이 됩니다.

     @IBAction func showNextQuestion(sender: AnyObject) {
    
            currentQuestionIndex += 1
    
            if currentQuestionIndex == questions.count {
                currentQuestionIndex = 0
            }
    
            let question: String = questions[currentQuestionIndex]
            questionLabel.text = question
    }
    
    

    Swift 3.0에서부터는

    SE-0004 : Remove the ++ and — operators (++/– 연산자 삭제)
    

    ++/ -- 연산자가 삭제 되어서 대신에 += 1 또는 -= 1 을 사용 하는게 맞습니다.

    Swift3.0의 변경 사항은 이사이트를 참고 하시면 될꺼 같아요. Swift3.0 변경사항

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)