【发布时间】:2016-08-01 20:16:06
【问题描述】:
注意:我是 Swift 的菜鸟
我正在使用Former。
我正在从领域模型中获取数据。
let industries = realm.objects(Industry)
然后我尝试从中定义InlinePickerItem 的列表:
$0.pickerItems = industries.map({ industry in
return InlinePickerItem(title: industry.name, value: industry.id)
})
但是 XCode 一直说:Cannot convert value of type 'Int' to expected argument type '_?',指向industry.id。
我错过了什么吗?我不知道这个问题是来自Former 还是来自我在Swift 中不理解的东西。比如_?是什么类型?
更新:
@dfri 发表评论后,尝试失败。从我对 Swift 的小理解来看,我知道 Swift 迷路了。所以我从闭包中提取了InlinePickerItem列表的初始化。
let industries = realm.objects(Industry)
let inlinePickerItems = industries.map({ industry in
return InlinePickerItem(title: industry.name, displayTitle: nil, value: industry.id)
})
let catRow = InlinePickerRowFormer<ProfileLabelCell, String>(instantiateType: .Nib(nibName: "ProfileLabelCell")) {
$0.titleLabel.text = "CATEGORY".localized
}.configure {
$0.pickerItems = inlinePickerItems
}
调用InlinePickerItem(title: industry.name, displayTitle: nil, value: industry.id) 时错误消失了,但将它分配给$0.pickerItems 时我得到了一些新的东西,现在是:
Cannot assign value of type '[InlinePickerItem<Int>]' to type '[InlinePickerItem<String>]'
希望这将为您提供一些有用的提示。
【问题讨论】:
-
如果你选择点击
industries,它是什么类型的? -
@vacawama 它的类型为
Industry。 @dfri 谢谢,我会试试这个。 -
@dfri 我已经尝试过您提出的修改,但错误仍然存在。从您提出的建议开始,我已经用一些新的提示更新了帖子。也许你会有新的见解。
-
@dfri 谢谢!我只是将
catRow的类型更改为InlinePickerRowFormer<ProfileLabelCell, Int>,它运行顺利。你想写答案吗? ;)
标签: swift