Types

Swift provides all standard types.

base types

class description
Bool A value type whose instances are either true or false.
Int A signed integer value type. On 32-bit platforms, Int is the same size as Int32, and on 64-bit platforms, Int is the same size as Int64.
Float A single-precision, floating-point value type.
Double A double-precision, floating-point value type.
Character A single extended grapheme cluster that approximates a user-perceived character.
String A Unicode string value that is a collection of characters.
Array An ordered, random-access mutable collection.
Dictionary A mutable collection whose elements are key-value pairs.
Set An unordered mutable collection of unique elements.
Date Represents a specific point in time, independent of any calendar or time zone.
DateInterval Represents the span of time between a specific start date and end date.
TimeInterval Represents a number of seconds.
Void Indicates that a function or method does not return a value. Used with function types and lambdas. Actually it is allias to the empty tuple (). Tuple is compound type without a name.

Optional type

An Optional type allows to hold either a value or no value. Optionals typically are written by appending a ? to any type.

let shortForm: Int? = Int("42")
let longForm: Optional<Int> = Int("42")

The nil literal indicates that variable does not have value. It is equivalent to the Optional.none.

let noNumber1: Int? = nil
let noNumber2: Int? = Optional.none
print(noNumber2 == nil) // print true

Foundation Kit

You can import Foundation framework to use class with a NS prefix like in Objective-C.

import Foundation

let str1: NSString = "hello"
let str2: String = "hello"
class description
NSObject Base class for Objective-C hierarchies and provides standard methods for working with objects.
NSString Represents immutable Unicode string.
NSMutableString Represents mutable Unicode string.
NSNumber An object wrapper for primitive scalar numeric values (i.e. for Objective-C types signed char, unsigned char, short int, int, long int, long long int, float, or double or as a BOOL).
NSArray Represents immutable ordered collection.
NSArray Represents immutable ordered collection.
NSMutableArray Represents mutable ordered collection.
NSDictionary Represents a immutable collection whose elements are key-value pairs.
NSMutableDictionary Represents a mutable collection whose elements are key-value pairs.
NSSet An unordered immutable collection of unique elements.
NSMutableSet An unordered mutable collection of unique elements.
NSDate Represents a specific point in time, independent of any calendar or time zone.
NSMutableSet An unordered mutable collection of unique elements.

You can convert some swift type to the corresponding NS type using operator as.

let colors = ["periwinkle", "rose", "moss"]
let moreColors: [String?] = ["ochre", "pine"]


let url = URL(fileURLWithPath: "names.plist")
(colors as NSArray).write(to: url, atomically: true)
// true


(moreColors as NSArray).write(to: url, atomically: true)
// error: cannot convert value of type '[String?]' to type 'NSArray'