클래스 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
'sourcetip' 카테고리의 다른 글
mysqdump를 사용하여 CREATE TABLE 명령만 내보내려면 어떻게 해야 합니까? (0) | 2023.08.21 |
---|---|
mkdir vs New-Item, 같은 cmdlet입니까? (0) | 2023.08.21 |
sqlalchemy: 여러 테이블을 하나의 쿼리로 조인하는 방법은 무엇입니까? (0) | 2023.08.21 |
XML을 사용하여 ActionBar Activity의 ActionBar 배경색을 변경하려면 어떻게 해야 합니까? (0) | 2023.08.21 |
송장, 송장 라인 및 수정사항에 대한 데이터베이스 설계 (0) | 2023.08.21 |