19

Standard libraries on iOS

Embed Size (px)

Citation preview

SWIFT STANDARD LIBRARY FOUNDATION

CORE FOUNDATION

AGENDA

Three standard libraries

Bridging

Swift Standard Library highlights

THREE STANDARD LIBRARIES

WHY THREE STANDARD LIBS?Swift Standard Library: Written entirely in

Swift, no dependency on Obj-C

Foundation: Written in Obj-C, Core

Framework for Cocoa development

Core Foundation: Written primarily in C.

Powers many Foundation features.

Created as common ground for Cocoa

and Carbon

Core Foundation

Foundation

Swift Standard Library

SWIFT STANDARD LIBRARY

Data Structures and fundamental types: Array, Dictionary, String, Int

Optionals and other language features have protocol

counterparts in the standard library (make Swift very extensible)

Swift Standard Library is still very small, no date calculation,

no URL loading, no file system access, etc.

SWIFT STANDARD LIBRARY

Consists mostly of structs, enums and protocols

Array, Dictionary, Int, String, etc. are implemented as structs

FOUNDATION

Data Structures, e.g.: NSArray, NSDictionary,

Date and Times, e.g.: NSDate, NSCalendar

URL Loading System, e.g.: NSURLSession

File System Access: NSFileManager

FOUNDATION

All foundation types are implement as classes

Immutable is the default: NSString, NSArray

Mutable classes are available when necessary: NSMutableString, NSMutableArray

BRIDGING

BRIDGING

Most Core Foundation types are toll free bridged to their

Foundation counterparts

Most Foundation types are toll free bridged to their Swift

Standard Library counterparts

More details in the Swift-Obj-C intro lecture

BRIDGING

Swift handles the length of Unicode strings correctly, Foundation’s NSString does not

More on this on next slides!

SWIFT STANDARD LIBRARY HIGHLIGHTS

STRINGSStrings in Swift 2 aren’t collections, they don’t have a length

The amount of characters visible to a user and the amount of

code points to represent these characters can differ 😁

Accessing individual characters is an O(n) operation

OPTIONALS

Are implemented as part of the Swift Standard Library

Optionals are enums:

public enum Optional<T> : _Reflectable, NilLiteralConvertible { case None case Some(T)

//...}

switch optionalValue {case let .Some(value): print(value)case .None: print("Nothing")}

OPTIONALS

And ImplicitlyUnwrappedOptional is an enum, too!

PROTOCOLSProtocols are essential building block of the Swift standard

library, make it extensible

Want to create a type that can be created from a string?

struct Car: StringLiteralConvertible {//…

}

let car: Car = "Mercedes"

Implementation details: Gist: Implement StringLiteralConvertible on custom type

ADDITIONAL RESOURCES