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

在 Swift 2.0 中將字符轉(zhuǎn)換為 Int

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

問題描述

我只想將 character 轉(zhuǎn)換為 Int.

I just want to convert a character into an Int.

這應(yīng)該很簡單.但我還沒有發(fā)現(xiàn)以前的答案有幫助.總是有一些錯誤.也許是因為我正在 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).)

因為不是所有的字符串都可以轉(zhuǎn)換成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 中工作,并完成了我認(rèn)為你正在嘗試做的事情:

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 進(jìn)一步縮短它:

You can shorten this more with flatMap:

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

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

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.

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

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

相關(guān)文檔推薦

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 轉(zhuǎn)換為字符)
In Swift 3, how to calculate the factorial when the result becomes too high?(在 Swift 3 中,當(dāng)結(jié)果變得太高時如何計算階乘?)
How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創(chuàng)建十六進(jìn)制顏色字符串 UIColor 初始化程序?)
How to round a Double to the nearest Int in swift?(如何快速將 Double 舍入到最近的 Int?)
主站蜘蛛池模板: 天天草天天操 | h免费观看| 手机看片在线播放 | 久久国产日韩 | 国产一区二区自拍 | 国产伦精品一区二区 | 色在线免费视频 | 久久岛国 | 伊人久久综合 | 久久久久亚洲精品 | 欧美日韩久久精品 | 色眯眯视频在线观看 | 天堂资源最新在线 | 日日摸夜夜添夜夜添特色大片 | 中文字幕在线一区二区三区 | 国产欧美日韩在线 | 亚洲一区二区三区四区五区午夜 | 欧美日韩国产一区二区 | 日本福利视频 | 国产成人99久久亚洲综合精品 | 精品二三区 | 久久久精品一区 | 日韩在线播放网址 | 国产精品一区二区不卡 | 欧美日本一区 | 国产激情精品一区二区三区 | 精品网| 蜜桃臀av一区二区三区 | 一区二区三区免费 | 欧美一区二区三区视频 | 亚洲国产区 | 亚洲天堂成人在线视频 | 黄色片a级| 亚洲色图综合 | 国产一区二区久久 | 天天草视频 | 国产欧美精品一区二区三区 | 国产女人与拘做视频免费 | www.国产一区 | 久久综合伊人一区二区三 | 在线免费国产视频 |