Reflection icon indicating copy to clipboard operation
Reflection copied to clipboard

Crashes if classes inherit from NSObject.

Open Lukas-Stuehrk opened this issue 7 years ago • 3 comments

If you try to reflect an instance of a class that inherits from NSObject, it will crash while accessing a null pointer when trying to reflect NSObject.

The minimal example would be:

class Foobar: NSObject {
    var stringProperty: String = "foobar"
}

let instance = Foobar()
try! properties(instance)

I have an example traceback in the attached screenshot. I tried with Swift 3.3 and Swift 4.1.

screen shot 2018-08-08 at 14 48 22

I see that there's already a workaround in the superclass reflection where we exclude the SwiftObject superclass:

https://github.com/Zewo/Reflection/blob/1414e1215c505a8fb4f548661bf4a51903d3c4b5/Sources/Reflection/Metadata+Class.swift#L22

Would it make sense to exclude the NSObject superclass in the same way? Or is reflecting on classes inherited from NSObject in general not supported?

Lukas-Stuehrk avatar Aug 08 '18 12:08 Lukas-Stuehrk

The same issue here..! Do you have any solutions?

GRGBISHOW avatar Aug 21 '18 04:08 GRGBISHOW

@Lukas-Stuehrk @GRGBISHOW you two may want to look at https://github.com/wickwirew/Runtime it's a bit more maintained than this library and functions very similarly.

robertjpayne avatar Aug 22 '18 05:08 robertjpayne

Thanks for the input, @robertjpayne. I had a look at Runtime before I reported the bug. It also crashes when objects inherit from NSObject.

I can provide a fix for this issue. It's just that it might be a surprising change, because it would simply stop to look for inherited properties from the NSObject superclass. That's why I wanted to discuss the issue first.

I can also report the issue at the Runtime project. But the fix looked much more complicated over there 😄.

Lukas-Stuehrk avatar Aug 22 '18 07:08 Lukas-Stuehrk