setValue:forUndefinedKey:

  • Hello.

    My object has some properties in a dictionary. It is like
    NSManagedObject that has (I guess) the properties in a Dictionary.
    Some of the properties have specific set/get methods, others do not.
    They should all be accessible with key value coding. I have read
    about setValue:forKey:'s search order at this web page:

    http://developer.apple.com/documentation/Cocoa/Conceptual/
    KeyValueCoding/Concepts/SearchImplementation.html

    My current solution is to implement a setValue:forUndefinedKey: that
    look up in the Dictionary for properties that do not have a separate
    set method. It is working. Is this a good idea or should I override
    setValue:forKey: instead?

      - Tore.
  • on 10/11/06 5:03 PM, <halset...> purportedly said:

    > My object has some properties in a dictionary. It is like
    > NSManagedObject that has (I guess) the properties in a Dictionary.
    > Some of the properties have specific set/get methods, others do not.
    > They should all be accessible with key value coding. I have read
    > about setValue:forKey:'s search order at this web page:
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/
    > KeyValueCoding/Concepts/SearchImplementation.html
    >
    > My current solution is to implement a setValue:forUndefinedKey: that
    > look up in the Dictionary for properties that do not have a separate
    > set method. It is working. Is this a good idea or should I override
    > setValue:forKey: instead?

    Using setValue:forUndefinedKey: is the only way I have found to do this. If
    you override setvalue:forkey: you will have to handle KVO notifications in
    your override, or your bindings won't update properly. So it's an easier
    solution, however inelegant.

    Core Data/NSManagedObject is probably a more elegant solution but I haven't
    pursued it yet.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • On Oct 12, 2006, at 3:10, Keary Suska wrote:

    > on 10/11/06 5:03 PM, <halset...> purportedly said:
    >
    >> My current solution is to implement a setValue:forUndefinedKey: that
    >> look up in the Dictionary for properties that do not have a separate
    >> set method. It is working. Is this a good idea or should I override
    >> setValue:forKey: instead?
    >
    > Using setValue:forUndefinedKey: is the only way I have found to do
    > this. If
    > you override setvalue:forkey: you will have to handle KVO
    > notifications in
    > your override, or your bindings won't update properly. So it's an
    > easier
    > solution, however inelegant.

    Okay, thanks. My problem with overriding setValue:forKey: is that any
    custom set/get methods are not called.  I guess my custom
    setValue:forKey: should follow key-value search ordering and look for
    custom set/get methods for the given key. Would like to see some
    examples on how to do this? :)

    > Core Data/NSManagedObject is probably a more elegant solution but I
    > haven't
    > pursued it yet.

    I am not able to use NSManagedObject on this, but it looks like
    NSManagedObject has solved this issue in some sort. But how?

      - Tore.
  • on 10/12/06 1:39 AM, <halset...> purportedly said:

    > Okay, thanks. My problem with overriding setValue:forKey: is that any
    > custom set/get methods are not called.  I guess my custom
    > setValue:forKey: should follow key-value search ordering and look for
    > custom set/get methods for the given key. Would like to see some
    > examples on how to do this? :)

    Yeah, that too. The KVC docs explain the search sequence fairly clearly, and
    if you do the same you should be safe. You would use NSSelectorFromString()
    and respondsToSelector: for checking (or not, if robustness isn't an issue),
    and you will probably need to use NSInvocation to call the methods. There
    are likely some parts of the search you can safely omit. Don't forget the
    KVO part. All in all, this is why the *forUndefinedKey: methods are easier
    to use ;-)

    >> Core Data/NSManagedObject is probably a more elegant solution but I
    >> haven't
    >> pursued it yet.
    >
    > I am not able to use NSManagedObject on this, but it looks like
    > NSManagedObject has solved this issue in some sort. But how?

    IMHO, Apple is not very good at explaining what's happening behind the
    scenes.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today