2016-04-03 35 views
0

我試圖檢查當前桌面牆紙是否具有特定文件名。我想通過將文件名追加到desktopImageURLForScreen並使用fileExistsAtPath來檢查該文件是否存在(不是完美的解決方案,但它適用於我的目的)。這裏是我的代碼:文件路徑中的空間被替換爲(null)

guard let wallpaperDirectoryURL = workspace.desktopImageURLForScreen(screen) 
    else { NSLog("Error getting desktop image URL."); return } 

let URLToCheckIfExists = wallpaperDirectoryURL.URLByAppendingPathComponent("\(lastId).jpg") 
let stringToCheckIfExists = URLToCheckIfExists.absoluteString 
print(stringToCheckIfExists) 
let lastIdIsCurrentWallpaper = NSFileManager.defaultManager().fileExistsAtPath(stringToCheckIfExists) 

我的問題是,當有在文件路徑中有空格,文件路徑字符串後替換空間和字符用(空)。

因此,而不是一個路徑,如:

/Users/Joe/Library/Application Support/Wallpaperer/Wallpapers/4d65y1.jpg

我得到:

/Users/Joe/Library/Application(null)upport/Wallpaperer/Wallpapers/4d65y1.jpg

這到底是怎麼回事?我怎樣才能找到正確的道路?

回答

0

嗯,我很快發現了這個問題。我應該使用URLToCheckIfExists.path而不是URLToCheckIfExists.absoluteString