开发者

iOS CFBundleVersion to float

Is there a way to change the CFBundleVersion variable (an NSString) to an float.

My version c开发者_运维知识库urrently is '1.0.2', but when i parse the floatValue from it, it will return 1.00000.

How to parse it so i will get this value: 1.02000

Thanks.


This is not possible. There are two decimal points, and no unambiguous way to represent that as a float. You might roll your own code to convert it to something, but basically the approach as such is flawed.


Try this:-

    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];
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