【发布时间】:2026-02-13 03:05:04
【问题描述】:
我正在 swift 上编写一个 iOS 应用程序,它从 URL 下载视频并将其写入磁盘。我正在获取数据,但迄今为止未能成功写入磁盘。下面是代码:
let yourURL = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_10mb.mp4")
//Create a URL request
let urlRequest = NSURLRequest(URL: yourURL!)
//get the data
var theData = NSData();
do{
theData = try NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: nil)
}
catch let err as NSError
{
}
try! PHPhotoLibrary.sharedPhotoLibrary().performChangesAndWait({ ()-> Void in
if #available(iOS 9.0, *) {
PHAssetCreationRequest.creationRequestForAsset().addResourceWithType(PHAssetResourceType.Video, data: theData, options: nil)
print("SUCESS");
} else {
};
});
我收到以下错误,感谢任何见解:
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=-1 "(null)": file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.1/src/swift/stdlib/public/core/ErrorType.swift, line 54
【问题讨论】:
-
不要使用
try!,而是使用do-try-catch模式并打印生成的error对象。它可能会给你一个更有意义的错误信息。 -
我在尝试时遇到类似的错误:Error Domain=NSCocoaErrorDomain Code=-1 "(null)"
-
我是 iOS 编程新手,所以我仍在学习中。对不起,您指的是localizedDescription 和userInfo 是什么?是的,该应用已请求并有权访问照片库。
-
当我打印出本地化描述时,我收到以下消息:“操作无法完成。(Cocoa 错误 -1。)”
-
是的,当我设置断点检查时,数据不为零。
标签: ios objective-c iphone swift xcode