ios - 我该如何修复 "Data(contentsOf: url) crashing app"?

标签 ios swift xcode

当我运行 data = try! Data(contentsOf: url) 我的应用程序崩溃了,我收到了这个错误。我尝试添加 UIConstraintBasedLayoutDebugging 断点,但没有太大帮助。

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-09-23 15:59:55.228440-0600 Biobot[15968:547259] -[LGSideMenuController isLoading]: unrecognized selector sent to instance 0x7f9cf90a1a00
Warning: hit breakpoint while running function, skipping commands and conditions to prevent recursion.
error: Execution was interrupted, reason: breakpoint 4.1.
The process has been returned to the state before expression evaluation.

我的代码中甚至没有 [LGSideMenuController isLoading]。这次我真的迷路了,因为今天早上代码运行良好。我将我的 xcode 更新到版本 11,我不知道这是否会导致问题

最佳答案

更新:

您应该仅在从本地存储加载数据时使用 Data(contentsOf: url)(因此只能使用以 file:// 开头的 url)。当您从 Internet 加载数据时,您应该使用 URLSession:

URLSession.shared.dataTask(with: url) { (data, response, error) in

    DispatchQueue.main.async { //all changes to UI must be called on main thread
        if let error = error {
            print("Error: \(error)")
            return
        }
        //transform your data and update UI
        self.label.text = "Loaded data: \(data)"
    }
}.resume()

其中 self.label.text = "Loaded data:\(data)" 是您可以对获取的数据执行的操作的一些示例。


尽量避免 try!。而不是使用 do{} catch {}。您的代码可能不会按预期工作(它不会解决问题,但您将能够获得更多详细信息)。所以在你的代码中做:

do {
 data = try Data(contentsOf: url)
} catch {
 print("\(error)")
}

对我来说,您的错误看起来与数据加载无关。它看起来更像是内存处理问题,因为您有一个指向 LGSideMenuController 实例的指针,并且您(或您正在使用的库之一)尝试调用 isLoading 方法。

关于ios - 我该如何修复 "Data(contentsOf: url) crashing app"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070838/

相关文章:

ios - Objective C iPhone 编程一个点或形状来跟随你的光标

将 NSObject 子类转换为非 objc 协议(protocol)时,Swift 崩溃

json - SwiftyJSON 遍历 JSON 对象数组

iphone - 在 xcode 的 Storyboard 中添加和控制 View Controller

ios - 如何确定当前的iPhone/设备型号?

ios - Swift:调整 iPad 屏幕导航栏的大小

ios - 为图表添加额外的左右偏移

ios - 我不能不隐藏键盘

ios - 静态库抛出链接器错误 : "No such file or directory"

ios - ADMOB 内存泄漏?