CFURLCopyResourcePropertyForKey失败,因为传递的URL没有方案

Jon*_*Jon 34 objective-c xcode6 ios8

我有一个程序可以将文件保存到iCloud,这对iOS7很有用,但现在我在iOS8上遇到这个错误,似乎无法找到解决方法.还有其他人有这个问题吗?任何想法将不胜感激.

错误: CFURLCopyResourcePropertyForKey失败,因为它传递了这个没有方案的URL:/var/mobile/Containers/Data/Application/ASFHDDE3-B1BB-41D7-A47C-DCC328362W21/Documents/mypictostore.png

代码行引发错误: [fileManager setUbiquitous:YES itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@"Documents"isDirectory:true] URLByAppendingPathComponent:backupName] error:&theError];

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

谢谢你,乔恩

Jor*_*rún 73

对我来说,这个问题通过添加来解决

file://
Run Code Online (Sandbox Code Playgroud)

就在文件路径地址之前,如下所示:

var filePath = "file://\(fileURLpath)"
Run Code Online (Sandbox Code Playgroud)


leo*_*ard 17

或者也许你可以使用NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)而不是使用NSURL(string: NSBundle.mainBundle().pathForResource("mypictostore", ofType: "png")!)

  • 在路径前添加"file:///" (3认同)

Ren*_*ida 10

查看Objective-c答案的此链接:在NSDocumentDirectory上禁用数据备份时CFURLSetResourcePropertyForKey失败

较旧的Swift版本答案:

var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"

var url:URL = URL.init(fileURLWithPath: str)
Run Code Online (Sandbox Code Playgroud)

Swift 4答案:

var str:String = "/var/mobile/Containers/Data/Application/3039975A-5E05-4A4C-8000-55C681A7C35F/Documents/index.html"

var url:URL = URL(string: str)!
Run Code Online (Sandbox Code Playgroud)


mic*_*all 9

根据您需要对路径执行的操作,您可能需要在前面添加方案file://。请参阅文档/基础/网址了解更多信息

如果您需要文件路径,请使用fileURLWithPath

let imageURL = URL(fileURLWithPath: imagePath)
Run Code Online (Sandbox Code Playgroud)

它会给你路径

文件:///var/mobile/Containers/Data/Application/7C1D854B-8A2E-4FF0-BD30-0652AEE10B6F/Documents/image_8ZMAM.jpg


如果您需要不带方案的路径,请使用string

let imageURL = URL(string: imagePath)
Run Code Online (Sandbox Code Playgroud)

它会给你路径

/var/mobile/Containers/Data/Application/7C1D854B-8A2E-4FF0-BD30-0652AEE10B6F/Documents/image_8ZMAM.jpg


hua*_*iao 5

看,我找到了,urlThe location to write the data into。我们应该告诉系统一个file path

  • var filePath =“ file://(fileURLpath)”
  • var url:URL = URL.init(fileURLWithPath:fileURLpath)

数据