I have these errors: 'NSApplication' is unavailable in Mac Catalyst…
- User
I have these errors: 'NSApplication' is unavailable in Mac Catalyst 'didUpdateNotification' is unavailable in Mac Catalyst 'shared' is unavailable in Mac Catalyst 'currentEvent' is unavailable in Mac Catalyst 'charactersignoring Modifiers' is unavailable in Mac Catalyst 'NSUpArrowFunctionKey' is unavailable in Mac Catalyst 'NSDownArrowFunctionKey' is unavailable in Mac Catalyst 'NSLeftArrowFunctionKey' is unavailable in Mac Catalyst 'NSRightArrowFunctionKey' is unavailable in Mac Catalyst; With this swift code: import SwiftUI
struct ContentView: View { @State private var position = CGPoint(x: 200, y: 200) let size: CGFloat = 50
var body: some View { Rectangle() .frame(width: size, height: size) .position(position) .foregroundColor(.red) .onReceive(NotificationCenter.default.publisher(for: NSApplication.didUpdateNotification)) { _ in guard let arrowKey = ArrowKey.current else { return } switch arrowKey { case .left: position.x -= 10 case .right: position.x += 10 case .up: position.y -= 10 case .down: position.y += 10 } } }}
enum ArrowKey { case up, down, left, right
static var current: ArrowKey? { guard let characters = NSApplication.shared.currentEvent?.charactersIgnoringModifiers else { return nil } switch characters { case String(Character(UnicodeScalar(NSUpArrowFunctionKey)!)): return .up case String(Character(UnicodeScalar(NSDownArrowFunctionKey)!)): return .down case String(Character(UnicodeScalar(NSLeftArrowFunctionKey)!)): return .left case String(Character(UnicodeScalar(NSRightArrowFunctionKey)!)): return .right default: return nil } }};
- Assistant
It looks like you are trying to use some APIs that are not available in Mac Catalyst, which is a framework for running iOS apps on macOS.
To fix this issue, you can use alternative APIs or wrap the code using
#if canImport(AppKit)for macOS only code or#if targetEnvironment(macCatalyst)for Mac Catalyst specific code.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.