Search code examples
swiftmacosterminalnsuserdefaults

macOS - UserDefaults is different in Swift and Terminal


I'm trying to access a system preference (com.apple.menuextra.clock DateFormat specifically) from my Swift app using UserDefaults. Using terminal, defaults read com.apple.menuextra.clock returns

{
    DateFormat = "EEE MMM d  h:mm:ss a";
    FlashDateSeparators = 0;
    IsAnalog = 0;
}

However, if I do this in my Swift app

print(UserDefaults.standard.dictionary(forKey: "com.apple.menuextra.clock"))

I get nil.

How do I access this in Swift? If it helps, I'm only looking for DateFormat so I know if the user prefers 12- or 24-hour time. I've tried

DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)!

then finding whether or not it contains a, but that doesn't work either.

Thanks in advance.


Solution

  • To anyone else who has this question in the future: I fixed it by turning off App Sandbox.