tsk*_*bru 5 objective-c objective-c-runtime ios
似乎我们使用的应用程序getPropertyType(..)在ios7下失败了.无论出于何种原因,getPropertyType(..)例如NSString属性NSString$'\x19\x03\x86\x13作为类型返回,而不仅仅是NSString,而不是返回NSNumber NSNumber\xf0\x90\xae\x04\xff\xff\xff\xff.当我稍后检查特定类型时,所有这些都会导致一些棘手的问题.我已经改变了这个(传统的?)代码isKindOfClass,但是让我感到困扰的是我不明白这里发生了什么.
有问题的代码:
#import <objc/runtime.h>
static const char *getPropertyType(objc_property_t property) {
const char *attributes = property_getAttributes(property);
char buffer[1 + strlen(attributes)];
strcpy(buffer, attributes);
char *state = buffer, *attribute;
while ((attribute = strsep(&state, ",")) != NULL) {
if (attribute[0] == 'T') {
return (const char *)[[NSData dataWithBytes:(attribute + 3) length:strlen(attribute) - 4] bytes];
}
}
return "@";
}
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事,为什么结果不同?
getPropertyType 返回的缓冲区不是以 NULL 结尾的。我认为这只是运气好而已。此外,一旦该函数返回,返回新创建的 NSData 所指向的数据并不能保证起作用。
我会让这个返回一个 NSString。
NSString* getPropertyType(objc_property_t property) {
const char *attributes = property_getAttributes(property);
char buffer[1 + strlen(attributes)];
strcpy(buffer, attributes);
char *state = buffer, *attribute;
while ((attribute = strsep(&state, ",")) != NULL) {
if (attribute[0] == 'T') {
return [[NSString alloc] initWithBytes:attribute + 3 length:strlen(attribute) - 4 encoding:NSASCIIStringEncoding];
}
}
return @"@";
}
Run Code Online (Sandbox Code Playgroud)
这假设是 ARC。
| 归档时间: |
|
| 查看次数: |
502 次 |
| 最近记录: |