objective-c CFURLCopyResourcePropertyForKey 失败,因为传递的 URL 没有方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26080120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CFURLCopyResourcePropertyForKey failed because passed URL no scheme
提问by Jon
I have a program that saves a file to the iCloud and this has worked great for iOS7, but now I get this error with iOS8 and cannot seem to find the answer on how to fix it. Anyone else had this problem? Any ideas would be greatly appreciated.
我有一个将文件保存到 iCloud 的程序,这对 iOS7 效果很好,但现在我在 iOS8 上遇到了这个错误,似乎找不到如何修复它的答案。还有其他人有这个问题吗?任何想法将不胜感激。
The Error:CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme: /var/mobile/Containers/Data/Application/ASFHDDE3-B1BB-41D7-A47C-DCC328362W21/Documents/mypictostore.png
错误:CFURLCopyResourcePropertyForKey 失败,因为它传递了这个没有方案的 URL:/var/mobile/Containers/Data/Application/ASFHDDE3-B1BB-41D7-A47C-DCC328362W21/Documents/mypictostore.png
The Line of Code Throws Error:[fileManager setUbiquitous:YES itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:backupName] error:&theError];
代码行抛出错误:[fileManager setUbiquitous:YES itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:backupName] 错误:&theError];
URLS:destinationURL: file:///private/var/mobile/Library/Mobile%20Documents/ABC23455~MY-Program/ backupUrl: /var/mobile/Containers/Data/Application/ASDFGEEW-B1BB—6FR6-A47C-DCCF21876D36/Documents/mypic.png
URLS:destinationURL:file:///private/var/mobile/Library/Mobile%20Documents/ABC23455~MY-Program/backupUrl:/var/mobile/Containers/Data/Application/ASDFGEEW-B1BB—6FR6-A47C-DCCF21876D36/文件/mypic.png
Thank you, Jon
谢谢你,乔恩
回答by Jorge Irún
For me this problem was fixed by adding
对我来说,这个问题是通过添加解决的
file://
right before the file path address like this:
就在文件路径地址之前,如下所示:
var filePath = "file://\(fileURLpath)"
回答by leonard
Or maybe you can use NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)instead of using NSURL(string: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)
或者也许你可以使用NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)而不是使用NSURL(string: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)
回答by Renato Ioshida
Look this link for Objective-c answer: CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory
查看此链接以获取 Objective-c 答案:CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory
Older Swift version answer:
较旧的 Swift 版本答案:
var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"
var url:URL = URL.init(fileURLWithPath: str)
Swift 4 Answer:
斯威夫特 4 答案:
var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"
var url:URL = URL(string: str)!
回答by huangqibiao
look, I find this,The urlis The location to write the data into. we should tell the system a file path.
看,我发现这个,url是The location to write the data into。我们应该告诉系统 a file path.
- var filePath = "file://(fileURLpath)"
- var url:URL = URL.init(fileURLWithPath: fileURLpath)
- var filePath = "file://(fileURLpath)"
- var url:URL = URL.init(fileURLWithPath: fileURLpath)
回答by iNoob
You can also try this, I think the following code is great as if you are not sure any particular url is correct or not for the CFURLCopyResourcePropertyForKey
你也可以试试这个,我认为下面的代码很棒,就好像你不确定任何特定的 url 对于 CFURLCopyResourcePropertyForKey 是否正确
if testURL.isFileURL == false {
testURL = URL(fileURLWithPath: testURL.absoluteString)
}
回答by zavidovych
var url = USL(fileUrlWithString: "/private/var/...")
var url = USL(fileUrlWithString: "/private/var/...")

