Você está na página 1de 2

1

A Swift Case Study


Joaquin Jacinto Luis Ligunas

I. A BSTRACT that came with Objective-C. This resulted in the rise of the
Swift is a general-purpose programming that is used for first version of Swift. It was seen as a modern alternative
developing apps for Apple’s various operating systems. It was to Objective-C, but it was made compatible to it to enable
developed as the programming language to replace Objective- a transition to Swift in projects that are already built with
C as the primary language for development. Swift is built to Objective-C. It proved to be significantly faster than its ances-
be safe, fast, and expressive. It makes use of object oriented tor and came with modern features. It was released alongside
programming and protocol oriented programming. Of the a new version of XCode and the then-new Playground feature
many notable features, this paper will focus on optionals, which allowed Swift-interested developers to try and test out
extensions, protocols, closures, and overflows. the new languages. [7], [6]
By the time Swift hit version 2.0, it became an open source
II. I NTRODUCTION programming language, much to the liking of the open source
community. This version came with additional safety features
Swift is a programming language developed by Apple Inc.
to make error handling more secure and, more importantly,
It was first introduced to the publicly during Apple’s 2014
protocols, one of the most prominent features, if not the most,
Worldwide Developers Conference. Prior to the development
of Swift programming. It was in this version that Swift became
of Swift, Objective-C was the only option for Apple software
a protocol-oriented programming language. Apple Inc. has
product development. On the day of release, a 550-page
even said that ¨[A]t its heart, Swift is protocol-oriented.” [6],
language guide, ”The Swift Programming Language,” was
[3]
released in the iBooks store[7]. Swift is a general-purpose
Version 3.0 came with the Swift Package Manager, a tool for
programming language but is more widely known for its
managing the distribution of Swift code, and the simplification
applications in macOS, iOS, watchOS, and tvOS[2].
of function names. Version 4.0 with improvements in the
use of Strings and Collections. Version 5.0 comes with the
III. L ITERARY R EVIEW r̈eimplementation of String, enforcement of exclusive access
In July 2010, Chris Lattner started working on the develop- to memory during runtime, new data types, and support for
ment of the Swift programming language. At first, not many dynamically callable types.̈
people knew about it, but more and more people started to
contribute to it in 2011. Two years later, Swift became a major
focus for the Apple Developer Tools group.[4] IV. D ISCUSSION OF N OTABLE /A DVANCED F EATURES
Swift is the product of inspiration from many different
Swift has tons of notable features. Swift documentation
programming languages. Its goal is to be the best language
explicitly refers to the following: closures unified with function
for a wide array of applications, including but not limited to
pointers, tuples and multiple return values, generics, fast and
”systems programming, ... mobile and desktop apps, ... [and]
concise iteration over a range or collection, structs that support
cloud services”. More importantly, Swift is designed to help
methods, extensions, and protocols, functional programming
the developer write and maintain programs correctly.[4], [1]
patterns, powerful error handling built-in, and advanced con-
The most obvious way to write Swift code must be safe, fast,
trol flow[1]. This paper, however, would focus on optionals,
and expressive. With regards to safety, ambiguity becomes a
protocols, extensions, closures, and overflows.
problem when trying to understand one’s code. This is the
reason that Swift may feel a little strict: clarity of code saves
the developer time understanding and tracking down bugs in
A. Optionals
the long run. With regards to speed, Swift was originally
intended to be a replacement for C-based language. It is In Swift, objects are not allowed to have nil values for safety
comparable to these languages for certain operations and is reasons. It prevents cases such as null pointer errors that are
significantly faster in operations like complex object sort and commonly found in other programming languages. In order
RC4 encryption. With regards to expressiveness, Swift aims to to allow developers to set nil values for objects, the object
have equip developers with modern features and syntax that is must be an optional. Optionals are used for when a value
easy to use. It is perhaps these reasons that Swift was voted may be absent and is denoted by the question mark symbol.
the Most Loved Programming Language in the 2015 Stack The optional class is often described to be similar to wrapper
Overflow Developer Survey, when it was only publicly known classes. To be able to access the value stored in an optional,
for one year. [7], [1], [5] it needs to be first unwrapped. There are multiple ways
Objective-C was the primary language used for Apple to unwrap an optional such as forced unwrapping, optional
product development, but Apple disliked the ”baggage of C” binding, and implicitly unwrapping.
2

