objective c - UIKit additions: bridging the gap between eg CGRectValue and NSStringFromCGRect? -


i'm trying implement specialized typed logger takes dictionary input , creates string of values out of it.

one sample such dictionary might be:

cgsize size = {300, 200} ; cgaffinetransform t = ::cgaffinetransformidentity ;  [self logit: @{     @"columns": @{         @"value": @3     ,   @"type": @"nsnumber"     } ,   @"size": @{         @"value": [nsvalue valuewithcgsize(size)]     ,   @"type": @"cgsize"     } ,   @"transform": @{         @"value": [nsvalue valuewithcgaffinetransform(t)]     ,   @"type":    @"cgaffinetransform"     } }] ; 

the issue is: how retrieve values in generic way?

i write

    (nsstring * key in dict) {         nsdictionary * params = dict[key] ;          if ([params[@"type"] isequaltostring: @"cgrect"]) {             cgrect r = [params[@"value"] cgrectvalue] ;             nsstring * s = ::nsstringfromcgrect(r) ;             ::nslog(s) ;         } else if ([params[@"type"] isequaltostring: @"nsnumber"]) {             ::nslog(@"%.3f", [params[@"value"] floatvalue]) ;         } else if ([params[@"type"] isequaltostring: @"cgaffinetransform"]) {             cgaffinetransform t = [params[@"value"] cgaffinetransformvalue]             nsstring * s = ::nsstringfromcgaffinetransform(t) ;             ::nslog(s) ;         }         ...     } 

but i'd rather write more "generic" solution. along lines:

sel valueselector = ::nsselectorfromstring([nsstring stringwithformat:@"%@value", params[@"type"]) ; nsstring * rawcfunctionname = [nsstring stringwithformat:@"nsstringfrom%@", params[@"type"]] ; 

so have selector , name of c function.

  1. how go string c function can call?

  2. using selector above unclear return type can vary id e.g.: in case of nsnumber * "plain c object" e.g. in case of cgrect, cgsize etc ...

any suggestion?

  1. you can't; see "call function named in string variable in c".

  2. yup, have know return type @ compile time.

all not lost, however: you're trying can accomplished in mundane way. nsvalue has nice feature description method gives output similar appropriate nsstringfrom...() function type it's storing (it's using them under hood). example:

nslog(@"%@", [nsvalue valuewithcgaffinetransform:cgaffinetransformmake(10, 10, 10, 10, 10, 10)]); nslog(@"%@", [nsvalue valuewithcgsize:cgsizemake(10, 10)]); 

yields:

2013-08-11 03:06:43.053 printingnsvalue[51538:11303] cgaffinetransform: {{10, 10, 10, 10}, {10, 10}}
2013-08-11 03:06:43.055 printingnsvalue[51538:11303] nssize: {10, 10}

nsnumber likewise give nice representation of value description.

so have this:

for( nsstring * key in thedictionary ){     nslog(@"%@: %@", key, thedictionary[key][@"value"]); } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -