phimage / Prephirences

Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults
MIT License
573 stars 35 forks source link

Add PreferencesTabViewController to project #2

Closed dehlen closed 9 years ago

dehlen commented 9 years ago

Hello I tried to follow your Readme but i currently get this error:

2015-07-26 20:49:18.408 T-Rex[50694:618288] An uncaught exception was raised
2015-07-26 20:49:18.408 T-Rex[50694:618288] Cannot remove an observer <Prephirences.PreferencesTabViewController 0x608000101290> for the key path "preferencesTabViewSize" from <T_Rex.GeneralPreferencesView 0x608000100a20> because it is not registered as an observer.

I did the following setup:


import Cocoa
import Prephirences

class GeneralPreferencesView : NSViewController, PreferencesTabViewItemControllerType {
    //if content changes, set this variable accordingly in order to update tab size automatically
    var preferencesTabViewSize: NSSize = NSSize(width: 0, height: 300)

}

Not sure what I am doing wrong ?! The error is due to an unset observer to preferencesTabViewSize but I don't know what I am missing ?

dehlen commented 9 years ago

The error occures because no observer has been added but in override public func tabView the controller is trying to remove one. Did i miss something to add them in the first place or is this a bug in your library that should check if there is an observer before trying to remove one ? Talking about line 55.

phimage commented 9 years ago

Its a bug if the first selected tab view implement the protocole. (For my own use not all tab views implement the protocol) I Will commit a fix when possible

dehlen commented 9 years ago

Please have a look at my latest commit on my fork: https://github.com/T-Rex-Editor/Prephirences/commit/0202cc9159d9f689df69c068fa56a04c06593e8a. I added an Array which holds all observed objects. Then it is possible to check if an object already has an observer or not. This commit fixes this issue but I guess it is not the prettiest solution. Feel free to improve my solution.

phimage commented 9 years ago

Keep track on observers into observed objects is the best way to be able to do a method like hasObserver before removing observer (or a list of observed object into observer)

But in your code you keep track of observers(not observed) into the observer himself, the list could contains only self`` Maybe its you want to addviewControllerintoobservedObjects`

So a simple boolean could achieve the same fix, and it will be the fix so thx ;)