typealias
AnyClass
=
AnyObject
.
Type
typealias
AnyObject
The protocol to which all classes implicitly conform.
You use AnyObject
when you need the flexibility of an untyped object or
when you use bridged Objective-C methods and properties that return an
untyped result. AnyObject
can be used as the concrete type for an
instance of any class, class type, or class-only protocol. For example:
AnyObject
can also be used as the concrete type for an instance of a type
that bridges to an Objective-C class. Many value types in Swift bridge to
Objective-C counterparts, like String
and Int
.
The flexible behavior of the AnyObject
protocol is similar to
Objective-C's id
type. For this reason, imported Objective-C types
frequently use AnyObject
as the type for properties, method parameters,
and return values.
Casting AnyObject Instances to a Known Type
Objects with a concrete type of AnyObject
maintain a specific dynamic
type and can be cast to that type using one of the type-cast operators
(as
, as?
, or as!
).
This example uses the conditional downcast operator (as?
) to
conditionally cast the s
constant declared above to an instance of
Swift's String
type.
if
let
message
=
s
as
?
String
{
(
"Successful cast to String: \(
message
)"
)
}
// Prints "Successful cast to String: This is a bridged string."
If you have prior knowledge that an AnyObject
instance has a particular
type, you can use the unconditional downcast operator (as!
). Performing
an invalid cast triggers a runtime error.
Casting is always safe in the context of a switch
statement.
Accessing Objective-C Methods and Properties
When you use AnyObject
as a concrete type, you have at your disposal
every @objc
method and property---that is, methods and properties
imported from Objective-C or marked with the @objc
attribute. Because
Swift can't guarantee at compile time that these methods and properties
are actually available on an AnyObject
instance's underlying type, these
@objc
symbols are available as implicitly unwrapped optional methods and
properties, respectively.
This example defines an IntegerRef
type with an @objc
method named
getIntegerValue()
.
In the example, obj
has a static type of AnyObject
and a dynamic type
of IntegerRef
. You can use optional chaining to call the @objc
method
getIntegerValue()
on obj
safely. If you're sure of the dynamic type of
obj
, you can call getIntegerValue()
directly.
let
possibleValue
=
obj
.
getIntegerValue
?()
(
possibleValue
)
// Prints "Optional(100)"
let
certainValue
=
obj
.
getIntegerValue
()
(
certainValue
)
// Prints "100"
If the dynamic type of obj
doesn't implement a getIntegerValue()
method, the system returns a runtime error when you initialize
certainValue
.
Alternatively, if you need to test whether obj.getIntegerValue()
exists,
use optional binding before calling the method.
if
let
f
=
obj
.
getIntegerValue
{
(
"The value of 'obj' is \(
f
()
)"
)
}
else
{
(
"'obj' does not have a 'getIntegerValue()' method"
)
}
// Prints "The value of 'obj' is 100"
typealias
ArrayLiteralConvertible
=
ExpressibleByArrayLiteral
typealias
BidirectionalIndexable
=
BidirectionalCollection
A type that provides subscript access to its elements, with bidirectional index traversal.
In most cases, it's best to ignore this protocol and use the
BidirectionalCollection
protocol instead, because it has a more complete
interface.
Deprecated: it will be removed in Swift 4.0. Please use 'BidirectionalCollection' instead.
typealias
BidirectionalSlice
<
T
>
=
Slice
<
T
>
typealias
BooleanLiteralConvertible
=
ExpressibleByBooleanLiteral
typealias
BooleanLiteralType
=
Bool
The default type for an otherwise-unconstrained Boolean literal.
When you create a constant or variable using one of the Boolean literals
true
or false
, the resulting type is determined by the
BooleanLiteralType
alias. For example:
let
isBool
=
true
(
"isBool is a '\(
type
(
of
:
isBool
)
)'"
)
// Prints "isBool is a 'Bool'"
The type aliased by BooleanLiteralType
must conform to the
ExpressibleByBooleanLiteral
protocol.
The C 'char' type.
This will be the same as either CSignedChar
(in the common
case) or CUnsignedChar
, depending on the platform.
typealias
CChar32
=
Unicode.Scalar
The C++11 'char32_t' type, which has UTF-32 encoding.
typealias
CLongDouble
=
Float80
typealias
CSignedChar
=
Int8
The C 'signed char' type.
typealias
CUnsignedChar
=
UInt8
The C 'unsigned char' type.
typealias
CUnsignedInt
=
UInt32
The C 'unsigned int' type.
typealias
CUnsignedLong
=
UInt
typealias
CUnsignedLongLong
=
UInt64
The C 'unsigned long long' type.
typealias
CUnsignedShort
=
UInt16
The C 'unsigned short' type.
typealias
CWideChar
=
Unicode.Scalar
The C++ 'wchar_t' type.
typealias
ClosedRangeIndex
<
T
>
=
ClosedRange
<
T
>
.
Index
A type that can convert itself into and out of an external representation.
Codable
is a type alias for the Encodable
and Decodable
protocols.
When you use Codable
as a type or a generic constraint, it matches
any type that conforms to both protocols.
typealias
CountableClosedRange
<
Bound
>
=
ClosedRange
<
Bound
>
typealias
CountablePartialRangeFrom
<
Bound
>
=
PartialRangeFrom
<
Bound
>
typealias
CountableRange
<
Bound
>
=
Range
<
Bound
>
typealias
DefaultBidirectionalIndices
<
T
>
=
DefaultIndices
<
T
>
typealias
DefaultRandomAccessIndices
<
T
>
=
DefaultIndices
<
T
>
typealias
DictionaryIndex
<
Key
, =
Dictionary
<
Key
,
Value
>
.
Index
typealias
EmptyIterator
<
T
>
=
EmptyCollection
<
T
>
.
Iterator
typealias
ExpressibleByStringInterpolation
A type that can be initialized by string interpolation with a string literal that includes expressions.
Use string interpolation to include one or more expressions in a string literal, wrapped in a set of parentheses and prefixed by a backslash. For example:
let
price
=
2
let
number
=
3
let
message
=
"One cookie: $\(
price
), \(
number
) cookies: $\(
price
*
number
)."
(
message
)
// Prints "One cookie: $2, 3 cookies: $6."
Conforming to the ExpressibleByStringInterpolation Protocol
The ExpressibleByStringInterpolation
protocol is deprecated. Do not add
new conformances to the protocol.
Deprecated: it will be replaced or redesigned in Swift 4.0. Instead of conforming to 'ExpressibleByStringInterpolation', consider adding an 'init(_:String)'.
typealias
ExtendedGraphemeClusterType
=
String
The default type for an otherwise-unconstrained Unicode extended grapheme cluster literal.
typealias
FlattenBidirectionalCollection
<
T
>
=
FlattenCollection
<
T
>
typealias
FlattenBidirectionalCollectionIndex
<
T
>
=
FlattenCollection
<
T
>
.
Index
typealias
FlattenCollectionIndex
<
T
>
=
FlattenCollection
<
T
>
.
Index
typealias
FloatLiteralConvertible
=
ExpressibleByFloatLiteral
typealias
FloatLiteralType
=
Double
The default type for an otherwise-unconstrained floating point literal.
typealias
Indexable
=
Collection
A type that provides subscript access to its elements, with forward index traversal.
In most cases, it's best to ignore this protocol and use the Collection
protocol instead, because it has a more complete interface.
Deprecated: it will be removed in Swift 4.0. Please use 'Collection' instead.
typealias
IndexableBase
=
Collection
A type that provides subscript access to its elements, with forward index traversal.
In most cases, it's best to ignore this protocol and use the Collection
protocol instead, because it has a more complete interface.
Deprecated: it will be removed in Swift 4.0. Please use 'Collection' instead.
typealias
IntegerLiteralConvertible
=
ExpressibleByIntegerLiteral
typealias
IntegerLiteralType
=
Int
The default type for an otherwise-unconstrained integer literal.
typealias
JoinedIterator
<
T
>
=
JoinedSequence
<
T
>
.
Iterator
typealias
LazyBidirectionalCollection
<
T
>
=
LazyCollection
<
T
>
typealias
LazyDropWhileBidirectionalCollection
<
T
>
=
LazyDropWhileCollection
<
T
>
typealias
LazyDropWhileIndex
<
T
>
=
LazyDropWhileCollection
<
T
>
.
Index
typealias
LazyDropWhileIterator
<
T
>
=
LazyDropWhileSequence
<
T
>
.
Iterator
typealias
LazyFilterBidirectionalCollection
<
T
>
=
LazyFilterCollection
<
T
>
typealias
LazyFilterIndex
<
Base
>
=
Base
.
Index
typealias
LazyFilterIterator
<
T
>
=
LazyFilterSequence
<
T
>
.
Iterator
typealias
LazyMapBidirectionalCollection
<
T
, =
LazyMapCollection
<
T
,
E
>
typealias
LazyMapIterator
<
T
, =
LazyMapSequence
<
T
,
E
>
.
Iterator
typealias
LazyMapRandomAccessCollection
<
T
, =
LazyMapCollection
<
T
,
E
>
typealias
LazyPrefixWhileBidirectionalCollection
<
T
>
=
LazyPrefixWhileCollection
<
T
>
typealias
LazyPrefixWhileIndex
<
T
>
=
LazyPrefixWhileCollection
<
T
>
.
Index
typealias
LazyPrefixWhileIterator
<
T
>
=
LazyPrefixWhileSequence
<
T
>
.
Iterator
typealias
LazyRandomAccessCollection
<
T
>
=
LazyCollection
<
T
>
typealias
MutableBidirectionalSlice
<
T
>
=
Slice
<
T
>
typealias
MutableIndexable
=
MutableCollection
A type that provides subscript access to its elements.
In most cases, it's best to ignore this protocol and use the
MutableCollection
protocol instead, because it has a more complete
interface.
Deprecated: it will be removed in Swift 4.0. Please use 'MutableCollection' instead.
typealias
MutableRandomAccessSlice
<
T
>
=
Slice
<
T
>
typealias
MutableRangeReplaceableBidirectionalSlice
<
T
>
=
Slice
<
T
>
typealias
MutableRangeReplaceableRandomAccessSlice
<
T
>
=
Slice
<
T
>
typealias
MutableRangeReplaceableSlice
<
T
>
=
Slice
<
T
>
typealias
MutableSlice
<
T
>
=
Slice
<
T
>
typealias
NilLiteralConvertible
=
ExpressibleByNilLiteral
typealias
RandomAccessIndexable
=
RandomAccessCollection
A collection that supports efficient random-access index traversal.
In most cases, it's best to ignore this protocol and use the
RandomAccessCollection
protocol instead, because it has a more complete
interface.
Deprecated: it will be removed in Swift 4.0. Please use 'RandomAccessCollection' instead.
typealias
RandomAccessSlice
<
T
>
=
Slice
<
T
>
typealias
RangeReplaceableBidirectionalSlice
<
T
>
=
Slice
<
T
>
typealias
RangeReplaceableIndexable
=
RangeReplaceableCollection
A type that supports replacement of an arbitrary subrange of elements with the elements of another collection.
In most cases, it's best to ignore this protocol and use the
RangeReplaceableCollection
protocol instead, because it has a more
complete interface.
Deprecated: it will be removed in Swift 4.0. Please use 'RandomAccessCollection' instead.
typealias
RangeReplaceableRandomAccessSlice
<
T
>
=
Slice
<
T
>
typealias
RangeReplaceableSlice
<
T
>
=
Slice
<
T
>
typealias
ReversedIndex
<
T
>
=
ReversedCollection
<
T
>
typealias
ReversedRandomAccessCollection
<
T
>
=
ReversedCollection
<
T
>
typealias
SetIndex
<
Element
>
=
Set
<
Element
>
.
Index
typealias
StringInterpolationConvertible
Deprecated: it will be replaced or redesigned in Swift 4.0. Instead of conforming to 'StringInterpolationConvertible', consider adding an 'init(_:String)'.
typealias
StringLiteralConvertible
=
ExpressibleByStringLiteral
typealias
StringLiteralType
=
String
The default type for an otherwise-unconstrained string literal.
typealias
UTF16
=
Unicode.UTF16
typealias
UTF32
=
Unicode.UTF32
typealias
UTF8
=
Unicode.UTF8
typealias
UnboundedRange
= (
UnboundedRange_
)
The type of an unbounded range operator.
typealias
UnfoldFirstSequence
<
T
>
=
UnfoldSequence
<
T
, (
T
?,
Bool
)
>
The return type of sequence(first:next:)
.
typealias
UnicodeScalar
=
Unicode.Scalar
typealias
UnicodeScalarType
=
String
The default type for an otherwise-unconstrained unicode scalar literal.
typealias
UnsafeBufferPointerIterator
<
T
>
=
UnsafeBufferPointer
<
T
>
.
Iterator
typealias
UnsafeMutableRawBufferPointerIterator
<
T
>
=
UnsafeBufferPointer
<
T
>
.
Iterator
typealias
UnsafeRawBufferPointerIterator
<
T
>
=
UnsafeBufferPointer
<
T
>
.
Iterator
typealias
Void
= ()
The return type of functions that don't explicitly specify a return type,
that is, an empty tuple ()
.
When declaring a function or method, you don't need to specify a return
type if no value will be returned. However, the type of a function,
method, or closure always includes a return type, which is Void
if
otherwise unspecified.
Use Void
or an empty tuple as the return type when declaring a closure,
function, or method that doesn't return a value.
typealias
Zip2Iterator
<
T
, =
Zip2Sequence
<
T
,
U
>
.
Iterator
The protocol to which all class types implicitly conform.
You can use the
AnyClass
protocol as the concrete type for an instance of any class. When you do, all known@objc
class methods and properties are available as implicitly unwrapped optional methods and properties, respectively. For example:The
getDefaultValue(_:)
function uses optional chaining to safely call the implicitly unwrapped class method onc
. Calling the function with different class types shows how thegetDefaultValue()
class method is only conditionally available.