Can you pair macOS and iOS device with same Magic Keyboard and switch between them?
No - Apple's firmware on the keyboard remembers the last paired device.

Some bluetooth keyboards can switch back and forth, but that feature isn't available on Apple's current hardware.