久久久久久久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的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

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

I just want to convert a character into an Int.

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

因?yàn)椴皇撬械淖址伎梢赞D(zhuǎn)換成int,所以這個(gè)初始化器是failable的,也就是說(shuō)它返回一個(gè)可選的int(Int?)而不僅僅是一個(gè) Int.最好的辦法是使用 if let 解開這個(gè)可選項(xiàng).

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)
}

或者,對(duì)于更快捷的解決方案:

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 返回一個(gè) Array,其中包含將 transform 映射到 self 的非零結(jié)果"……所以當(dāng) Int(String($0))nil 時(shí),結(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的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Which is the best way to estimate measure of photographed things?(哪種方法是估計(jì)拍攝物體尺寸的最佳方法?)
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é)果變得太高時(shí)如何計(jì)算階乘?)
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视频在线播放 | av中文字幕在线 | 国产精品一区二区三区在线播放 | 成人午夜视频在线观看 | 精品国产一区二区三区性色av | 日本涩涩视频 | 天堂亚洲| 色网站在线 | 在线观看国产 | 日韩精品一区二区三区中文在线 | 嫩草研究影院 | 中文字幕亚洲一区二区三区 | 中文字幕亚洲视频 | 国产91视频播放 | 国产色在线 | 人人草人人干 | 亚洲精品一区二区 | 99这里只有精品视频 | 亚洲国产精品视频 | 亚洲精品成人 | 你懂的在线视频播放 | 黄色网址免费看 | 观看av| 一区二区三区国产在线观看 | 亚洲日本一区二区 | 男人av网 | 成人午夜在线 | 精品一区二区久久久久久久网站 | 久久天天躁狠狠躁夜夜躁2014 | 亚洲精品中文在线 | 国产污视频在线 | 亚洲人在线观看视频 |