arrays - 如何在 Swift 2 中使结构成为可订阅的?

标签 arrays swift macos swift2 subscript

在我的结构中,我有以下内容:

subscript(index: Int) -> FileSystemObject {
    var i: Int = 0
    for a in contents! {
        if (i == index) {
            return a
        }
        i++
    }
}

在哪里,

var contents: FileSystemObject = [FileSystemObject]?

但是什么时候,

let it: FileSystemObject = FileSystemObject()

然后我写:

return it.contents![index]

我收到错误

Cannot subscript a value of type [FileSystemObject]

我在这里做错了什么?

此外,请注意:

用值改变每个对象

FileSystemObject

为了,

[FileSystemObject]

没有帮助。

编辑 1:

这是完整的代码:

MainWindowController.swift

class MainWindowController: NSWindowController, NSOutlineViewDataSource, NSOutlineViewDelegate {
@IBOutlet weak var sourceView: NSOutlineView!

static var fileManager: NSFileManager = NSFileManager.defaultManager()
static var fileSystem: FileSystemObject = FileSystemObject(path: "/", fs: fileManager)
var outlineSource: OutlinePrep = OutlinePrep(fs: fileSystem)

override func windowDidLoad() {
    super.windowDidLoad()

    sourceView.setDataSource(self)
    sourceView.setDelegate(self)

}

func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
    guard let it = item as? OutlinePrep else {
        return outlineSource.basePath
    }
    return it.data[index]
}

func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
    // return (item == nil) ? YES : ([item numberOfChildren] != -1);
    print(item)
    guard let it = item as? OutlinePrep else {

        return false
    }
    for (var i: Int = 0; i < it.data.count; i++) {
        guard let _ = it.data[i].contents else {
            return false
        }
    }
    return true
}

func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
    guard let it = item as? OutlinePrep else {
        return outlineSource.data.count
    }
    var i: Int = 0
    for a in it.data {
        guard let _ = a.contents else {
            continue
        }
        i++
    }
    return i
}
}

文件系统.swift

struct FileSystemObject {
let basePath: String
let name: String
var isDir = ObjCBool(false)
var contents: [FileSystemObject]?

init(path: String, fs: NSFileManager) {
    basePath = path
    let root: [String]
    fs.fileExistsAtPath(path, isDirectory: &isDir)
    if (isDir.boolValue) {
        do {
            root = try fs.contentsOfDirectoryAtPath(path)
        }
        catch {
            root = ["Error"]
        }
        contents = []
        for r in root {
            contents!.append(FileSystemObject(path: (path + (r as String) + "/"), fs: fs))
        }
    }

    name = path
}

subscript(index: Int) -> FileSystemObject {
    get {
        let error: FileSystemObject = FileSystemObject(path: "", fs: NSFileManager.defaultManager())
        guard let _ = contents else {
            return error
        }
        var i: Int = 0
        for a in contents! {
            if (i == index) {
                return a
            }
            i++
        }
        return error
    }
    set {

    }
}
}

大纲.swift

struct OutlinePrep {
var data: [FileSystemObject]
let basePath: String
private var cell: Int = -1

init (fs: FileSystemObject) {
    data = fs.contents!
    basePath = fs.basePath
}

mutating func outlineDelegate() -> String {
    cell++
    return data[cell].name
}

func testFunc(data: [FileSystemObject]) {
    for (var i: Int = 0; i < data.count; i++) {
        guard let d = data[i].contents else {
            print(data[i].name)
            continue
        }

        testFunc(d)
    }
}
}

编辑 2:

为了澄清,我正在询问如何解决错误,因为所有其他提供的代码都按预期工作。

最佳答案

错误信息具有误导性。问题变得更加明显 如果你 split

return it.data[index]

分为两个单独的语句:

func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
    guard let it = item as? OutlinePrep else {
        return outlineSource.basePath
    }
    let fso = it.data[index]
    return fso // error: return expression of type 'FileSystemObject' does not conform to 'AnyObject'
}

it.data[index] 的值是一个 FileSystemObject,它是一个 struct,因此它 不符合AnyObject,不能是返回值 那种方法。 如果你想返回一个FileSystemObject 那么您必须将其定义为 class

关于arrays - 如何在 Swift 2 中使结构成为可订阅的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33907341/

相关文章:

c - 如何选择何时打印字符数组中的特定字符

ios - MQTT-in-iOS - mqttDidDisconnect : Socket closed by remote peer

swift - 更改 UISearchBar 内 UItextField 中图标的颜色

swift - 向下滚动 UITableView 时使用 Kingfisher Swift 加载 Gif 图像会导致延迟

arrays - JADE、Nodejs 和 JSON 数据渲染

java - java中数组的转换

c - 对于数组,为什么会出现 a[5] == 5[a] 的情况?

node.js - 无法在 Node 中获得颜色输出

swift - 为什么我不能在 Playground 中使用 AVURLAsset 从媒体文件中提取数据?

bash - 递归清空多个文件的命令