Objective-C class properties not viewable on Swift playground

Question about Objective-C and Swift interoperability.
I have some Objective-C headers and implementation files which I am using in a Swift playground for a bridging project. However the Playground does not show any of my Objective-C class properties or methods of the instances of the class in the preview. I can however see properties of instances of URL, CIImage, and NSImage just fine. What step/s am I missing?

let image = CIImage(contentsOf: myUrl!) // With a url location to the image file on my disk.

The Swift Playground shows w, h beside this line.

However for my Objective-C properties of my

@interface SampleRect : NSObject

/** NSRect contains region of output pixels to render **/

@property CGRect dimensions;

-(id) init;  
@end  

The properties after setting the preview shows no information on the SampleRect instance.

What keyword or steps am I missing?

  • Does this answer your question? How to import own classes from your own project into a Playground

    – 

See the documentation for CustomPlaygroundDisplayConvertible. Playgrounds only supports specialised displays for a limited number of types. That includes CIImage, URL etc. For other types, only a “structured description” is displayed, which is probably what you are seeing.

Luckily, your SampleRect is just a wrapper around a CGRect, so you can easily conform to CustomPlaygroundDisplayConvertible and use the wrapped CGRect as the playground display.

extension SampleRect: CustomPlaygroundDisplayConvertible {
    var playgroundDescription: Any {
        dimensions
    }
}

Leave a Comment