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

 星魂陨石 发布于 2023-01-11 19:30

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

DiscoverViewController.swift:34:20: Could not find an overload for 'init' that
accepts the supplied arguments

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

以下是Objective-C的示例:

int ex:(NSMutableArray *)in { return in.count; }

以及Swift的一个例子:

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

ex(nil) // does compile

我需要做什么才能将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;

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

1 个回答
  • ex([1,2,3,4]) // doesn't compile
    

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

    ex(nil) // does compile
    

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

    2023-01-11 19:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有