Reputation: 3162
In Swift, i cant cast Int to String by:
var iString:Int = 100
var strString = String(iString)
But my variable in Int? , there for error: Cant invoke 'init' with type '@Ivalue Int?'
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???
}
Upvotes: 38
Views: 47124
Reputation: 3830
If you want an empty string if it not set (nil)
extension Int? {
var stringValue:String {
return self == nil ? "" : "\(self!)"
}
}
Upvotes: 0
Reputation: 275
You can try this to convert Int? to string
let myString : String = "42"
let x : Int? = myString.toInt()
let newString = "\(x ?? 0)"
print(newString) // if x is nil then optional value will be "0"
Upvotes: 0
Reputation: 388
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)
Apple's flatMap(_:) Documentation
Upvotes: 28
Reputation: 5569
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)
Bonus: Add similar init methods for commonly used types such as: Bool
, CGFloat
etc.
Upvotes: 0
Reputation: 1554
Optional Int -> Optional String:
If x: Int?
(or Double?
- doesn't matter)
var s = x.map({String($0)})
This will return String?
To get a String
you can use :
var t = s ?? ""
Upvotes: 6
Reputation: 11276
For preventing unsafe optional unwraps I use it like below as suggested by @AntiStrike12,
if let theString = someVariableThatIsAnInt {
theStringValue = String(theString!))
}
Upvotes: 1
Reputation: 24572
You can use string interpolation.
let x = 100
let str = "\(x)"
if x
is an optional you can use optional binding
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
.
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 (??)
let str = x ?? ""
Upvotes: 51
Reputation: 23986
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:
var string = optionalInt?.stringValue
Upvotes: 28
Reputation: 230
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!)
Upvotes: 1
Reputation: 774
Sonrobby, I believe that "Int?" means an optional int. Basically, by my understanding, needs to be unwrapped.
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.
Upvotes: 1
Reputation: 2538
Crude perhaps, but you could just do:
let int100 = 100
println(int100.description) //Prints 100
Upvotes: 1