代码之家  ›  专栏  ›  技术社区  ›  Jalil

如何修复“数据(contentsOf:url)崩溃应用程序”?

  •  0
  • Jalil  · 技术社区  · 7 年前

    当我跑的时候 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,我不知道这是否会导致问题

    0 回复  |  直到 7 年前
        1
  •  2
  •   Maciej Gad    7 年前

    更新:

    你应该使用 Data(contentsOf: url) 仅当从本地存储加载数据时(因此只有以 file:// ).从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 方法。