Programmatic Equivalent of “defaults write” command, e.g., how to use NSUserDefaults?

后端 未结 2 1325
长情又很酷
长情又很酷 2021-01-06 09:21

Trying programmatically do what the \'defaults write\' command does in OS X. I can\'t seem to figure out how to get the correct preferences dictionary for the domain I\'m l

相关标签:
2条回答
  • 2021-01-06 09:51

    The only problem is your line here:

     [dockDict setValue:YES forKey:@"mcx-expose-disabled"];
    

    This should be

     [dockDict setValue:[NSNumber numberWithBool:YES] forKey:@"mcx-expose-disabled"];
    

    Objective-C doesn't "auto-box" values of primitive types into objects.

    And, the compiler should have given you a warning saying that you can't pass YES to setValue:forKey:. You should inspect every warning the compiler emits! That's what the warnings are for!

    0 讨论(0)
  • 2021-01-06 09:56

    It might be easier to use Core Foundation for this, e.g.,

    CFPreferencesSetAppValue( CFSTR("mcx-expose-disabled"), kCFBooleanTrue, CFSTR("com.apple.dock") );
    CFPreferencesAppSynchronize( CFSTR("com.apple.dock") );
    
    0 讨论(0)
提交回复
热议问题