简体   繁体   English

Swift 3:文件列表Optional()

[英]Swift 3 : List of files Optional()

i'm going nuts. 我要疯了。

I'm trying to get the list of "txt" files from a folder but i get the list in the form of 我正在尝试从文件夹中获取“ txt”文件列表,但我以以下形式获取列表:

Optional("Filename").txt 可选(“文件名”).txt

Here's my code, nothing fancy. 这是我的代码,没什么花哨的。 I tried unwrapping the filename but the compiler gives me an error.I tried a "guard", a with "if", i tried "!" 我尝试解包文件名,但是编译器给我一个错误。我尝试了一个“后卫”,一个“如果”,我尝试了“!” ... nothing works. ...什么都行不通。

   let enumerator:FileManager.DirectoryEnumerator = FileManager().enumerator(atPath: myFolderPath)!

    while let element = enumerator.nextObject() as? String {

        print(element)

        if element.hasSuffix("txt") {

            fileList.append(element)
        }

    }

I need to show this list in a table view. 我需要在表格视图中显示此列表。

I hope i'm not supposed to run trought the array and get the name of the files by using a bunch o string methods just to get rid of this text... 我希望我不应该通过使用一堆o字符串方法运行数组并获取文件名来摆脱这个文本...

I'm not sure what can i do! 我不确定该怎么办! I don't really want to use this solution: 我真的不想使用此解决方案:

Swift: Optional Text In Optional Value Swift:可选值中的可选文本

Thank you 谢谢

@ Vadian and Rob: you guys rock, you asked the right question. // @ Vadian和Rob:你们摇滚,你问对了问题。

The issue was that i'm naming the .txt files with a textfield and i'm doing all the necessary work to make sure that this textfield is not empty or otherwise. 问题是我正在用文本字段命名.txt文件,并且正在做所有必要的工作以确保此文本字段不为空或其他。

I never thought that this textfield could be optional, hence the Optional("Filename").txt 我从没想过这个文本字段可以是可选的,因此是Optional(“ Filename”)。txt

Once i unwrapped the value from the textfield Optional() had gone. 一旦我解包了,文本字段Optional()中的值就消失了。

Thank you. 谢谢。

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

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