How did Apple bridge this struct to Objective-C

It's quite a challenge to replicate that setup. Complications:

  • For those who's unfamiliar: to use UICollectionViewCell you typically make a subclass of it and override its methods (one of which is updateConfiguration).
  • UICollectionViewCell has to be declared in Obj-C otherwise Obj-C code won't be able subclassing it (I assume that using it from Obj-C is important).
  • updateConfiguration(using: Struct_version_of_UICellConfigurationState) can't be declared in Obj-C as the "Struct_version_of_UICellConfigurationState" is only available in Swift.
  • you could add updateConfiguration(using: Struct_version_of_UICellConfigurationState) in a Swift's extension of UICollectionViewCell but instance methods declared in extensions are not overridable, unless they are marked with @objc.
  • that method can't be marked @objc as it's using a Swift-only type that's not available in Objc-C.
Details
// .h
#import <Foundation/Foundation.h>

NS_SWIFT_NAME(__UICellConfigurationState)
@interface UICellConfigurationState: NSObject // UIViewConfigurationState but let's keep it simpler
@end

@interface UICollectionViewCell: NSObject
- (void)updateConfigurationUsingState:(UICellConfigurationState*)state NS_SWIFT_NAME(__updateConfiguration(using:));
@end

// .m
@implementation UICellConfigurationState
@end

@implementation UICollectionViewCell
- (void)updateConfigurationUsingState:(UICellConfigurationState*)state {}
@end

// .swift
public struct UICellConfigurationState {}

extension UICollectionViewCell {
    open func updateConfiguration(using state: UICellConfigurationState) {}
    // 🔶 Warning: Non-'@objc' instance method in extensions cannot be overridden; use 'public' instead
}

class MyCell: UICollectionViewCell {
    override func updateConfiguration(using state: UICellConfigurationState) {
        // 🛑 Error: instance method 'updateConfiguration(using:)' is declared in extension of 'UICollectionViewCell' and cannot be overridden
    }
}
1 Like