0

如何创建任何原始值的字符串?好的,结构等可能是个大问题。但是对于浮点数,整数

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

注意:这仅用于调试、单元测试和娱乐

4

1 回答 1

1

这涵盖了最基本的......我不知道它是否足够......也许我忽略了一些明显的东西?

#define _PRIMITIVE_AS_STRING(value) \
({\
    const char *valueType = @encode(__typeof__(value));\
    NSString *format = [NSString stringWithFormat:@"%s", printFormatTypeForObjCType(valueType)];\
    NSString *valueAsString = [NSString stringWithFormat:format, value];\
    valueAsString;\
})

static const char * printFormatTypeForObjCType(const char *type)
{
    if(strcmp(type, @encode(BOOL)) == 0)
        return "%d";
    else if(strcmp(type, @encode(int)) == 0)
        return "%d";
    else if(strcmp(type, @encode(unsigned int)) == 0)
        return "%u";
    else if(strcmp(type, @encode(long)) == 0)
        return "%li";
    else if(strcmp(type, @encode(unsigned long)) == 0)
        return "%lu";
    else if(strcmp(type, @encode(long long)) == 0)
        return "%lli";
    else if(strcmp(type, @encode(unsigned long long)) == 0)
        return "%llu";
    else if(strcmp(type, @encode(float)) == 0)
        return "%f";
    else if(strcmp(type, @encode(double)) == 0)
        return "%f";
    else
        return "%d";
}
于 2013-03-19T16:07:33.437 回答