gpt4 book ai didi

swift - “AnyHashable”不能转换为 ‘NSObject’

转载 作者:行者123 更新时间:2023-11-28 10:09:44 24 4
gpt4 key购买 nike

我正在将我的 Objective-C 代码转换为 Swift。我有自己的 DataStructure 类,称为“DCclass”,继承自“NSObject”...我已经完成了所有必需的事情,例如:在 Bridging-header 中导入了“DCclass.h”等等,一切都很好,但是在 for in 循环中我遇到了类似类型转换问题的错误。提前致谢。

当前的 Objective-C 代码:

#import “DCclass.h”
@interface LPFPickerCell ()
@property (nonatomic) NSMutableArray *myMutableArray;
@end

@implementation LPFPickerCell
- (void)setSelectedData:(NSString *) selectedString
{
int i = 0;
for (DCclass *dc in self.myMutableArray) /// Swift is showing compile time error here
{
NSLog(@"DCclass.empName = %@",dc.empName);
}
}
@end

转换后的 Swift 代码:

class LPFPickerCell {

var myMutableArray = [AnyHashable]()

func setSelectedData(_ selectedString: String?) {
let i: Int = 0
for dc: DCclass in myMutableArray { ///‘AnyHashable’ is not convertible to ‘DCclass’
print("DCclass.empName = \(dc.empName)")
}
}
}

最佳答案

也许你需要改变你的数组类型

var myMutableArray = [AnyHashable]()

var myMutableArray = [DCclass]()

然后在for循环中你可以像这样访问它

for dc in myMutableArray {    
print("DCclass.empName = \(dc.empName)")
}

关于swift - “AnyHashable”不能转换为 ‘NSObject’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49557977/

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