gpt4 book ai didi

iphone - 在 Objective-C 中确定属性是否为 int、float、double、NSString、NSDate、NSNumber 等

转载 作者:太空狗 更新时间:2023-10-30 03:18:32 26 4
gpt4 key购买 nike

我需要确定一个对象的属性(按名称传递)类型,以便从 XML 执行反序列化。我有一些通用的伪代码(但是我不确定如何在 Objective-C 中执行这些比较):

id object = [[[Record alloc] init] autorelease];

NSString *element = @"date";
NSString *data = @"2010-10-16";

objc_property_t property = class_getProperty([object class], [element UTF8String]);
const char *attributes = property_getAttributes(property);

char buffer[strlen(attributes) + 1];
strcpy(buffer, attributes);

char *attribute = strtok(buffer, ",");
if (*attribute == 'T') attribute++; else attribute = NULL;

if (attribute == NULL);
else if (strcmp(attribute, "@\"NSDate\"") == 0) [object setValue:[NSDate convertToDate:self.value] forKey:element];
else if (strcmp(attribute, "@\"NSString\"") == 0) [object setValue:[NSString convertToString:self.value] forKey:element];
else if (strcmp(attribute, "@\"NSNumber\"") == 0) [object setValue:[NSNumber convertToNumber:self.value] forKey:element];

我已经查看了 class_getPropertyproperty_getAttributes , 但是我仍然不确定如何进行上述比较。

最佳答案

@Ahruman 的回答是正确的,如果你正在处理对象。让我提出一些替代方案:

  1. valueForKey::如果您使用[myObject valueForKey:@"myPropertyName"],它将返回一个对象。如果该属性对应于某种原始类型(intfloatCGRect 等),那么它将被装箱到一个 NSNumberNSValue(视情况而定)。如果它作为 NSNumber 返回,您可以轻松提取 double 表示 (doubleValue) 并将其用作 NSTimeInterval 以创建 NSDate。我可能会推荐这种方法。
  2. 每种类型的特例。 property_getAttributes() 返回表示属性所有属性的 char*,您可以通过执行以下操作提取类型:

    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 ....

    This is pedantically more correct than Louis's answer, because while most types have a single-character encoding, they don't have to. (his suggestion assumes a single-character encoding)

  3. Finally, if you're doing this on a subclass of NSManagedObject, then I would encourage checking out NSPropertyDescription.

From these alternatives, you can probably see that letting the runtime box the value for you is probably simplest.

edit extracting the type:

From the code above, you can extract the class name like so:

if ([typeAttribute hasPrefix:@"T@"] && [typeAttribute length] > 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];
}
}

然后不使用类方法类别来进行转换(即 [NSDate convertToDate:]),而是在 self 上创建一个方法来为您完成转换,并且接受所需的类型作为参数。你可以(现在)这样做:

- (id) convertValue:(id)value toType:(Class)type {
if (type == [NSDate class]) {
return [NSDate convertToDate:value];
} else if (type == [NSString class]) {
return [NSString convertToString:value];
} ...
}

不过,我内心的一部分在想:你究竟为什么需要以这种方式做事?你在做什么?

关于iphone - 在 Objective-C 中确定属性是否为 int、float、double、NSString、NSDate、NSNumber 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3497625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com