久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Swift 2.0 中將字符轉換為 Int

Convert Character to Int in Swift 2.0(在 Swift 2.0 中將字符轉換為 Int)
本文介紹了在 Swift 2.0 中將字符轉換為 Int的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我只想將 character 轉換為 Int.

I just want to convert a character into an Int.

這應該很簡單.但我還沒有發現以前的答案有幫助.總是有一些錯誤.也許是因為我正在 Swift 2.0 中嘗試它.

This should be simple. But I haven't found the previous answers helpful. There is always some error. Perhaps it is because I'm trying it in Swift 2.0.

for i in (unsolved.characters) {
  fileLines += String(i).toInt()
  print(i)
}

推薦答案

在 Swift 2.0 中,toInt() 等已被替換為初始化器.(在這種情況下,Int(someString).)

In Swift 2.0, toInt(), etc., have been replaced with initializers. (In this case, Int(someString).)

因為不是所有的字符串都可以轉換成int,所以這個初始化器是failable的,也就是說它返回一個可選的int(Int?)而不僅僅是一個 Int.最好的辦法是使用 if let 解開這個可選項.

Because not all strings can be converted to ints, this initializer is failable, which means it returns an optional int (Int?) instead of just an Int. The best thing to do is unwrap this optional using if let.

我不確定你到底想要什么,但這段代碼在 Swift 2 中工作,并完成了我認為你正在嘗試做的事情:

I'm not sure exactly what you're going for, but this code works in Swift 2, and accomplishes what I think you're trying to do:

let unsolved = "123abc"

var fileLines = [Int]()

for i in unsolved.characters {
    let someString = String(i)
    if let someInt = Int(someString) {
        fileLines += [someInt]
    }
    print(i)
}

或者,對于更快捷的解決方案:

Or, for a Swiftier solution:

let unsolved = "123abc"

let fileLines = unsolved.characters.filter({ Int(String($0)) != nil }).map({ Int(String($0))! })

// fileLines = [1, 2, 3]

您可以使用 flatMap 進一步縮短它:

You can shorten this more with flatMap:

let fileLines = unsolved.characters.flatMap { Int(String($0)) }

flatMap 返回一個 Array,其中包含將 transform 映射到 self 的非零結果"……所以當 Int(String($0))nil 時,結果被丟棄.

flatMap returns "an Array containing the non-nil results of mapping transform over self"… so when Int(String($0)) is nil, the result is discarded.

這篇關于在 Swift 2.0 中將字符轉換為 Int的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Which is the best way to estimate measure of photographed things?(哪種方法是估計拍攝物體尺寸的最佳方法?)
Edit Text shows exception Invalid Int quot;quot;(編輯文本顯示異常 Invalid Int “;)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉換為字符)
In Swift 3, how to calculate the factorial when the result becomes too high?(在 Swift 3 中,當結果變得太高時如何計算階乘?)
How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創建十六進制顏色字符串 UIColor 初始化程序?)
How to round a Double to the nearest Int in swift?(如何快速將 Double 舍入到最近的 Int?)
主站蜘蛛池模板: 91视频色| 亚洲欧美另类在线 | 黄色影院在线观看 | 免费网站观看www在线观看 | 日皮视频在线观看 | 91一区二区 | 91黄色大片 | 午夜国产福利 | 一级片在线视频 | 日韩中文字幕一区 | 91av精品| 夜夜操天天干 | 欧美精品一区在线 | 午夜视频在线看 | 午夜视频在线播放 | 久久久一本 | 国产一级片免费 | 久久在线免费视频 | 天天操天天干天天操 | 欧美日韩国产在线播放 | 国产无精乱码一区二区三区 | 成人午夜又粗又硬又大 | 亚洲国产欧美日韩在线 | 国产精品欧美精品 | 91久久精品日日躁夜夜躁欧美 | 人人干人人爱 | 欧美综合一区 | 欧美在线播放 | 成年视频在线观看 | 色综合天天综合网天天狠天天 | 成人午夜网 | 国产福利91 | 国产黄a三级三级三级看三级男男 | 国产欧美日韩在线视频 | 亚洲一区二区精品视频 | 黄色一级大片在线免费看产 | 狠狠干综合网 | 欧美在线中文字幕 | 秋霞午夜鲁丝一区二区老狼 | 一道本在线 | 久久久婷婷|