iosswiftclosuresinit-parameters

Passing a closure as a init parameter in Swift


I'm trying to convert an Objective-C code snippet I found on this article. Here's the original code.

.h file

#import <Foundation/Foundation.h>

typedef void (^TableViewCellConfigureBlock)(id cell, id item);

@interface ArrayDataSource : NSObject <UITableViewDataSource>

- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;

@end

.m file

@interface ArrayDataSource ()

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;

@end


@implementation ArrayDataSource

- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
    self = [super init];
    if (self) {
        self.items = anItems;
        self.cellIdentifier = aCellIdentifier;
        self.configureCellBlock = [aConfigureCellBlock copy];
    }
    return self;
}

@end

Here's my attempt.

import Foundation
import UIKit

public class TableViewDataSource: NSObject, UITableViewDataSource {

    var items: [AnyObject]
    var cellIdentifier: String
    var TableViewCellConfigure: (cell: AnyObject, item: AnyObject) -> Void

    init(items: [AnyObject]!, cellIdentifier: String!, configureCell: TableViewCellConfigure) {
        self.items = items
        self.cellIdentifier = cellIdentifier
        self.TableViewCellConfigure = configureCell

        super.init()
    }

}

But I get an error at this line self.TableViewCellConfigure = configureCell saying Use of undeclared type 'TableViewCellConfigure'.

I tried another way. Instead of declaring a variable for the closure, I declared it as a typealias.

typealias TableViewCellConfigure = (cell: AnyObject, item: AnyObject) -> Void

But then I get a new error at the same line above saying 'TableViewDataSource' does not have a member named 'TableViewCellConfigure'.

Can anyone please help me out to resolve this?

Thank you.


Solution

  • The problem is that you have confused TableViewDataSource with TableViewCellConfigure in your init. This compiles fine for me:

    typealias TableViewCellConfigure = (cell: AnyObject, item: AnyObject) -> Void // At outer level
    
    class TableViewDataSource: NSObject/*, UITableViewDataSource */ { // Protocol commented out, as irrelevant to question
    
        var items: [AnyObject]
        var cellIdentifier: String
        var tableViewCellConfigure: TableViewCellConfigure // NB case
    
        init(items: [AnyObject], cellIdentifier: String!, configureCell: TableViewCellConfigure) {
            self.items = items
            self.cellIdentifier = cellIdentifier
            self.tableViewCellConfigure = configureCell
    
            super.init()
        }
    
    }
    

    Note also that you used uppercase for the property name tableViewCellConfigure - I've changed it, as it was confusing me, and, possibly, you!