In Swift, i cant cast Int to String by:
在Swift中,我无法通过以下方式将Int转换为String:
var iString:Int = 100
var strString = String(iString)
But my variable in Int? , there for error: Cant invoke 'init' with type '@Ivalue Int?'
但我在Int中的变量? ,出于错误:无法调用类型为'@Ivalue Int?'的'init'
Example
例
let myString : String = "42"
let x : Int? = myString.toInt()
if (x != null) {
// Successfully converted String to Int
//And how do can i convert x to string???
}
9 个解决方案
#1
30
You can use string interpolation.
您可以使用字符串插值。
let x = 100
let str = "\(x)"
if x
is an optional you can use optional binding
如果x是可选的,则可以使用可选绑定
var str = ""
if let v = x {
str = "\(v)"
}
println(str)
if you are sure that x
will never be nil
, you can do a forced unwrapping
on an optional value
.
如果您确定x永远不会为nil,则可以对可选值进行强制解包。
var str = "\(x!)"
In a single statement you can try this
在一个声明中,您可以尝试这一点
let str = x != nil ? "\(x!)" : ""
Based on @RealMae's comment, you can further shorten this code using the nil coalescing operator (??)
根据@ RealMae的评论,您可以使用nil合并运算符进一步缩短此代码(??)
let str = x ?? ""
#2
16
I like to create small extensions for this:
我喜欢为此创建小扩展:
extension Int {
var stringValue:String {
return "\(self)"
}
}
This makes it possible to call optional ints, without having to unwrap and think about nil values:
这使得调用可选的int成为可能,而不必解开和考虑nil值:
var string = optionalInt?.stringValue
#3
3
Sonrobby, I believe that "Int?" means an optional int. Basically, by my understanding, needs to be unwrapped.
Sonrobby,我相信“Int?”表示可选的int。基本上,根据我的理解,需要打开包装。
So doing the following works fine:
所以做以下工作很好:
let y: Int? = 42
let c = String(y!)
That "!" unwraps the variable. Hope this helps!
那个“!”展开变量。希望这可以帮助!
As rakeshbs mentioned, make sure the variable won't be nill.
正如rakeshbs所提到的那样,确保变量不会是nill。
#4
3
If you need a one-liner it can be achieved by:
如果您需要单线,可以通过以下方式实现:
let x: Int? = 10
x.flatMap { String($0) } // produces "10"
let y: Int? = nil
y.flatMap { String($0) } // produces nil
if you need a default value, you can simply go with
如果你需要一个默认值,你可以简单地使用
(y.flatMap { String($0) }) ?? ""
EDIT:
编辑:
Even better without curly brackets:
没有花括号更好:
y.flatMap(String.init)
#5
1
You need to "unwrap" your optional in order to get to the real value inside of it as described here. You unwrap an option with "!". So, in your example, the code would be:
您需要“解包”您的可选项,以便获得其中的实际值,如此处所述。用“!”打开选项。因此,在您的示例中,代码将是:
let myString : String = "42"
let x : Int? = myString.toInt()
if (x != null) {
// Successfully converted String to Int
// Convert x (an optional) to string by unwrapping
let myNewString = String(x!)
}
Or within that conditional, you could use string interpolation:
或者在该条件内,您可以使用字符串插值:
let myNewString = "\(x!)" // does the same thing as String(x!)
#6
1
For preventing unsafe optional unwraps I use it like below as suggested by @AntiStrike12,
为了防止不安全的可选unwraps,我按照@ AntiStrike12的建议使用它,
if let theString = someVariableThatIsAnInt {
theStringValue = String(theString!))
}
#7
1
Optional Int -> Optional String:
可选Int - >可选字符串:
If x: Int?
(or Double?
- doesn't matter)
如果x:Int? (或双倍? - 无关紧要)
var s = x.map({String($0)})
This will return String?
这将返回String?
To get a String
you can use :
要获得String,您可以使用:
var t = s ?? ""
#8
0
Swift 3:
var iString:Int = 100
var strString = String(iString)
extension String {
init(_ value:Int){/*Brings back String() casting which was removed in swift 3*/
self.init(describing:value)
}
}
This avoids littering your code with the verbose: String(describing:iString)
这可以避免使用verbose:String(描述:iString)乱丢你的代码
Bonus: Add similar init methods for commonly used types such as: Bool
, CGFloat
etc.
额外:为常用类型添加类似的init方法,例如:Bool,CGFloat等。
#9
-1
Crude perhaps, but you could just do:
也许原油,但你可以这样做:
let int100 = 100
println(int100.description) //Prints 100
#1
30
You can use string interpolation.
您可以使用字符串插值。
let x = 100
let str = "\(x)"
if x
is an optional you can use optional binding
如果x是可选的,则可以使用可选绑定
var str = ""
if let v = x {
str = "\(v)"
}
println(str)
if you are sure that x
will never be nil
, you can do a forced unwrapping
on an optional value
.
如果您确定x永远不会为nil,则可以对可选值进行强制解包。
var str = "\(x!)"
In a single statement you can try this
在一个声明中,您可以尝试这一点
let str = x != nil ? "\(x!)" : ""
Based on @RealMae's comment, you can further shorten this code using the nil coalescing operator (??)
根据@ RealMae的评论,您可以使用nil合并运算符进一步缩短此代码(??)
let str = x ?? ""
#2
16
I like to create small extensions for this:
我喜欢为此创建小扩展:
extension Int {
var stringValue:String {
return "\(self)"
}
}
This makes it possible to call optional ints, without having to unwrap and think about nil values:
这使得调用可选的int成为可能,而不必解开和考虑nil值:
var string = optionalInt?.stringValue
#3
3
Sonrobby, I believe that "Int?" means an optional int. Basically, by my understanding, needs to be unwrapped.
Sonrobby,我相信“Int?”表示可选的int。基本上,根据我的理解,需要打开包装。
So doing the following works fine:
所以做以下工作很好:
let y: Int? = 42
let c = String(y!)
That "!" unwraps the variable. Hope this helps!
那个“!”展开变量。希望这可以帮助!
As rakeshbs mentioned, make sure the variable won't be nill.
正如rakeshbs所提到的那样,确保变量不会是nill。
#4
3
If you need a one-liner it can be achieved by:
如果您需要单线,可以通过以下方式实现:
let x: Int? = 10
x.flatMap { String($0) } // produces "10"
let y: Int? = nil
y.flatMap { String($0) } // produces nil
if you need a default value, you can simply go with
如果你需要一个默认值,你可以简单地使用
(y.flatMap { String($0) }) ?? ""
EDIT:
编辑:
Even better without curly brackets:
没有花括号更好:
y.flatMap(String.init)
#5
1
You need to "unwrap" your optional in order to get to the real value inside of it as described here. You unwrap an option with "!". So, in your example, the code would be:
您需要“解包”您的可选项,以便获得其中的实际值,如此处所述。用“!”打开选项。因此,在您的示例中,代码将是:
let myString : String = "42"
let x : Int? = myString.toInt()
if (x != null) {
// Successfully converted String to Int
// Convert x (an optional) to string by unwrapping
let myNewString = String(x!)
}
Or within that conditional, you could use string interpolation:
或者在该条件内,您可以使用字符串插值:
let myNewString = "\(x!)" // does the same thing as String(x!)
#6
1
For preventing unsafe optional unwraps I use it like below as suggested by @AntiStrike12,
为了防止不安全的可选unwraps,我按照@ AntiStrike12的建议使用它,
if let theString = someVariableThatIsAnInt {
theStringValue = String(theString!))
}
#7
1
Optional Int -> Optional String:
可选Int - >可选字符串:
If x: Int?
(or Double?
- doesn't matter)
如果x:Int? (或双倍? - 无关紧要)
var s = x.map({String($0)})
This will return String?
这将返回String?
To get a String
you can use :
要获得String,您可以使用:
var t = s ?? ""
#8
0
Swift 3:
var iString:Int = 100
var strString = String(iString)
extension String {
init(_ value:Int){/*Brings back String() casting which was removed in swift 3*/
self.init(describing:value)
}
}
This avoids littering your code with the verbose: String(describing:iString)
这可以避免使用verbose:String(描述:iString)乱丢你的代码
Bonus: Add similar init methods for commonly used types such as: Bool
, CGFloat
etc.
额外:为常用类型添加类似的init方法,例如:Bool,CGFloat等。
#9
-1
Crude perhaps, but you could just do:
也许原油,但你可以这样做:
let int100 = 100
println(int100.description) //Prints 100