我目前正在尝试将 DropBox 集成到我的应用程序中。我可以登录并提出文件请求。
现在我想将整个当前目录保存在一个名为 directory 的类中。此类由 DirectoryViewModel 实例化。您可以在代码片段中看到编号的打印,输出显示:
1 2 3 END --- 文件夹:0 文件:0 计数项目:0 4 文件夹计数:4:文件:1
所以顺序是错误的......它应该是 3 - 4 - END 我不知道如何解决这个问题......如果有人能帮我解决这个问题会很棒
class Directory{
var files = [file]()
var folders = [folder]()
init()
{
print("2")
if let client = DropboxClientsManager.authorizedClient {
print("3")
// List folder
client.files.listFolder(path: "").response { response, error in
print("4")
if let result = response {
print("Folder contents:")
for entry in result.entries {
if (self.isFile(testStr: entry.pathLower!)){
print("File detected")
self.files.append(file(pathDisplay: entry.pathDisplay!, pathLower: entry.pathLower!, name: entry.name))
} else {
print("Folder detected")
self.folders.append(folder(pathDisplay: entry.pathDisplay!, pathLower: entry.pathLower!, name: entry.name))
}
}
print("folders count: \(self.folders.count) : files: \(self.files.count)")
} else {
print(error!)
}
}
}
}
}
class file{
var pathDisplay: String
var pathLower: String
var name: String
init(pathDisplay: String, pathLower: String, name: String) {
self.pathDisplay = pathDisplay
self.pathLower = pathLower
self.name = name
}
}
类文件夹{ var pathDisplay:字符串 var pathLower:字符串 var name:字符串
init(pathDisplay: String, pathLower: String, name: String) {
self.pathDisplay = pathDisplay
self.pathLower = pathLower
self.name = name
}
}
class DirectoryViewModel: NSObject {
var items = [FileFolderItem]()
override init() {
super.init()
print("1")
let directory = Directory()
print("END --- folders: \(directory.folders.count) files: \(directory.files.count)")
for folder in directory.folders{
print("fo")
self.items.append(FolderItem(pathDisplay: folder.pathDisplay, pathLower: folder.pathLower, name: folder.name))
}
for file in directory.files{
print("fi")
self.items.append(FolderItem(pathDisplay: file.pathDisplay, pathLower: file.pathLower, name: file.name))
}
print("COUNT ITEMS: \(items.count)")
}
}
DirectoryViewModel 充当表格视图中的数据源,符合 UITableViewDataSource 协议
extension DirectoryViewModel: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].rowCount
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
switch item.type{
case .file:
if let cell = tableView.dequeueReusableCell(withIdentifier: FileCell.identifier, for: indexPath) as? FileCell {
cell.item = item
return cell
}
case .folder:
if let cell = tableView.dequeueReusableCell(withIdentifier: FolderCell.identifier, for: indexPath) as? FolderCell {
print("folder added name: ")
cell.item = item
return cell
}
}
return UITableViewCell()
}