SwiftUI - Logging on preview

Updated
Jun 7, 2022 10:41 PM
Created
Jun 7, 2022 10:41 PM
Tags
SwiftUI.framework
Attributes

prototyping

enum Log {
  
  static func debug(_ objects: Any...) {
    let string = objects.map { "\($0)" }.joined(separator: ", ")
    LogStorage.shared.logs.append("\n")
    LogStorage.shared.logs.append(string)
  }
}

final class LogStorage: ObservableObject {
  static let shared = LogStorage()
  
  @Published var logs: String = ""
}

struct LogView: View {
  
  @ObservedObject var storage = LogStorage.shared
      
  var body: some View {
    ScrollView {
      TextField("", text: $storage.logs, axis: .vertical)
        .disabled(true)
        .font(.system(size: 9))
    }
  }
}

image
image