如何在Swift数组中存储闭包

3
我想把闭包存储在一个数组中。但是我不知道如何做,或者我的想法完全错误。使用下面的设置,我得到了一个错误:
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))
         }
     }

2
我认为问题在于你的函数('readGoodStories',...)是在Reader类中定义的。你需要将它们定义在该类之外或更改“let fileNamesActions:[(filename:String,action:(URL)->())]”为“let fileNamesActions:[(filename:String,action:(Reader)->(URL)->())]”。 - Paprikadobi
但是如果我需要在Reader类内部使用它们怎么办? - Łukasz
1
你需要将 'let fileNamesActions:[(filename:String,action:(URL)->())]' 更改为 'let fileNamesActions:[(filename:String,action:(Reader)->(URL)->())]'。 - Paprikadobi
2
默认初始化在 self 可用之前运行,因此 readGoodStories 尚未匹配到 self。尝试将 fileNamesActions 设为计算属性。 - Cristik
我已经尝试过了。但是 action(url.appendingPathComponent(filename)) 这一行提示“无法将类型为 'URL' 的值转换为预期的参数类型 'UFOFont'”。 - Łukasz
我编辑了一篇文章。实际上,在self可用之前,我正在调用函数。 - Łukasz
1个回答

3

fileNamesActions的声明更改为lazy var,因为您在其赋值中访问了class成员。

class Reader {

    lazy var fileNamesActions:[( filename:String, action:(URL) -> () )]  = [
        (filename:"goodStories.txt", action: readGoodStories),
        (filename:"badStories.txt", action: readBadStories),
        (filename:"stupidStories", action: readStupidStories)]

    init (with url:URL) {
        for (filename, action) in fileNamesActions {
            action(url.appendingPathComponent(filename))
        }
    }

    func readBadStories(from url:URL) {
        print(url.path)
    }

    func readStupidStories(from url:URL) {
        print(url.path)
    }

    func readGoodStories(from url:URL) {
        print(url.path)
    }
}

使用方法

let reader = Reader(with: URL(string: "www.xxxxxxxxxx.com")!)

输出

www.xxxxxxxxxx.com/goodStories.txt
www.xxxxxxxxxx.com/badStories.txt
www.xxxxxxxxxx.com/stupidStories

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接