我想把闭包存储在一个数组中。但是我不知道如何做,或者我的想法完全错误。使用下面的设置,我得到了一个错误:
Cannot convert value of type '(Reader) -> (URL) -> ()' to expected element type '(URL) -> ()'
我不理解它。我的类:
class Reader {
let fileNamesActions:[( filename:String, action:(URL) -> () )] = [
(filename:"goodStories.txt", action: readGoodStories),
(filename:"badStories.txt", action: readBadStories),
(filename:"stupidStories", action: readStupidStories)]
我已经这样声明函数:
func readGoodStories(from url:URL) {
//read, do whatever i want with url
}
...
我称它们为:
init (with url:URL) {
for (filename, action) in fileNamesActions {
action(url.appendingPathComponent(filename))
}
}
self
可用之前运行,因此readGoodStories
尚未匹配到self
。尝试将fileNamesActions
设为计算属性。 - Cristikaction(url.appendingPathComponent(filename))
这一行提示“无法将类型为 'URL' 的值转换为预期的参数类型 'UFOFont'”。 - Łukasz