How does work on Swift? about class func return Self

Through Xcode's own controlparts feature, the header file of NSCollectionLayoutGroup below UIKit is converted into Swift file, and an interesting problem was found.

Class func is the type that requires a range of Self keywords, while NSCollection LayoutItem is not requiret Init.

I want to know, if I want to support the implementation of this feature with Swift, how this code syntax works.

open class NSCollectionLayoutItem : NSObject, NSCopying {

    public convenience init(layoutSize: NSCollectionLayoutSize) {
        
    }

    public convenience init(layoutSize: NSCollectionLayoutSize, supplementaryItems: [NSCollectionLayoutSupplementaryItem]) {
        
    }
    
    open var contentInsets: NSDirectionalEdgeInsets
    @NSCopying open var edgeSpacing: NSCollectionLayoutEdgeSpacing?
    open var layoutSize: NSCollectionLayoutSize { get }
    open var supplementaryItems: [NSCollectionLayoutSupplementaryItem] { get }
}

open class NSCollectionLayoutGroup : NSCollectionLayoutItem, NSCopying {

    
    open class func horizontal(layoutSize: NSCollectionLayoutSize, subitem: NSCollectionLayoutItem, count: Int) -> Self {
        /** 
        How is work? cuz Swift need Self.init() by 'required init', then will work.
        The farther class is NSCollectionLayoutItem, but it's not have requiret init, how does Apple make it works?
        */
        Self.init() //How implementation?
    }

    
    open class func horizontal(layoutSize: NSCollectionLayoutSize, subitems: [NSCollectionLayoutItem]) -> Self {
        //How is work?
    }

    open class func vertical(layoutSize: NSCollectionLayoutSize, subitem: NSCollectionLayoutItem, count: Int) -> Self {
        //How is work?    
    }

    open class func vertical(layoutSize: NSCollectionLayoutSize, subitems: [NSCollectionLayoutItem]) -> Self {
        //How is work?
    }

    open class func custom(layoutSize: NSCollectionLayoutSize, itemProvider: @escaping NSCollectionLayoutGroupCustomItemProvider) -> Self {
        //How is work?
    }

    
    open var supplementaryItems: [NSCollectionLayoutSupplementaryItem]
    @NSCopying open var interItemSpacing: NSCollectionLayoutSpacing?
    open var subitems: [NSCollectionLayoutItem] { get }
    open func visualDescription() -> String
}

There is a key background to note here, I have tried to put NSCollection LayoutItem (note here is Open's requiret Init. Set to interna, this will allow Apple's API design to ensure that the requirent init interface is not seen in the external SDK. But all of the above failed together. Who can help, how does this design work, or is it still a bug that appears when converting Objective-C?

Terms of Service

Privacy Policy

Cookie Policy