swift optionals - 1
學(xué)習(xí)是個(gè)循序漸進(jìn)的過程,萬事開頭難. Swift說起來簡單易上手,但有一些知識(shí)要反復(fù)琢磨,多加練習(xí),才會(huì)深入理解。
Need to practise a lot and test what is Optiontal Type and how to use it very well
Optional 介紹:?
An optional value either contains a value or contains nil (no value) to indicate that a value is missing. Write a question mark (?) after the type of a value to mark the value as optional.
If a var or constant could be a nil, the var must be declared as optional
Optional is an enumeration
Optional 例子:
1. pNumber is a string constant
2. when cast it to a Int, it might be a Int or nil since some strings can not be casted, liked "good"
3. Optional binding statement is if let a = b {}
If you are sure the optional does contain a value, it can be accessed by pNumber! (Implicitly optional)
Sample code:
let?pNumber =?"123"
//let converterNumber = Int(possibleNumber!)
if?let?actualNumber =?Int(pNumber) {?//casting and force it to a constant. A constan
? ??print("\'\(pNumber)\' has an integer valuel of?\(actualNumber)")
}?else?{
? ??print("\'\(pNumber)\' could not be converted to an integer")
}
轉(zhuǎn)載于:https://www.cnblogs.com/iyoubo/p/4609319.html
總結(jié)
以上是生活随笔為你收集整理的swift optionals - 1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转] 机器学习那些事儿
- 下一篇: (三)PHP网页架站