在我的代码,我得到以下错误: // cannot convert value of type 'NSURL' to expected argument type 'String'
和 // Extra argument 'error' in call
无法将类型的价值“NSURL”预期参数类型“字符串”
class ScoreManager {
var scores:Array<Score> = [];
init() {
// load existing high scores or set up an empty array
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let path = documentsURL.URLByAppendingPathComponent("Scores.plist")
let fileManager = NSFileManager.defaultManager()
// check if file exists
if !fileManager.fileExistsAtPath(path) { // cannot convert value of type 'NSURL' to expected argument type 'String'
// create an empty file if it doesn't exist
if let bundle = NSBundle.mainBundle().pathForResource("DefaultFile", ofType: "plist") {
fileManager.copyItemAtPath(bundle, toPath: path, error:nil) // Extra argument 'error' in call
}
}
需要实现斯威夫特2不要试图捕捉错误处理 –
BTW命名NSURL'path'是完全误导性的,将它改为'let scoreURL = documentsURL.URLByAppendingPathComponent(“Scores.plist”)',如果你需要路径,只需使用'scoreURL.path!' –