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

标签 objective-c swift macos

我已经在 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/

相关文章:

iphone - CALayers 没有在其 UIView 的边界更改时调整大小。为什么?

ios - 这是一个什么样的讽刺错误?

swift - 访问 tableview Controller 中的 AppDelegate 函数?

conio.h 的 C 处理器错误

macos - 如何对已经运行的进程执行 "nice"操作?

objective-c - 声音仅在设备上有效,但在模拟器上无效

c# - 帮助理解 C# 代码并移植到 Objective-C

ios - 如何让应用程序居中和/或填满屏幕?

ios - View 在 iPhone plus 机型上显示不正确

ruby-on-rails - 使用 bundler 安装 gem 时传递安装标志