Is there a difference between as? String
vs. as String?
in Swift? If so, what's the difference and when should I use one vs. another?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
From
The Swift Programming Language
book,as
is a type cast operator which we use to downcast to thesubclass
andas?
is used for an optional form, when we are not sure if thedowncast
will succeed. Consider the following exampleThe example starts by trying to
downcast
the current item as aMovie
. Becauseitem
is aMediaItem
instance, it’s possible that it might be aMovie
; equally, it’s also possible that it might be aSong
, or even just a base MediaItem.String?
Anoptional value
either contains a value or containsnil
to indicate that the value is missing.From this,
as? String
means when you don't know what you're downcasting, you are assuming that as aString
, but it might meInteger
orFloat
orArray
orDictionary
as String?
means it's anOptional Value
, it may either contain aString
orNil
value.There's a subtle but important difference:
variable as? String
:variable
can be any type, such as an array, an integer, etc. Cast to string if it's a string, set tonil
otherwise.variable as String?
:variable
is aString?
, but stored in an opaque type, such asAnyObject?
, or it's a non optional string. If it's something different, a runtime exception is generated.Some examples:
So:
as? Type
means: cast to this type, if possible, otherwise evaluate tonil
as Type?
means: cast to an optionalType
, because I know it's an optionalType
. I understand that if it's not that, a runtime exception is generatedHowever, the real difference is between
as?
andas
: the former is an attempt to cast, the latter is a forced cast, resulting in runtime error if not possible.Update Dec 14, 2015 Since Swift 1.2, there are 3 variations of the
as
operator:as?
is an attempt to cast, evaluating tonil
if cast failsas!
is a forced cast, resulting to an runtime exception if cast fails (this is whatas
previously did)as
is now a special type of cast to be used when casting to equivalent types, usually bridged types, such as Swift'sString
andNSString
.Yes there is a difference.
In the first case, you are doing an optional cast to the type
String
. This will return a value if the object you are attempting to cast is indeed aString
or nil if it is not.In the second case, you are doing a forced cast to the type
String?
. If the value you are casting is not a string, it will crash your program.YES, there is diffrence.
variable as String?
downcast to optional String.Ifvariable
is notString?
it will cause run-time exception.while
variable as? String
will returnnil
if yourvariable
is notString
type or return downcastvariable
to String. This is conditional downcasting, if you not sure about down-casting you need to use this .