IOS, IOS Questions, Swift

What is Swift (programming language)

Swift is a multi-paradigm, compiled programming language developed by Apple for iOS and OS X development. Introduced at Apple’s developer conference WWDC 2014,Swift is designed to replace Objective-C, Apple’s object-oriented language,[4] while working with Apple’s Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient against erroneous code. It is built with the LLVM compiler included in Xcode 6 beta, and uses the Objective-C runtime, allowing Objective-C, Objective-C++ and Swift code to run within a single program

Features
Swift is, in large part, a reimagining of the Objective-C language using modern concepts and syntax. During its introduction, it was described simply as “Objective-C without the C”.

Swift lacks pointers and other unsafe accessors, contrary to Objective-C which made widespread use of pointers to refer to objects, as well as their traditional use in embedded C-language code. Additionally, Objective-C’s use of a Smalltalk-like syntax for making method calls has been replaced with a dot-notation style and namespace system more in common with other C-derived modern languages like Java or C#. Swift introduces true named parameters and retains key Objective-C concepts, including protocols, closures and categories, often replacing former syntax with cleaner versions and allowing these concepts to be applied to other language structures, like enums.
Types, variables and optionals
Under the Cocoa and Cocoa Touch environments, many common classes were part of the Foundation Kit library. This included the NSString string library (using Unicode), the NSArray and NSDictionary collection classes, and many others. Obj-C provided various bits of syntactic sugar to allow some of these objects to be created on-the-fly within the language, but once created the objects were manipulated with object calls. For instance, concatenating two NSStrings required method calls similar to this:

NSString *str = @”hello,”;
str = [str stringByAppendingString:@” world”];
In Swift, many of these basic types have been promoted to be first-class citizens of the language and can be manipulated directly. For instance, strings are invisibly bridged to NSString and can now be concatenated with the + operator, allowing greatly simplified syntax; the previous example becoming

var str = “hello,”
str += ” world”
Libraries, runtime and development
Swift uses the same runtime as the existing Obj-C system on Mac OS and iOS. That means that Swift programs can be run on many existing platforms, including at least some capability under iOS 6 and OS X 10.8. More importantly, this means Swift and Obj-C code can be used in a single program, and by extension, C and C++ as well. In the case of Obj-C, Swift has considerable access to the object model, and can be used to subclass, extend and use Obj-C code to provide protocol support.

To aid development of such programs, and the re-use of existing code, Xcode 6 offers a semi-automated system that builds and maintains a “bridging header” to expose Obj-C code to Swift. This takes the form of an additional file that simply lists all of the Obj-C imports (includes) that are called by that Obj-C code. At that point, Swift can refer to the code by referring to that header as if it were a namespace. Obj-C code can use Swift code directly, by importing the automatically maintained header file that has the namespace followed by -Swift. For instance, a Swift class known as “MyClass” can be used in Obj-C with the code #import “MyClass-Swift.h”.

Swift also has limited support for attributes, metadata that is read by the development environment, and is not necessarily part of the compiled code. Like Obj-C, attributes use the @ syntax, but the currently available set is small. One example is the @IBOutlet attribute, which marks a given value in the code as an “outlet”, available for use within Interface Builder (IB). An “outlet” is device that binds the value of the on-screen display to an object in code.

Memory management
Swift uses Automatic Reference Counting (ARC) to manage memory. Apple at one time had garbage collection for OS X Objective-C, but deprecated it in favor of ARC and in iOS only supports ARC.One problem with ARC is the possibility of creating a strong reference cycle, where instances of two different classes each include a reference to the other. Swift provides the weak and unowned keywords that allow the programmer to prevent strong reference cycles from occurring. Typically a parent-child relationship would use a strong reference while a child-parent would use either weak reference, where parents and children can be unrelated, or unowned where a child always has a parent, but parent may not have a child. Weak references must be optional variables, since they can change and become nil.

A closure within a class can also create a strong reference cycle by capturing self references. The programmer can indicate which self references should be treated as weak or unowned using a capture list.

Debugging and Other Elements
A key element of the Swift system is its ability to be cleanly debugged and run within the development environment, using a read–eval–print loop (REPL), giving it interactive properties more in common with scripting systems like Python than traditional systems programming languages. The REPL is further enhanced with the new ‘playgrounds’ concept; ‘playgrounds’ are interactive views running within the Xcode environment that respond to code or debugger changes on-the-fly. If the code in question changes over time or with regard to some other ranged input value, the view can be used with the Timeline Assistant to demonstrate the output in an animated fashion. Apple claims that Swift “is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.”

mycodetips

Passionate about coding and software development holding 10+ Years of experience including 6+ years of experience on Mobile application Development.

More Posts