properties - How to test property existence and type based on NSString typed key? -


in quest update core data model within ios project, i'm querying server json objects correspond - extent - managed entities of model. end result i'm striving reliable update solution json output.

for examples in question, i'll name core data managed object existingobj , incoming json deserialized dictionary updatedict. tricky part dealing these facts:

  1. not properties of existingobj present in updatedict
  2. not properties of updatedict available in extistingobj.
  3. not types of existingobj's properties match json deserialized properties. (some strings may need custom objective-c wrapper).
  4. updatedict may contain values keys uninitialized (nil) in existingobj.

this means while iterating through updated dictionaries, there has testing of properties , forth. first have test whether properties of updatedict exist in existingobj, set value using kvc, so:

// key nsstring, e.g. @"displayname" if ([existingobj respondstoselector:nsselectorfromstring(key)) {     [existingobj setvalue:[updatedict objectforkey:key] forkey:key]; } 

although part works, don't fact i'm testing displayname getter, while i'm call setdisplayname: setter (indirectly via kvc). i'd rather [existingobj haswritablepropertywithname:key], can't find.

this makes subquestion a: how 1 test property setter, if have property's name?

the next part i'd automate property identification based on types. if both updatedict , existingobj have nsstring key @"displayname", setting new value easy. however, if updatedict contains nsstring key @"color" @"niceshadeofgreen", i'd transform right uicolor instance. how test type of receiving property in existingobj know when convert values , when assign? hoping along lines of typeofselector:

if ([existingobj typeofselector:sel] == [[updatedict objectforkey:key] class]) {      // regular assignment } else {      // perform custom assignment } 

of course boguscode. can't rely on testing type of existingobj-property's value, may unitialized or nil.

subquestion b: how 1 test type of property, if have property's name?

i guess that's it. figured must dupe of that's on here, couldn't find it. maybe guys can?
cheers, ep.

p.s. if you'd have better way synchronize custom objective-c objects deserialized json objects, please share! in end, result counts.

if want query whether object has setter given kvc key called key corresponds declared property, need check whether responds selector method called setkey: (starts set, capitalise first character in key, add trailing colon). instance,

nsstring *key = @"displayname"; nsstring *setterstr = [nsstring stringwithformat:@"set%@%@:",                        [[key substringtoindex:1] capitalizedstring],                       [key substringfromindex:1]];  if ([obj respondstoselector:nsselectorfromstring(setterstr)]) {     nslog(@"found setter!");     [obj setvalue:somevalue forkey:key]; } 

two remarks:

  • even though properties can have setters names not follow pattern described above, wouldn’t kvc compliant, safe check set<key>: since you’re using kvc set corresponding value.

  • kvc doesn’t use setter method only. if doesn’t find setter method, checks whether class allows direct access instance variables and, if so, use instance variable set value. also, if no setter method or instance variable found, sends -setvalue:forundefinedkey: receiver, class might have overridden standard implementation throws exception. described in key-value coding programming guide.that said, if you’re using properties, checking setter method should safe.

as second question, not possible query runtime know actual objective-c class of property. runtime perspective, there’s implementation specific type encoding properties , general types (such method parameters/return types). type encoding uses single encoding (namely @) objective-c object, type encoding of nsstring property same type encoding of uicolor property since they’re both objective-c classes.

if need functionality, 1 alternative process classes , add class method returns dictionary keys , corresponding types every property (or ones you’re interested in) declared in class , superclasses, or maybe sort of description language. you’d have on own , rely on information not available during runtime.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -