Swift UI - 线程 1:EXC_BAD_ACCESS(代码=2

Swift UI - Thread 1: EXC_BAD_ACCESS (code=2

我在我的驱动器列表中添加了系统驱动器图标 (NSImage)

现在应用程序崩溃 AppDelegate.swift 在第 13 行 class AppDelegate: NSObject, NSApplicationDelegate {Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3fff48)

我知道这是我要添加的图像,因为当我用文本替换它时没问题

这是我添加图片的 class。

import SwiftUI

let workspace = NSWorkspace.init()

struct DriveList: View {
   let drives = SDCardTools.getDrives()!
   var body: some View {
       return List(drives) { drive in
           DriveRow(drive : drive)
       }
   }
}

struct DriveRow: View {
   var drive : Drive
   
   var body: some View {
       HStack {
           DriveIcon(path : drive.path)
               padding(10)
           Text(drive.name)
       }
   }
}

struct DriveIcon : View {
   
   var path: String
   var body: some View {
       Image(nsImage: workspace.icon(forFile: path ))
           .resizable()
           .frame(width: 50, height: 50)
   }
}

struct DriveList_Previews: PreviewProvider {
   static var previews: some View {
       DriveList()
   }
}

线程 1 队列:com.apple.main-线程(串行) #0 0x00007fff44382883 专用静态 EnvironmentReadingView._makeView(view:inputs:) () #1 0x00007fff44384668 在静态协议见证中 View._makeView(view:inputs:) in conformance Image () #2 0x00007fff44384610 在静态协议见证中 View._makeView(view:inputs:) in conformance Image () #3 0x00007fff441c20e9 在 TypedUnaryViewGenerator.makeView(in:inputs:id:indirectMap:) () #4 0x00007fff441c21f9 在 UnaryViewGenerator.makeView(in:inputs:id:indirectMap:) 的协议见证中符合 TypedUnaryViewGenerator () #5 0x00007fff441bc9b0 闭包#1 in UnaryElements.makeElements(from:in:inputs:indirectMap:body:) () #6 0x00007fff441c4801 in partial apply for closure #1 in UnaryElements.makeElements(from:in:inputs:indirectMap:body:) () #7 0x00007fff441bf61b 闭包#1 闭包#1 闭包#1 闭包#1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #8 0x00007fff441ce4c5 in partial apply for closure #1 in closure #1 in closure #1 in closure #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #9 0x00007fff445adb15 在专门的静态 UnaryLayout<>.makeViewImpl(modifier:inputs:body:) () #10 0x00007fff4430e339 in specialized static UnaryLayout._makeView(modifier:inputs:body:) () #11 0x00007fff4430f101 in protocol witness for static ViewModifier._makeView(modifier:inputs:body:) in conformance _AspectRatioLayout () #12 0x00007fff4430eec8 in protocol witness for static ViewModifier._makeView(modifier:inputs:body:) in conformance _FrameLayout () #13 0x00007fff441bf2a5 闭包 #1 闭包 #1 闭包 #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #14 0x00007fff441ce43d in partial apply for closure #1 in closure #1 in closure #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #15 0x00007fff441813ef 闭包 #2 in static _Layout<>.makeStaticView(root:inputs:list:) ()

如果您能提供任何帮助,我们将不胜感激:)

这个错误非常令人困惑,它是由于 padding 修饰符的不可分割的性质...并且编译器传递它 w/o 实际使用它作为修饰符,导致崩溃。

这里是修复

struct DriveRow: View {
    var drive : Drive
    
    var body: some View {
        HStack {
            DriveIcon(path : drive.path)
                .padding(10)            // << in this line you missed '.' dot
            Text(drive.name)
        }
    }
}