在 Swift 2.0 中将字符转换为 Int

2023-02-16移动开发问题
24

本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

如何检查设备是否有网络连接:cocos-2d
How to check whether there is internet connection with the device: cocos-2d(如何检查设备是否有网络连接:cocos-2d)...
2024-08-12 移动开发问题
1

Cocos2d中Sprites在两点之间画一条线Sprite
Draw a Line Sprite Between Two Points made by Sprites in Cocos2d(Cocos2d中Sprites在两点之间画一条线Sprite)...
2024-08-12 移动开发问题
9

“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’"MKS
quot;No visible @interface for #39;NSData#39; declares the selector #39;base64EncodedStringquot; error in MKStoreKit(“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’MKStoreKit 中的错误)...
2024-08-12 移动开发问题
3

在 Cocos2d & 中获取身体的接触点Box2d
Getting contact points on bodies in Cocos2d amp; Box2d(在 Cocos2d amp; 中获取身体的接触点Box2d)...
2024-08-12 移动开发问题
4

iOS 和 Cocos2d - 更改 CCSprite 的图像和新尺寸 = FAIL
iOS and Cocos2d - Changing CCSprite#39;s image AND new dimensions = FAIL(iOS 和 Cocos2d - 更改 CCSprite 的图像和新尺寸 = FAIL)...
2024-08-12 移动开发问题
8