gpt4 book ai didi

objective-c - Swift 不从框架中获取 NSMutableArray 类型注释

转载 作者:行者123 更新时间:2023-11-28 06:40:58 25 4
gpt4 key购买 nike

我已经在 objC 中创建了一个框架,并且在主项目的 Swift 文件中为一个类创建了一个扩展。我已经对该类(在框架中)进行了如下注释:

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

在 Swift 中,我按如下方式访问 levels 属性:

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

levels 属性然后被键入为 NSMutableArray 并且 level 变量被键入为 Element

为什么它不显示我在框架中指定的类型?是否需要调整任何设置才能使其正常工作?

最佳答案

来自 Interacting with Objective-C APIs (强调):

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 不在基础类型列表中Swift 中保留了轻量级泛型参数化。

示例: Objective-C 属性

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

由 Swift 导入(如您在 Xcode 菜单中使用“导航 -> 跳转至生成的界面”所见)为

public var anArrayProperty: [String]
public var aMutableArrayProperty: NSMutableArray

关于objective-c - Swift 不从框架中获取 NSMutableArray 类型注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com