我试图在Int中增加String的值,但是我不为什么增加一次才发生!这是代码:

class DownloadFile : NSObject {

  var number = 1
init(url : String) {

    urlOfDownload = url
    fileUrl = URL(string: url)!


    //Added by me , checks if file is already exist try to add new file name
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
    let url = NSURL(fileURLWithPath: path)
    let filePath = url.appendingPathComponent(fileUrl.lastPathComponent)!.path
    let fileManager = FileManager.default
    if fileManager.fileExists(atPath: filePath) {


            number += 1

        print("FILE AVAILABLE")
        nameOfDownload = "\(fileUrl.deletingPathExtension().lastPathComponent)\(number).\(fileUrl.pathExtension)"

    } else {

        print("FILE NOT AVAILABLE")
        nameOfDownload = fileUrl.lastPathComponent
    }

}

}

使用类:
let downloadFile = DownloadFile(url: url)
    downloadFile.startDownload()

最佳答案

您需要使自己的var static在类实例之间共享:

class DownloadFile : NSObject {

    static var number = 1
    init(url : String) {
        ...
        DownloadFile.number += 1
        ...
    }
}

09-25 20:44