1) Forced Unwrapping: Force unwrapping forces the op- and unnamed. Closures can emulate the idea of a callback
tional to return the value that it is storing regardless of what function. For example, a function can ask for a closure as one
it is. It is denoted by the exclamation point symbol. This is of the required parameters, which would be called at some
generally used when the developer is certain that the optional point during the execution of this function. The part of the
will have a value. Despite this being available, it is generally code which calls this function can define the functionality of
seen as a bad practice because it will cause an error if the this closure. In essence, closures allow the passing around of
unwrapped value is nil. functionality as if it were a variable.
2) Optional Binding: The next way to unwrap an optional
is through optional binding. Optional binding generally uses E. Overflows
if and while statements to temporarily bind the stored value
C-based languages generally allow the data types that
in a variable. It is used to find out if the optional contains a
represent numbers to overflow. For example, adding 1 to a
value. If the optional contains a value, it will proceed with the
32-bit unsigned integer with value 232 − 1 would return a
succeeding lines of code, but if the value is nil, it will skip
value of 0. This is due to the wrap around nature of integer
the code within the if or while statements. This is the more
overflow. Swift, by default, does not allow this. If a number
commonly used practice as compared to force unwrapping
is added or subtracted from another that is not within the
since it prevents any chances of nil values being stored into
range accepted by that data type, an error occurs at runtime.
a non-optional value.
This is an advantage of Swift because it prevents security
threats that make use of the vulnerability of integer overflow.
3) Implicit Unwrapping: Lastly, there is the implicitly
This is a disadvantage, however, because an error at runtime
unwrapped optionals. Like force unwrapping, this uses the
would be obvious to the end user if not handled. Overflowing,
exclamation point symbol. It is similar to force unwrapping,
however, can still be allowed (by prepending an ampersand
but implicitly unwrapped optionals is used when an optional
to the arithmetic operators) if the programmer desires that
will always have a value. As with force unwrapping, it will
functionality.
cause an error if the optional has a nil value. The difference
between the two is that implicitly unwrapped optionals are
used for value that will always have a value while force V. C ONTEMPORARY A PPLICATIONS
unwrapping does not guarantee that the optional will have a As mentioned in the Introduction, Swift is a general-purpose
value. Implicitly unwrapped optionals removes the need to programming language. In essence, it can be used for any
check and unwrap the optional value. It is primarily used purpose that any programming language has ever been used.
during class initialization. It is like a normal optional, but it Also mentioned earlier is that, however, Swift is primarily
can be used like a non-optional value. used today for the development of Apple software products
on macOS, iOS, watchOS, and tvOS.
B. Protocols
VI. C ONCLUSIONS
A protocol is similar to the concept of an interface in Java.
It provides a blueprint for classes, structures, or enumerations R EFERENCES
that would adopt the said protocol. For example, if a protocol [1] Apple Inc. About swift.
[2] Apple Inc. Swift.
contained a property named name and a function named get- [3] Andrew Jaffee. Protocol oriented programming in swift: An introduction.
Name, all classes, structures, and enumerations that conform to [4] Chris Lattner. Chris lattner’s homepage.
this protocol must have a property named name and a function [5] Stack Overflow. 2015 developer survey.
[6] Mindfire Solutions. The evolution of swift.
with the exact same method signature as that of getName. [7] the unofficial AppleKeynotes channel. Apple wwdc 2014 - swift intro-
duction.
C. Extensions
Extensions are quite connected to protocols. Extensions
allow the addition of functionality into existing classes, struc-
tures, enumerations, and protocols without modifying the
contents of the said data types. For example, if a developer
decided to add the functionality of gravity on a Person
class, an extension of Person could be made with functions
and properties necessary for the functionality of gravity. An
addition of an extension even allows for a class, structure, or
enumeration to conform to a protocol.

D. Closures
Closures are blocks of code that can be passed around. They
are very similar to lambdas in programming languages like
Python. A closure can take one of three forms: global, nested,

Você também pode gostar