简体   繁体   中英

Swift NSTableView, NSFileManager, trying to load file names into TableView, and having problems with optionals

Trying to load contents of a directory into tableview.

Using: NSFileManager and NSTableView.

Getting Error Message in the last function:

'[AnyObject]?' does not have a member named 'subscript'

Do I miss something regarding Optionals?

Code:

import Cocoa

let fileManager = NSFileManager.defaultManager()

let folderURL = NSURL.fileURLWithPath("/Applications/")

var error : NSError? = nil

let folderContents: [AnyObject]? = fileManager.contentsOfDirectoryAtURL(folderURL!, includingPropertiesForKeys:nil, options:NSDirectoryEnumerationOptions(), error:&error)

class tonik: NSObject, NSTableViewDataSource {

func numberOfRowsInTableView(tableView: NSTableView) -> Int {
 folderContents?.count
}

func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
     folderContents[row]
    }
}

Now it compiles.

I created object in xib file and table view, used the tonik class as datasource for the tableview, now I am getting message:

2015-01-10 07:21:18.420 ac3[6016:143038] *** Illegal NSTableView data source (). Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:

but both functions are implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM