14 Handy Pointers For Programming With Apple Swift

By | January 9, 2015
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

All set to start creating iOS apps with Swift? Go through our round-up of some interesting features of Apple’s new programming language, before getting down to code with it.

 

Apple’s all-new programming language, Swift, has been in the news ever since it was initially announced at WWDC 2014. The internet has been flooded with online tutorials, for programmers and app developers to get a hang of the new language. On October 7, Apple posted a video on its official developer blog, providing directions as to how iOS apps could be created using the new programming language. We will here share a few tips and pointers that would come in handy while coding for apps with Swift:

 

  1. No more ‘go to fail’ errors – Swift has been conceptualized as a more secure programming language than Objective-C. Software and mobile app developers need to use open brackets while using ‘if’ statements in their codes. This, in turn, removes all chances of the problematic SSL ‘go to fail’ bug message. A default statement also has to be provided in the switch statements. That way, even if none of the statement possibilities are satisfied, the program will have something to run on.
  2. Using Swift REPL – One of the smartest (and easiest too) ways to interact with the Swift codes is Apple’s very own ‘Read-Eval-Print-Loop’ (or, REPL) technique. The best thing about REPL is that you need not be working in an Xcode playground to use the method. All that you require are the program command lines and the latest version of Xcode beta – and you will be able to implement Swift REPL right from the Terminal on your Mac system running on OS X Yosemite.
  3. Working with Arrays – The way in which iPhone app developers can create and manage arrays in Swift is a major departure from the manner in which the same tasks can be done in good ol’ Objective-C. In particular, the process of adding objects to arrays has been made easier than ever before. The following line of code does the trick:

          <code>var yourArray = [“Hello”] var yourString: String = “World” yourArray+=yourString

  1. Other array-related functionality in Swift – Apart from allocating arrays and adding    objects to them, Swift allows iOS app coders make and manage array subsets, change the objects inside arrays and join multiple arrays with ease. The feature that allows arrays to be declared as ‘constants’ (Constant Arrays) is also worth a mention. Working with Arrays can become significantly faster in Swift, once you have invested some time in learning the language thoroughly.
  2. Creating a caching library – A common problem of both Objective-C and Swift is data caching, particularly when developers are working with lengthy codes. With Apple’s new programming language, caching libraries can be created to do away with this issue. For instance, in table view, images can be cached in the library – till the time the table view is finally deallocated. After that, the cache gets cleared. That way, scrolling remains smooth, your coding speed remains high, and there is no chance of unnecessary data overloading. However, for smaller Swift codes, default libraries like NSCache might be enough.
  3. Advantage of Inferred Typing – With Swift, coders from iPhone app companies no longer have to explicitly set types in their codes at all times. The built-in compiler can access information to activate ‘Inferred Typing’, to set types on its own. Of course, the option of explicit typing is still available, but picking ‘inferred typing’ generally makes app codes much easier to use/understand/share.
  4. Optionals in Swift – In Objective-C, every property has to be declared with an initial value (barring which, an error message is generated). In Swift too, variables are assigned the ‘non-optional’ attribute (i.e., valid values are required) by default. However, coders get more leeway in this language, thanks to the ‘Optionals’ feature. A ‘question-mark’ (?) has to be included to define any variable as ‘optional’, and it does not contain any initial value (remember that having a nil value is a different thing altogether). What’s more – values can be added to variables declared as ‘optional’ later on.

                Example of Optionals in Swift: var message = String? //OK

  1. Using generics for more customized functions – What ‘Templates’ are for the C++ language, ‘Generics’ are (roughly) for Swift. You no longer have to rewrite function types for every different case (e.g., when it has to handle integers and floating point numbers). Generics are, in essence, functions which offer enhanced reusability with all types of variable types. Once you are comfortable with using Generics, you will find that the total amount of manual coding required has gone down quite a bit.
  2. Working in Xcode Playgrounds – To take full advantage of the features of Swift, iPhone app development experts need to have in-depth knowledge of Xcode Playgrounds first. You need to download Xcode 6 on your system, and move to File → New File → iOS\Source\Playground. Once you have clicked on ‘Next’ a new editor file will open. Rename the file with the ‘ .playground’ extension. As you start coding in the editor file, the outputs will be viewable on a real-time basis in the sidebar. This, understandably makes code debugging – and ultimately, the overall mobile app testing – a lot more foolproof.
  3. Overriding dealloc – Yet another effective way for iOS memory management while coding with Swift is by overriding the ‘dealloc’ method. In other words, while creating iPhone apps, developers can deallocate the viewcontroller whenever required. In addition, the retain cycle within an app code can be found very easily with this method. Never deallocating the ‘dealloc’ method (in either Swift or Objective-C often leads to memory leakages and errors.
  4. Do not expect a big difference in speed – iOS apps created with Swift are accepted at the Apple app store (the new language was accorded ‘Gold Master’ status in early-September). However, contrary to initial expectations (and despite the name!) Swift is roughly similar to Objective-C in terms of coding speed. This is, in fact, not a surprising fact – since both the languages use the Cocoa and Cocoa Touch frameworks (for making Mac and iOS applications). In any case, Apple does not have any immediate plans to phase out the 30-year old Objective-C language anytime soon, and Swift is meant to be used in collaboration with it.
  5. Compound variables in tuples – Unlike most other programming languages, the code tuples in Swift do not require that the values in them have to be identical. Right from integers and string elements, to Boolean values – multiple types of values can be stored together in a single Swift tuple. The tuples can be further divided into variables and constants as well. Accessing a tuple in Swift by its index number is not at all difficult either.
  6. ‘Let’ vs ‘Var’ – In an Apple Swift code, variables are declared with the ‘var’ keyword, while all constants must have the ‘let’ keyword preceding them. While declaring every value as variables offers greater flexibility, coding experts have highlighted that doing so prevents the Swift compiler from functioning optimally. In fact, you should try to use the ‘let’ keyword (i.e., use Constants) as much as possible. Use ‘var’ only when it is absolutely necessary.
  7. No more semicolons – Just as in Python, you need not worry about putting a semicolon at the end of every statement in a Swift code. For iPhone app developers used to coding in Objective-C, Swift offers a way out from forever being wary of error messages being flashed – whenever they forget to put in a semicolon after any line. Furthermore, the Swift shares the code bases and libraries of Objective-C – which makes it easier for developers to make the transition to the new language.


The growing popularity of Swift programming language would pose at least one difficulty for professionals who specialize in cross-platform mobile app development – since porting iOS apps to the Android platform will become much tougher than before. However, that is only a minor blip in what is a really well-conceived and user-friendly language. You have to agree with what most developers have to say – “Swift is the future”.

Hussain Fakhruddin
Follow me

Hussain Fakhruddin

Hussain Fakhruddin is the founder/CEO of Teknowledge mobile apps company. He heads a large team of app developers, and has overseen the creation of nearly 600 applications. Apart from app development, his interests include reading, traveling and online blogging.
Hussain Fakhruddin
Follow me
 

Leave a Reply

Your email address will not be published. Required fields are marked *