Book Image

Mastering Swift 2

By : Jon Hoffman
Book Image

Mastering Swift 2

By: Jon Hoffman

Overview of this book

<p><span id="description" class="sugar_field">At their Worldwide Developer’s conference (WWDC) in 2015, Apple announced Swift 2, a major update to the innovative programming language they first unveiled to the world the year before. Swift 2 features exciting enhancements to the original iteration of Swift, acting, as Apple put it themselves as “a successor to the C and Objective-C languages.” – This book demonstrates how to get the most from these new features, and gives you the skills and knowledge you need to develop dynamic iOS and OS X applications.<br /> </span></p> <p><span id="description" class="sugar_field">Learn how to harness the newest features of Swift 2 todevelop advanced applications on a wide range of platforms with this cutting-edge development guide. Exploring and demonstrating how to tackle advanced topics such as Objective-C interoperability, ARC, closures, and concurrency, you’ll develop your Swift expertise and become even more fluent in this vital and innovative language. With examples that demonstrate how to put the concepts into practice, and design patterns and best practices, you’ll be writing better iOS and OSX applications in with a new level of sophistication and control.</span></p>
Table of Contents (24 chapters)
Mastering Swift 2
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Free Chapter
1
Taking the First Steps with Swift
2
Learning about Variables, Constants, Strings, and Operators
Index

Subscripts with Swift arrays


The following example shows how to use subscripts to access and change the values of an array:

var arrayOne = [1,2,3,4,5,6]
print(arrayOne[3])  //Displays '4'
arrayOne[3] = 10
print(arrayOne[3])  //Displays '10'

In the preceding example, we create an array of integers and then use the subscript syntax to display and change the item of element number 3 in the array. Subscripts are mainly used to get or retrieve information from a collection. We generally do not use subscripts when specific logic needs to be applied to determine which item to select. As examples, we will not use subscripts to append an item to the end of the array or to retrieve the number of items in the array. To append an item to the end of an array, or to get the number of items in an array, we will use functions or properties like this:

arrayOne.append(7)  //append 7 to the end of the array
arrayOne.count  //returns the number of items in an array

Subscripts in our custom types should follow the...