不能在Objective-C类中使用方法变量' Swift中的扩展

时间:2015-02-01 12:44:38

标签: objective-c swift extension-methods

class FirstTest {
    func testMe() {
        println("This is a simple test.")
    }
}

extension FirstTest {
    func output() {
        let myTestMe = testMe
        myTestMe()
    }
}

let myFirstTest = FirstTest()
myFirstTest.output()
//This is a simple test.

以上代码有效。但是,如果我让类继承NSObject,我将得到以下错误。

class FirstTest: NSObject {
    func testMe() {
        println("This is a simple test.")
    }
}

extension FirstTest {
    func output() {
        let myTestMe = testMe
        myTestMe()
    }
}

let myFirstTest = FirstTest()
//objc[22959]: Method cache corrupted. This may be a message to an invalid object, or a memory error somewhere else.
//objc[22959]: receiver 0x10d62c980, SEL 0x1039021a5, isa 0x10d62ca20, cache 0x10d62ca30, buckets 0x7fc6a1d18d80, mask 0x0, occupied 0x0
//objc[22959]: receiver 0 bytes, buckets 64 bytes
//objc[22959]: selector 'allocWithZone:'
//objc[22959]: isa '__lldb_expr_1.FirstTest'
//objc[22959]: Method cache corrupted.

有人可能会说这是因为某事的c指针。但是,如果我将扩展中的代码放回到类中,一切都可以正常工作。

class FirstTest: NSObject {
    func testMe() {
        println("This is a simple test.")
    }

    func output() {
        let myTestMe = testMe
        myTestMe()
    }
}

let myFirstTest = FirstTest()
myFirstTest.output()
//This is a simple test.

以上所有代码均在Xcode Version 6.1.1(6A2008a)中运行。

0 个答案:

没有答案