Edit: The intent of this question was to understand the use of the implicit optional operator "!" in the early UIKit API updates, specifically if a function was declared as returning a type expected to be non optional, why use optional at all, and if it was optional, why not use the optional "?" operator. The semantics of "!" were always clear though.
As time later showed, it was a matter of Apple auditing the API to make the optionality more precise, using ? for truly optional types, and using non optional types when they were in fact non optional. This was a hold-over of the original Objective-C signatures which were often ambiguous.
The swift documentation explains the purpose of the ! unboxing operator on optional types,
var optionalString : String? = "optional"
var regularString: String = optionalString!
but they have used it on type definitions themselves (String!), without an explicit explanation that I can find.
Example:
func takesBang(value:String!) -> String {
if !value {
return "nil value, without the safe syntax"
}
return "This works"
}
var unsafe:String!
takesBang(unsafe) // yields "nil value, without the safe syntax"
The String! type does not force an unboxing of the optional type, but only seems to remove the need for optional syntax (?.). Apple uses this in their own examples, but it seems to only negate the optional safety (pointer) mechanisms.
Can anybody explain the purpose / motivation? This seems generally unsafe as the caller won't have to check or at least think about their value.