简体   繁体   English

如何在Swift 2中使结构成为下标?

[英]How to make struct a subscriptable in Swift 2?

Within my struct I have the following: 在我的结构中,我有以下内容:

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

Where, 哪里,

var contents: FileSystemObject = [FileSystemObject]?

But when, 但当,

let it: FileSystemObject = FileSystemObject()

And I write: 我写:

return it.contents![index]

I receive the error 我收到错误

Cannot subscript a value of type [FileSystemObject] 无法下标[FileSystemObject]类型的值

What am I doing wrong here? 我在这里做错了什么?

Additionally, note that: 此外,请注意:

Changing each of the objects with the value 使用值更改每个对象

FileSystemObject

To, 至,

[FileSystemObject]

Does not help. 没有帮助。

EDIT 1: 编辑1:

This is the entirety of the code: 这是完整的代码:

MainWindowController.swift 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
}
}

FileSystem.swift FileSystem.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 {

    }
}
}

Outline.swift Outline.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)
    }
}
}

EDIT 2: 编辑2:

To clarify, I am inquiring as to how I might resolve the error, as all other provided code works as intended. 为了澄清,我正在询问如何解决该错误,因为所有其他提供的代码均按预期工作。

The error message is misleading. 该错误信息是令人误解的。 The problem becomes more apparent if you split 如果您拆分,问题将变得更加明显

return it.data[index]

into two separate statements: 分为两个独立的语句:

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

The value of it.data[index] is a FileSystemObject , which is a struct and therefore it does not conform to AnyObject and cannot be the return value of that method. it.data[index]的值是FileSystemObject ,它是一个struct ,因此不符合AnyObject规定,并且不能是该方法的返回值。 If you want to return a FileSystemObject then you have to define that as a class instead. 如果要返回FileSystemObject则必须将其定义为class

simplified ... 简化...

struct FileSystemObject {
    var context: [FileSystemObject]?
    init() {
        context = []
        // your init is called recursively, forever ...
        let fso = FileSystemObject()
        context?.append(fso)
    }
}
let s = FileSystemObject()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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