ios 6和7不会返回相同的结果 [英] ios 6 and 7 doesnt return same results
问题描述
似乎我们使用 getPropertyType(..)
的应用程序在ios7下失败。无论出于何种原因, getPropertyType(..)
on例如NSString属性返回 NSString $'\ x19 \ x03 \ x86 \ x13
作为类型,而不仅仅是NSString,而不是NSNumber,它返回 NSNumber \ xf0 \ x90 \ xae \ x04 \ xff \ xff \ xff\xff
。当我稍后检查特定类型时,所有这些都会导致一些棘手的问题。我已经改变了这个(遗产?)代码来使用 isKindOfClass
,但是我不理解我在这里发生了什么。
It seems that our apps which use getPropertyType(..)
are failing under ios7. For whatever reason, getPropertyType(..)
on for example a NSString property returns NSString$'\x19\x03\x86\x13
as the type, instead of just NSString, and also instead of NSNumber it returns NSNumber\xf0\x90\xae\x04\xff\xff\xff\xff
. All of this is causing some tricky problems when i later on check against a specific type. I have changed this (legacy?) code to use isKindOfClass
instead, but it bothers me that I don't understand whats going on here.
有问题的代码:
#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 "@";
}
到底发生了什么,为什么结果不同?
What on earth is going on, why are the results different??
推荐答案
getPropertyType返回的缓冲区未终止NULL。我认为这是唯一有用的运气。此外,返回新创建的NSData指向的数据并不能保证一旦该函数返回就会起作用。
The buffer returned by getPropertyType isn't NULL terminated. I think it's only dumb luck that it ever worked. Also, returning the data pointed to by a newly created NSData is not guaranteed to work once that function returns.
我将返回一个NSString。
I'd make this return an 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 @"@";
}
这假定为ARC。
这篇关于ios 6和7不会返回相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!