2015-11-25 18 views
1

在我的结构我有以下几点:如何在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] 

于事无补。

EDIT 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:

要澄清,我询问,我怎么可能解决错误的,因为所有其他提供的代码按预期工作。

+0

如果你包含了一些完整的东西,我可以去编译自己来展示问题,这将是有帮助的。 –

+0

var contents:FileSystemObject = [FileSystemObject]?不应该编译!,更新你的问题.... – user3441734

+0

@ user3441734排除上述问题,上面的所有内容都会编译。 –

回答

1

该错误消息是误导性的。

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:如果分割

return it.data[index] 

成两个单独的语句,这个问题变得更加明显 并且不能是该方法的返回值 。 如果想要返回FileSystemObject 那么您必须将其定义为class

+0

好的,谢谢。这样可行。 –

0

简化...

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

然而,这将是这种情况,递归代码只能满足条件运行,因此不会永远运行。 –

相关问题