林轉換代碼斯威夫特試圖轉換到SWIFT代碼,它設置文件屬性設置爲「不備份到iCloud」問題從OBJ-C
原始代碼看起來像這樣
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString {
NSURL* URL= [NSURL fileURLWithPath: filePathString];
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
return success;
}
我來了與這樣的代碼
func addSkipBackupAttributeToItemAtURL(URL: NSURL) -> Bool{
let fileManager = NSFileManager.defaultManager()
assert(fileManager.fileExistsAtPath(URL.absoluteString))
var error:NSError?
let success:Bool = try? URL.setResourceValue(NSNumber(bool: true),forKey: NSURLIsExcludedFromBackupKey)
if !success {
print("Error excluding \(URL.lastPathComponent) from backup \(error)")
} else {
print("File at path \(URL) was succesfully updated")
}
return success
}
但它在線拋出誤差let success:Bool
無法轉換類型'()?'的值指定類型'Bool'
如何解決這個問題?
請不要」不要重複提問。如果給出的答案不令人滿意,請向作者澄清,或*編輯*您的問題以提供更多信息。 –