Swift 4 - The powerful easy to learn programming language

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for iOS, macOS, watchOS, tvOS, Linux and z/OS. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. It is built with the open source LLVM compiler framework and has been included in Xcode since version 6. On Apple platforms it uses the Objective-C runtime library which allows C, Objective-C, C++ and Swift code to run within one program.



Apple intended Swift to support many core concepts associated with Objective-C, notably dynamic dispatch, widespread late binding, extensible programming and similar features, but in a "safer" way, making it easier to catch software bugs; Swift has features addressing some common programming errors like null pointer dereferencing and provides syntactic sugar to help avoid the pyramid of doom. Swift supports the concept of protocol extensibility, an extensibility system that can be applied to types, structs and classes, which Apple promotes as a real change in programming paradigms they term "protocol-oriented programming"(similar to traits).

Swift was introduced at Apple's 2014 Worldwide Developers Conference (WWDC).It underwent an upgrade to version 1.2 during 2014 and a more major upgrade to Swift 2 at WWDC 2015. Initially a proprietary language, version 2.2 was made open-source software under the Apache License 2.0 on December 3, 2015, for Apple's platforms and Linux.

Different major versions have been released annually with incompatible syntax and library invocations that require significant source code rewrites. For larger code bases this has caused many developers to dismiss Swift until a more stable version becomes available.

Swift is an alternative to the Objective-C language that employs modern programming-language theory concepts and strives to present a simpler syntax. During its introduction, it was described simply as "Objective-C without the C".
By default, Swift does not expose pointers and other unsafe accessors, in contrast to Objective-C, which uses pointers pervasively to refer to object instances. Also, 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 familiar to programmers from other common object-oriented (OO) 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 enumerated types (enums)

Syntactic sugar

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 others. Objective-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, in Objective-C 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 the language's core, and can be manipulated directly. For instance, strings are invisibly bridged to NSString (when Foundation is imported) and can now be concatenated with the + operator, allowing greatly simplified syntax; the prior example becoming:


var str = "hello,"
str += " world"

Swift supports five access control levels for symbols: open, public, internal, fileprivate, and private. Unlike many object-oriented languages, these access controls ignore inheritance hierarchies: private indicates that a symbol is accessible only in the immediate scope, fileprivate indicates it is accessible only from within the file, internal indicates it is accessible within the containing module, public indicates it is accessible from any module, and open (only for classes and their methods) indicates that the class may be subclassed outside of the module.


Comparisons to other languages


This section is in a list format that may be better presented using prose. You can help by converting this section to prose, if appropriate. Editing help is available. (February 2017)
Swift is similar to C in various ways:


  • Most C operators are used in Swift, but there are some new operators.
  • Curly braces are used to group statements.
  • Variables are assigned using an equals sign, but compared using two consecutive equals signs. A new identity operator, ===, is provided to check if two data elements refer to the same object.
  • Control statements while, if, and switch are similar, but have extended functions, e.g., a switch that takes non-integer cases, while and if supporting pattern matching and conditionally unwrapping optionals, etc.
  • Square brackets are used with arrays, both to declare them and to get a value at a given index in one of them.
  • It also has similarities to Objective-C:
  • Basic numeric types (Int, UInt, Float, Double)
  • Class methods are inherited, like instance methods; self in class methods is the class the method was called on.
  • Similar for...in enumeration syntax.
  • Differences from Objective-C include:
  • Statements do not need to end with semicolons (;), though these must be used to allow more than one statement on a line.
  • No header files.
  • Uses type inference.
  • Generic programming.
  • Functions are first-class objects.
  • Enumeration cases can have associated data (algebraic data types).
  • Operators can be redefined for classes (operator overloading), and new operators can be defined.
  • Strings fully support Unicode. Most Unicode characters can be used in either identifiers or operators.
  • No exception handling. Swift 2 introduces a different and incompatible error-handling model.
  • Several notoriously error-prone behaviors of earlier C-family languages have been changed:
  • Pointers are not exposed by default. There is no need for the programmer to keep track of and mark names for referencing or dereferencing.
  • Assignments return no value. This prevents the common error of writing i = 0 instead of i == 0 by throwing a compile-time error.
  • No need to use break statements in switch blocks. Individual cases do not fall through to the next case unless the fallthrough statement is used.
  • Variables and constants are always initialized and array bounds are always checked.
  • Integer overflows, which result in undefined behavior for signed integers in C, are trapped as a run-time error in Swift. Programmers can choose to allow overflows by using the special arithmetical operators &+, &-, &*, &/ and &%. The properties min and max are defined in Swift for all integer types and can be used to safely check for potential overflows, versus relying on constants defined for each type in external libraries.
  • The one-statement form of if and while, which allows for the omission of braces around the statement, is unsupported.
  • C-style enumeration for (int i = 0; i < c; i++), which is prone to off-by-one errors, is unsupported (from Swift 3 onward).
  • The pre- and post- increment and decrement operators (i++, --i ...) are unsupported (from Swift 3 onward), more so since C-style for statements are also unsupported from Swift 3 onward.




Development and other implementations

Since the language is open-source, there are prospects of it being ported to the web.Some web frameworks have already been developed, such as IBM's Kitura, Perfect and Vapor.

An official "Server APIs" work group has also been started by Apple,with members of the Swift developer community playing a central role.

A second free implementation of Swift that targets Cocoa, Microsoft's Common Language Infrastructure (.NET), and the Java and Android platform exists as part of the Elements Compiler from RemObjects Software.
Share:

No comments:

Post a Comment

Popular Posts

Labels

Followers

Recent Posts

Back to Top