Skip to content

Crashes if classes inherit from NSObject. #16

Description

@Ehlster

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:

guard let superclass = superclass, String(describing: unsafeBitCast(superclass.pointer, to: Any.Type.self)) != "SwiftObject" else {

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?

Activity

  1. GRGBISHOW commented on Aug 21, 2018

    @GRGBISHOW

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

  2. robertjpayne commented on Aug 22, 2018

    @robertjpayne
    Member

    @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.

  3. Ehlster commented on Aug 22, 2018

    @Ehlster
    Author

    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 😄.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions