Łukasz
Łukasz

Reputation: 805

How to store closures in a Swift array

I would like to store closures in an array. But I have no clue how to do it, or my thinking is totally wrong. With setup shown below I got an error:

Cannot convert value of type '(Reader) -> (URL) -> ()' to expected element type '(URL) -> ()'

I don't understand it. My class:

class Reader {
    let fileNamesActions:[( filename:String, action:(URL) -> () )]  = [
        (filename:"goodStories.txt", action: readGoodStories),
        (filename:"badStories.txt", action: readBadStories),
        (filename:"stupidStories", action: readStupidStories)]

I have declared functions like this:

    func readGoodStories(from url:URL) {
        //read, do whatever i want with url
    }
    ...

And I call them by:

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

Upvotes: 3

Views: 657

Answers (1)

Kamran
Kamran

Reputation: 15248

Change declaration of fileNamesActions as lazy var because you are accessing the class members in its assignment,

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)
    }
}

Usage

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

Output

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

Upvotes: 3

Related Questions