简体   繁体   中英

What is the difference between NSUserDefaultsController and NSArrayController?

I have happily used a "Array Controller" object in the interface builder. The binding worked like charm and everything works like magic.

Because of that I took a look at the "User Default Controller" - thinking that it will work similarly as the "Array Controller". To my surprise there is not binding interface in Xcode. I had to bind the UI element with the user defaults by code. Example bellow:

    [_cbPlaySound bind:@"value"
          toObject:[NSUserDefaultsController sharedUserDefaultsController]
       withKeyPath:@"values.playSound"
           options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
            forKey:@"NSContinuouslyUpdatesValue"]];

Everything again works like it should. But why can't i bind the "User Default Controller" like the "Array Controller"?

To be clear: I did red the Apple documentation, but I still don't understand why to things that seams to me that should work similarly, they don't.

Best.

Can you post a more detailed code snippet? What is _cbPlaySound? If this is a button or something like that, you can go to the Interface Builder and Navigate to the Bindings Inspector (second to last icon) where you have a lot of options (Action Invocation, Availability, ...) you can bind to the SharedUserDefaultsController there.

Back to your question: I think the difference between those two is that NSUserDefaultsController is a Singleton while NSArrayController is not. That means you only have one instance of the UserDefaultsController in the whole app and you only get a pointer to that instance. If you use a NSArrayController you create your own instance which must be then made public in the Interface Builder.

So you don't really 'own' the UserDefaultsController and you don't need to add it specifically as an object to the Interface Builder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM