2017-07-13 2 views
1

の中でIntの値をインクリメントしようとしていますが、インクリメントが1回しか発生しないのはなぜですか?ここでのコードは次のとおりです。Swift:文字列内のインクリメントIntの問題

クラスを使用して
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() 
+2

あなたは1に番号を設定し、その後、それを2に増やしてください。あなたはこれからどのような行動を期待していましたか? – Connor

+0

@ConnorGクラスを開始するたびに、数字がFile1、File2、File3 –

答えて

4

あなたのvarstaticは、クラスのインスタンス間でそれを共有するようにする必要があり:

class DownloadFile : NSObject { 

    static var number = 1 
    init(url : String) { 
     ... 
     DownloadFile.number += 1 
     ... 
    } 
} 
関連する問題