在Swift中为Objective C函数提供NSMutableArray类型

eat*_*hil 1 objective-c ios swift

我有一个用Objective C编写的库函数,它带有一个指向NSMutableArray的指针.但是,当我尝试使用Swift数组调用它时,我收到此错误:

DiscoverViewController.swift:34:20: Could not find an overload for 'init' that
accepts the supplied arguments
Run Code Online (Sandbox Code Playgroud)

但是,如果我通过nil,它的确有效.那讲得通.

以下是Objective-C的示例:

int ex:(NSMutableArray *)in { return in.count; }
Run Code Online (Sandbox Code Playgroud)

以及Swift的一个例子:

ex([1,2,3,4]) // doesn't compile

ex(nil) // does compile
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能将Swift数组转换/转换为我的库函数正在寻找的NSMutableArray类型?

以下是我的示例过于简化的完整方法签名:

- (id) initWithTitle: (NSString *) title place: (PublicPlaceInfo *) place
datetimeInterval: (DateTimeInterval *) datetimeInterval costBracket: (int)
costBracket creatorId: (PersonID) creatorId cover: (Url) cover experienceId: 
(ExperienceID) experienceId numJoined: (int32_t) numJoined relatedJoined: 
(NSMutableArray *) relatedJoined;
Run Code Online (Sandbox Code Playgroud)

然后我知道它在relatedJoined部分失败了,因为我将所有其他可能的匿名实例转换为变量.我不得不说,这肯定不是最有帮助的编译错误..

Tom*_*rdt 6

ex([1,2,3,4]) // doesn't compile
Run Code Online (Sandbox Code Playgroud)

这是因为你传递的是普通数组,而不是可变数组.首先尝试将其转换为可变数组:ex(NSMutableArray([1,2,3,4]))

ex(nil) // does compile
Run Code Online (Sandbox Code Playgroud)

nil是一个有效的论据,NSMutableArray?它是有效的.