Jasper Blues
Jasper Blues

Reputation: 28746

Swift class as argument to objc runtime

When using the objc runtime for Swift introspection, I noted that the following is possible:

object_setClass(anObject, Birdy.classForCoder());

. . . but not

object_setClass(anObject, Birdy);

Both Birdy.classForCoder() and Birdy are an instance of AnyClass, so why is it that only the former will propagate into the objc runtime?

Upvotes: 2

Views: 1095

Answers (1)

Erik
Erik

Reputation: 12858

Use Birdy.self to get the AnyClass metatype.

Example:

object_setClass(anObject, Birdy.self)

If you plan on making your Swift classes available within Obj-C, you should mark them with the @objc annotation:

@objc class Birdy
{
    func tweet()
    {
        println("Tweet tweet! #swift")
    }
}

Upvotes: 4

Related Questions