Swift Pocket Reference

Swift Pocket Reference

Anthony Gray

Language: English

Pages: 186

ISBN: 1491915420

Format: PDF / Kindle (mobi) / ePub

Swift Pocket Reference

Anthony Gray

Language: English

Pages: 186

ISBN: 1491915420

Format: PDF / Kindle (mobi) / ePub


Get quick answers for developing and debugging applications with Swift, Apple’s multi-paradigm programming language. This pocket reference is the perfect on-the-job tool for learning Swift’s modern language features, including type safety, generics, type inference, closures, tuples, automatic memory management, and support for Unicode.

Designed to work with Cocoa and Cocoa Touch, Swift can be used in tandem with Objective-C, and either of these languages can call APIs implemented in the other. Swift is still evolving, but it’s clear that Apple sees it as the future language of choice for iOS and OS X software development.

Topics include:

  • Swift’s Run-Eval-Print-Loop (REPL) and interactive playgrounds
  • Supported data types, such as strings, arrays, and dictionaries
  • Variables and constants
  • Program flow: loops and conditional execution
  • Classes, structures, enumerations, functions, and protocols
  • Closures: similar to blocks in Objective-C and lambdas in C#
  • Optionals: values that can explicitly have no value
  • Operators, operator overloading, and custom operators
  • Access control: restricting access to types, methods, and properties
  • Built-in global functions and their parameter requirements

Agile Software Engineering with Visual Studio: From Concept to Continuous Feedback (2nd Edition) (Microsoft .NET Development Series)

Professional Test Driven Development with C#: Developing Real World Applications with TDD (Wrox Professional Guides)

Quaternions for Computer Graphics

Oracle PL/SQL Programming (6th Edition)

Discovering SQL: A Hands-On Guide for Beginners (Wrox Programmer to Programmer)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

char‐ acters in UnicodeScalar format. Strings and Characters | 39 Comparing Strings You can compare strings and substrings by using the following comparison operators and methods: == != < <= > >= Returns true if two strings contain the same sequence of characters. Returns true if two strings contain different sequen‐ ces of characters. Returns true if the string to the left of the operator sorts lexically before the string to the right of the operator. Returns true if the string to

parameters, specify exter‐ nal parameter names to make clear the role of each parameter. You specify external parameter names as follows: func funcName(externalName internalName: type, …) func search (string s: String, forString s2: String) -> Int { … } The search function thus defined would be called like this: let openDoor = search(string: userInput, forString: "open sesame") Where the local and external parameter names can be the same, you can write the name once, and prefix it with #. For

area of the rectangle: class Rect { var x = 0.0, y = 0.0 var width = 0.0, height = 0.0 func area() -> Double { return width * height } } The method is called using dot syntax, as in this example: 90 | Swift Pocket Reference var q = Rect() q.width = 5.0 q.height = 2.0 q.area() // returns 10.0 Local and external parameter names In a function definition, parameter names are local unless an external parameter name is explicitly declared or the local parameter name is preceded by a # character.

convenience initializer: class Processor { var dataWidth = 0 var addressWidth = 0 var registers = 0 var name = "" init (name: String, dWidth: Int, aWidth: Int, regs: Int) { self.name = name dataWidth = dWidth addressWidth = aWidth registers = regs } convenience init (eightbitName: String, regs: Int) { self.init(name: eightbitName, dWidth:8, aWidth:16, regs: regs) } } Note that the convenience initializer defaults two of the four parameters required by the designated initializer, which it calls

assigned a value, the setter is called: var badPi: Float { return 22.0/7.0 } let radius=1.5 let circumference = 2.0 * badPi * radius As global or local variables, computed variables would appear to be of limited use, but the same syntax can also be used for properties in structures and classes. In this context, as compu‐ ted properties, the feature becomes more useful. For more information about computed properties, see the section “Prop‐ erties” on page 84 later in this book. Variable

Download sample

Download