10 Tips For Improving Your Swift Programming

In June of 2014, Apple released the Swift programming language for iOS and OS X applications. Following this, Objective C developer Rick Walter created a Udemy course covering Swift development. The course is aimed at people wanting to learn Swift, including new programmers and Objective C developers alike. As a free bonus, Walter describes 10 Tips for improving your Swift programming.

  1. Swift compilers are usually able to determine the variable type from the context, so leverage this type inference to simplify variable declarations
  2. Contrary to when a reference type is assigned to a variable, which assigns a reference to the instance rather than the instance itself, value types assigned to a variable pass the actual value by copying it. Despite the seeming inefficiency, the compiler is intelligent enough to recognise the conditions and act accordingly, optimizing usage
  3. Manipulating data in Objective C often involves using a for-each loop. However, you can link multiple tasks while simplifying the code by using Swift’s Reduce, Map and Filter methods
  4. Harness the power of generic programming methods to overcome Swift’s lack of an explicit conversion for duplicating an algorithm or type to work with different data types
  5. With Objective C offering no special value to indicate the lack of a value, Swift has introduced Optionals to indicate the absence of a value. These are implemented as a generic enumeration but are made less verbose through the use of syntactic sugar
  6. A Tuple (a value type) is a list of typed values optionally named and enclosed in parentheses. These anonymous structures can be used when a function is returning more than one value
  7. Swift enumerations with associated values can be used to improve on the standard “either-or” method which returns a value or error. This is done by assigning associated value types in the case declaration
  8. Swift Closures are unnamed, self-contained blocks of code that fall under the category of reference type. They replace the blocks in Objective C and can be executed at a later time without any invocation limits
  9. Make the most of Swift’s supercharged switch statements that can be used with almost any type. But make the cases list exhaustive to ensure that every possible value is assigned to at least one case
  10. Work around the limitations of Swift for use in frameworks by combining Swift with Objective C in the same project. However, to prevent creating a circular reference, do not expose Objective C code to Swift if it the two are interdependent.
Original Article

Swift - Learn Apple's New Programming Language Step By Step

Martin W Brennan Martin W Brennan is a co-founder of ViewPop, the social network that puts the creation of 3D photos and videos in the hands of anyone with a smartphone. For his day job, Martin is a copywriting consultant at We Write Words, learning about the world as he writes about it.

Comments

Comments(1)

Patrick Weigel

Great post. The concept of optionals (#5) is the most important Swift concept (assuming you already know a programming language). Also the use of idioms relating to optionals such as "if let".

The power of the switch statement was another great Swift feature.

I haven't use .map, .filter yet, but I can see how they are clearer at expressing intent compared to an explicit loop.