Swift is a new programming language for iOS and OS X app development, combining many parts, types and concepts of C and Objective-C languages. In this post we will have a quick look of some basic concepts like manipulating strings, declaring and looping arrays, using switches for checking conditions. We want to thank our user marksimon232 for all the snippets posted here.
Let’s start with the common String type.
You can create an empty string using double quotes (“”) as string literal or using the default String() constructor.
These two strings are both empty, and are equivalent to each other.
Moreover you can check if the string is empty accessing the boolean isEmpty property or getting its length using the built-in function “countElements”.
String variables can of course be concatenated, using (+) operator. Since in this case the string is mutable, you need to use “var” operator.
Otherwise declare the string using “let” operator if you want to assign it to a constant value.
Once set, you can iterate through characters in string using the “for” statement and print out the values.
You can access a UTF-8 representation of a String by iterating over its utf8 property.
Swift allows you to format a string, constructing a new string value from a mix of constants, variables and other types. This is called “String Interpolation”. For example you need to pass the name of the variable to be put into the string using a pair of parentheses prefixed by a backslash.
Finally you can compare strings, using “if”. Two String values are considered equal if they contain exactly the same characters in the same order.
Get the full snippet here
Arrays & Loops
Declaring arrays and managing their elements are quite simple operations in Swift.
You can declare a variable array just using the typical brackets notation (). Note that you may not specify the type of the data contained into the array, since Swift can infer it from the data type you are assigning from.
Data in array may be iterated in different ways.
- You can use a standard loop on indexes, getting the actual array length with the “count” property and then accessing the array using the index.
- You can iterate the array using a “for-in” statement, accessing each element in the array.
- If you need the integer index of each item as well as its value, you can use the global built-in”enumerate” function.
This will iterate over the array, returning a tuple for each item in the array composed of the index and the value for that item.
Arrays can be changed adding/removing items on top, on bottom or on certain position, using indexes.
You can do that using the built-in functions “append”, “insert”, “removeAtIndex” and “removeLast” as in the example below.
You can also split array in a subarray, specifying the range where to split the original data from. You can specify a pair of indexes, eg X,Y, indicating the range. Note the usage of double (..) or triple (…) dot notation used when you need to exclude/include from the resulting subarray the item at the Y index.
Get the full snippet here
In Swift, the switch statement is a lot more flexible than C or Objective-C and can be used to test a variety of comparison operations.
Note you DO NOT need to specify any BREAK statement, since there is no implicit fallthrough between cases.
This makes the switch statement safer and easier to use than in C, and avoids executing more than one switch case by mistake.
Pretty nice, isn’t it?
Get the full snippet here.
Find more about Swift on the official Apple Developing Area.