1

I've created a framework in objC and I'm creating an extension for a class in a Swift file in the main project. I've annotated the class (in the framework) as follows:

@interface GerberStepAndRepeat : GerberElement <GerberBlock>
@property (nonatomic,strong,nonnull) NSMutableArray<GerberLevel *> *levels;
@end

In Swift I access the levels property as follows:

extension GerberStepAndRepeat {
    func flatten() {
        for level in self.levels {
        }
    }
}

The levels property is then typed as NSMutableArray and the level variable is typed as Element.

Why doesn't it show the types that I've specified in the framework? Are there any settings I need to tweak to make this work?

0

1 Answer 1

1

From Interacting with Objective-C APIs (emphasis added):

Objective-C declarations of NSArray, NSSet and NSDictionary types using lightweight generic parameterization are imported by Swift with information about the type of their contents preserved.
...
Aside from these Foundation collection classes, Objective-C lightweight generics are ignored by Swift. Any other types using lightweight generics are imported into Swift as if they were unparameterised.

NSMutableArray is not in the list of Foundation types whose lightweight generic parameterization is preserved in Swift.

Example: The Objective-C properties

@property(strong, nonatomic, nonnull) NSArray<NSString *> *anArrayProperty;
@property(strong, nonatomic, nonnull) NSMutableArray<NSString *> *aMutableArrayProperty;

are imported by Swift (as you can see using "Navigate -> Jump to Generated Interface" in the Xcode menu) as

public var anArrayProperty: [String]
public var aMutableArrayProperty: NSMutableArray
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.