簡體   English   中英

如何在Swift 2中使結構成為下標?

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

在我的結構中,我有以下內容:

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]

我收到錯誤

無法下標[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
}
}

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

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:

為了澄清,我正在詢問如何解決該錯誤,因為所有其他提供的代碼均按預期工作。

該錯誤信息是令人誤解的。 如果您拆分,問題將變得更加明顯

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

簡化...

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