iOS CFBundleVersion浮动

时间:2011-06-17 10:11:00

标签: ios nsstring floating-point version

有没有办法将CFBundleVersion变量(NSString)更改为float。

我的版本目前是'1.0.2',但是当我从中解析floatValue时,它将返回1.00000。

如何解析它以便我得到这个值:1.02000

感谢。

2 个答案:

答案 0 :(得分:1)

这是不可能的。有两个小数点,并没有明确的方式来表示它作为一个浮点数。您可以滚动自己的代码将其转换为某些内容,但基本上这样的方法是有缺陷的。

答案 1 :(得分:1)

试试这个: -

    CGFloat versionNumber;

    NSArray *versionItems = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."];

    if (versionItems.count == 3)
    {
        NSInteger major = [[versionItems objectAtIndex:0] integerValue];
        NSInteger minor = [[versionItems objectAtIndex:1] integerValue];
        NSInteger bug = [[versionItems objectAtIndex:2] integerValue];

        versionNumber = [[NSString stringWithFormat:@"%ld.%ld%ld",(long)major,(long)minor,(long)bug] floatValue];
    }
    else
    {
        versionNumber = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] floatValue];
    }