Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@Ahruman's answer is correct, if you're dealing with objects. Let me suggest some alternatives:</p> <ol> <li><code>valueForKey:</code>: If you use <code>[myObject valueForKey:@"myPropertyName"]</code>, it will return an object. If the property corresponds to some sort of primitive (<code>int</code>, <code>float</code>, <code>CGRect</code>, etc), then it will be boxed for you into an <code>NSNumber</code> or <code>NSValue</code> (as appropriate). If it comes back as an <code>NSNumber</code>, you can then easily extract a double representation (<code>doubleValue</code>) and use that as an <code>NSTimeInterval</code> to create an <code>NSDate</code>. I would probably recommend this approach.</li> <li><p>Special case each type. <code>property_getAttributes()</code> returns a <code>char*</code> representing all of the attributes of the property, and you can extract the type by doing this:</p> <pre>const char * type = property_getAttributes(class_getProperty([self class], "myPropertyName")); NSString * typeString = [NSString stringWithUTF8String:type]; NSArray * attributes = [typeString componentsSeparatedByString:@","]; NSString * typeAttribute = [attributes objectAtIndex:0]; NSString * propertyType = [typeAttribute substringFromIndex:1]; const char * rawPropertyType = [propertyType UTF8String]; if (strcmp(rawPropertyType, @encode(float)) == 0) { //it's a float } else if (strcmp(rawPropertyType, @encode(int)) == 0) { //it's an int } else if (strcmp(rawPropertyType, @encode(id)) == 0) { //it's some sort of object } else ....</pre> <p>This is pedantically more correct than Louis's answer, because while <em>most</em> types have a single-character encoding, <em>they don't have to</em>. (his suggestion assumes a single-character encoding)</p></li> <li><p>Finally, if you're doing this on a subclass of <code>NSManagedObject</code>, then I would encourage checking out <code>NSPropertyDescription</code>.</p></li> </ol> <p>From these alternatives, you can probably see that letting the runtime box the value for you is probably simplest.</p> <p><strong>edit</strong> extracting the type:</p> <p>From the code above, you can extract the class name like so:</p> <pre><code>if ([typeAttribute hasPrefix:@"T@"] &amp;&amp; [typeAttribute length] &gt; 1) { NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)]; //turns @"NSDate" into NSDate Class typeClass = NSClassFromString(typeClassName); if (typeClass != nil) { [object setValue:[self convertValue:self.value toType:typeClass] forKey:element]; } } </code></pre> <p>And then instead of using class method categories to do the conversion (ie, <code>[NSDate convertToDate:]</code>), make a method on <code>self</code> that does that for you and accepts the desired type as a parameter. You could (for now) do it like:</p> <pre><code>- (id) convertValue:(id)value toType:(Class)type { if (type == [NSDate class]) { return [NSDate convertToDate:value]; } else if (type == [NSString class]) { return [NSString convertToString:value]; } ... } </code></pre> <p>Part of me is wondering, though: why on earth are you needing to do things this way? What are you making?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload