So in order to answer your question and here you can have some optional cases as follows :
your first one:
let intValue: Int? = rawValue == nil ? Int(rawValue) : nil
your second one:
var intValue: Int?
if let unwrappedRawValue = rawValue {
intValue = Int(unwrappedRawValue)
}
third case:
var intValue : Int?
if intValue !=nil
{
//do something
}
fourth case if you are sure the value is not nil
var intValue : Int?
intValue!
The last case will crash your application in case it's value is nil so you might use it for debugging purposes in the future. I suggest you have a look on those links for optional binding and optional chaining from apple's manual
optional Chaining
full apple guide for swift
and to finish with answering your comment section question most developers tend to use this method:
var intValue: Int?
if let unwrappedRawValue = rawValue {
intValue = Int(unwrappedRawValue)
}
cause it seems to be the most type safe. Your call.