Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

I have these errors: 'NSApplication' is unavailable in Mac Catalyst…

  1. 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
        }
    }
    

    };

  2. 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.

    Here's an example of how you can modify your code by wrapping the incompatible parts with #if canImport(AppKit):

    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? {
          #if canImport(AppKit)
            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
            }
          #endif
          return nil
        }
    }
    

    This will disable the arrow key movement for Mac Catalyst builds, but will work as expected on macOS-only builds. You can also use #if targetEnvironment(macCatalyst) if you want to handle specific logic for Mac Catalyst.