sourcetip

클래스 Y의 개체 X가 Swift에서 methodSignatureForSelector를 구현하지 않습니다.

fileupload 2023. 8. 21. 21:32
반응형

클래스 Y의 개체 X가 Swift에서 methodSignatureForSelector를 구현하지 않습니다.

여러 번 인스턴스화되는 클래스 사람이 있습니다.각자가 각자의 타이머를 받습니다.내 안에서init위해서Person부를게요startTimer().

class Person {
 var timer = NSTimer()
 func startTimer() {
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerTick"), userInfo: nil, repeats: true)
 }

 func timerTick() {
    angerLevel++
    println("Angry! \(angerLevel)")
 }
...
...
}

그래서 나는 아마도 일련의 3개의 사람의 인스턴스를 가지고 있을 것입니다.Person[]오류가 발생합니다.

2014-06-25 13:57:14.956 ThisProgram[3842:148856] *** NSForwarding: warning: object 0x113760048 of class '_TtC11ThisProgram6Person' does not implement methodSignatureForSelector: -- trouble ahead

나는 다른 곳에서 내가 물려받아야 할 것을 읽었습니다.NSObject하지만 여긴 Obj-C가 아니라 Swift에 있습니다.그 기능은 클래스 안에 있어서 어떻게 해야 할지 모르겠습니다.

생각하지 마NSObject목표-C 클래스로서 코코아/파운데이션 클래스라고 생각합니다.비록 당신이 Objective-C 대신 Swift를 사용하고 있지만, 당신은 여전히 모든 동일한 프레임워크를 사용하고 있습니다.

두 가지 옵션: (1) 추가dynamic선택기로 참조할 함수의 속성:

    dynamic func timerTick() {
        self.angerLevel++
        print("Angry! \(self.angerLevel)")
    }

또는 (2) 선언합니다.Person의 하위 분류로서NSObject그럼 그냥 전화해요.super.init()이니셜라이저 시작 시:

class Person: NSObject {
    var timer = NSTimer()
    var angerLevel = 0

    func startTimer() {
        print("starting timer")
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerTick", userInfo: nil, repeats: true)
    }

    func timerTick() {
        self.angerLevel++
        print("Angry! \(self.angerLevel)")
    }

    override init() {
        super.init()
        self.startTimer()
    }
}

XCode6 베타 6부터는 '다이나믹' 기능을 사용할 수 있습니다.

dynamic func timerTick() { .... }

사용하려고 할 때 비슷한 오류가 발생했습니다.let encodedArchive = NSKeyedArchiver.archivedDataWithRootObject(archive) as NSData여기서 아카이브는 사용자 지정 클래스의 배열입니다.사용자 지정 클래스를 NSObject 및 NSCoding의 하위 클래스로 선언하는 것이 효과적이라는 것을 알게 되었습니다.NS 코딩 프로토콜을 준수하려면 다음과 같은 몇 줄이 더 필요합니다.

class Person: NSObject, NSCoding {
  init() {
    super.init()
  }

  func encodeWithCoder(_aCoder: NSCoder) {   }
}

언급URL : https://stackoverflow.com/questions/24415662/object-x-of-class-y-does-not-implement-methodsignatureforselector-in-swift

반응형