Module-Swift.h: Property type 'Type2Details * _Nonnull' is incompatible with type 'Type1Details * _Nonnull' inherited from 'Type1'

I have following code in Swift:

@objc
class Type1 {
  @objc var Details: Details

  @objc(Type1Details)
  class Details {}
}

@objc
class Type2: Type1 {
  @objc var Details: Details

  @objc(Type2Details)
  class Details: Type1.Details {}
}

The generated Swift Generated header gives following warning in such case:

Module-Swift.h: Property type 'Type2Details * _Nonnull' is incompatible with type 'Type1Details * _Nonnull' inherited from 'Type1'

This warning doesn’t make sense as Type2Details inherits Type1Details. From what I have found out from developers facing such errors the header file needs to be modified to prevent this error:

  1. By ordering the Type2Details definition above its usage
  2. Or by ignoring this diagnostic.

For point 1, I couldn’t find any way to achieve ordering definitions in the generated header. So I went with option 2 with custom script phase that modifies the generated header to ignore the diagnostics.

In my opinion this diagnostic doesn’t make sense in Swift generated header since Swift compiler does the type checking, allowing issues with type safety being caught during compile time.

It's unsafe in general because someone could say:

void setType2DetailsToType1Details(Type2* t2) {
    Type1* t1 = t2; // Safe because Type1 is a superclass of Type2
    Type1Details* t1d = [[Type1Details alloc] init];
    [t1 setDetails:t1d];
}

and set the Details on an instance of Type2 to an instance of Type1Details (that is not an instance of Type2Details).

1 Like