检查文件存在于Swift中带有文件名前缀的目录中

时间:2015-07-09 09:38:55

标签: swift nsfilemanager

我想在SWIFT中检查我的文件是否只存在文件名的前缀。

E.g

我的文件名与Companies_12344

相同

所以在_值是动态的但是"公司_"是静态的。

我该怎么做?

我已经在

下面完成了分割文件名代码

如何通过NSFileManager检查是否存在文件名"公司_"

我的代码如下为拆分

int FEATURES = 10000;       
OnlineLogisticRegression learningAlgorithm = new OnlineLogisticRegression(20, FEATURES, new L1())
                        .alpha(1).stepOffset(1000).decayExponent(0.9).lambda(3.0e-5).learningRate(20);

1 个答案:

答案 0 :(得分:1)

我认为您需要此代码:

let str = "Companies_12344"

if str.hasPrefix("Companies") {
    println("Yes, this one has 'Companies' as a prefix")
    let compos = str.componentsSeparatedByString("_")
    if let file = compos.first {

        println("There was a code after the prefix: \(file)")
        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String

        var yourPath = paths.stringByAppendingPathComponent("\(file)_")

        var checkValidation = NSFileManager.defaultManager()

        if (checkValidation.fileExistsAtPath(yourPath))
        {
            println("FILE AVAILABLE");
        }
        else
        {
            println("FILE NOT AVAILABLE");
        }
    }
}
相关问题