IOS 擷取系統版本字串,並且轉化成float類型

來源:互聯網
上載者:User

IOS 中擷取系統版本,比較簡單([[UIDevicecurrentDevice] systemVersion]);

看到網路上很多轉化系統字串到float 的方法,都是使用 [[[UIDevice currentDevice] systemVersion] floatValue];

但是這個方法我遇到一個問題,就是如果系統版本是7.1.1 的時候,轉化產生的float 有問題,我debug 產生的資料為7.0899999999999。導致出現了問題,

後來為瞭解決這個問題,我自己寫了一個方法。


+ (CGFloat )systemVersion{

NSString *sVersion = [[UIDevice currentDevice] systemVersion];

NSRange fRange = [sVersion rangeOfString:@"."];


CGFloat version = 0.0f;

if(fRange.location != NSNotFound){
sVersion = [sVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
NSMutableString *mVersion = [NSMutableString stringWithString:sVersion];
[mVersion insertString:@"." atIndex:fRange.location];
version = [mVersion floatValue];
}else {
// 版本應該有問題(由於ios 的版本 是7.0.1,沒有發現出現過沒有小數點的情況)
version = [sVersion floatValue];
}

return version;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.