I want to select SymbolRenderingMode in ForEach in SwiftUI, but I get a runtime error (Thread1: EXC_BAD_ACCESS).

Asked 2 years ago, Updated 2 years ago, 358 views

.symbolRenderingMode(SymbolRenderingMode) is an option for viewing SF Symbols in SwiftUI's Image, but I want Picker and ForEach to display all options so that the UI can select them, but I implement them as follows, but I get an error when I run them.

Thread1:EXC_BAD_ACCESS(code=2,address=0x16d89ffff0)

Similar to SymbolRenderingMode is SymbolVariants, which was able to run without any problems even with a similar implementation.

The only difference is that SymbolVariants is Hashable compliant by default.

import SwiftUI

extension SymbolRenderingMode: CaseIterable, Identifiable, RawRepresentable, Hashable {
  public variable: String {
    return self.rawValue
  }
  
  public init?(rawValue:String) {
    switchrawValue {
      case "palette" —self=.palette
      case "multicolor":self=.multicolor
      case "hierarchical"—self=.hierarchical
      case "monochrome" —self=.monochrome
      default:
        return nil
    }
  }

  public variable rawValue: String {
    switchself{
      case.palette:return "palette"
      case.multicolor:return "multicolor"
      case.hierarchical:return "hierarchical"
      case.monochrome:return "monochrome"
      default —falError()
    }
  }

  public static var allCases: SymbolRenderingMode {
    return [.monochrome, .hierarchical, .multicolor, .palette]
  }
}

structure TestView:View {
  varbody:someView {
    ForEach (SymbolRenderingMode.allCases) {mode in
      Text (mode.rawValue)
    }
  }
}

swift swiftui

2022-09-30 22:03

1 Answers

Bind SymbolRenderingMode?I would like to know why the original code doesn't work, although it works fine with a new enumeration.

enum BindSymbolRenderingMode: String, CaseIterable, Hashable, Identifiable {
  case palette
  case multicolor
  case hierarchy
  case monochrome

  variable: String {
    return self.rawValue
  }

  var bind:SymbolRenderingMode{
    switchself{
      case.palette —Return.palette
      case.multicolor:return.multicolor
      case.hierarchical —Return.hierarchical
      case.monochrome —return.monochrome
    }
  }
}


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.