Dart Client Side Web Programming
Dart Client Side Web Programming
CONTENTS
öö INTRODUCTION
öö GETTING STARTED
öö OPTIONALITY
Swift Essentials öö
öö
öö
CLASSES AND STRUCTS
FUNCTIONAL PROGRAMMING
öö SECURITY IN SWIFT
öö TESTING IN SWIFT
INTRODUCTION This shows several features of Swift: the interpreter, which makes
Swift is the fastest growing programming language today. It is it really easy to experiment with code; the fact that the language is
built on LLVM, which is a key part of both Clang and other popular strongly typed (the function takes an unsigned integer argument,
native runtimes like Rust and Julia. It's easy to learn, compiles and returns an unsigned integer argument); the fact that although
down to native code, and interoperates with existing languages like argument labels are possible for each variable, they can be
Objective-C. Developers are increasingly using this modern, type-safe, omitted using the underscore operator; and that--unlike C-derived
DZO N E .CO M/ RE FCA RDZ
object-oriented/functional programming language for more than languages--the type is on the right of the variable instead of the left
client-side mobile apps. This is because the popular mobile language (i.e. i:UInt and not UInt i). The reason for the type appearing on
moved into an open source project at Swift.org under an Apache the right is that Swift uses type inference where possible so that the
license in December of 2015, just over a year after it was introduced types don't need to be declared:
at Apple's WWDC 2014 developer event. Developers from around
let fiveFactorial = factorial(5)
the world are working to ensure Swift is available for use in other
fiveFactorial: UInt = 120
environments and for efforts spanning Web, Mobile, Cloud and IoT.
As Swift the programming language matures, it can increasingly be
The let is a constant definition, which means it can't be changed. It
the language of choice for end-to-end use cases, bringing its modern
has also been inferred to be a UInt, because that's what the return
advantages to both client-side and server-side deployments.
type of factorial is. Variables also exist, and are declared using
the var keyword:
GETTING STARTED
Although Swift is a compiled language, it comes with an
interpreter swift which can be used to experiment with files and
functions. For example, a factorial function can be defined in the
interpreter as follows:
$ swift #CoverYourApps
Welcome to Swift
func factorial(_ i:UInt) -> UInt{
with IBM Application
if i == 0 {
return 1
Security on Cloud
}else {
return i * factorial(i-1) Start free trial
}
}
factorial(5)
1
Stop hiding from
your security officer.
#CoverYourApps with IBM Security’s leading application security solution.
Introduce a strong, security-first mindset into your DevOps culture, without disrupting
agile practices that your organization relies on to produce quality, on-time software.
Start your free trial today!
IBM Application Security on Cloud enhances web and mobile application security, improves application
security program management and strengthens regulatory compliance. Testing web and mobile applications
prior to deployment helps you identify security risks, generate reports and receive fix recommendations.
©
Copyright IBM Corporation 2018. IBM, the IBM logo and ibm.com are trademarks of International Business Machines Corp., registered in many jurisdictions worldwide.
Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at “Copyright and trademark
information” at ibm.com/legal/copytrades.html.
SWIFT ESSENTIALS
var myFactorial = factorial(6) functions by reference and structs are passed by value (copied).
myFactorial: UInt = 720 They can both have methods associated with them, and the syntax
myFactorial = factorial(7)
for calling methods is identical in both cases. The only significant
print(myFactorial)
difference is that structs can't inherit from other structs (although
5040
they can embed them).
Being a typed language, it is not possible to assign a value to a
variable of the wrong type (or to initialize a constant with the struct Time{
reference types having a nil value by default, variables explicitly init(h24: Bool = true) {
opt-in to be an optional type or not. This also applies to value types self.h24 = h24
like integers, although the overhead is far less than a primitive self.time = Time(hh: h24 ? 13 : 1, mm:15, ss:23)
}
wrapper like Integer in languages such as Java.
}
Optional values can be unpacked using either ?? (which allows for var (if they are mutable) or let (if they are immutable). A function
a default value to be substituted in case of being nil) or forcibly could have been used instead, but then the invocation would have
with !. In most cases these are unnecessary since it's possible been time() in the example at line 3 in that case.
} $R3: Int8 = -1
} Int16(1).negate()
Swift also allows anonymous functions using a { } block and an in TESTING IN SWIFT
to separate functions from the return result: Testing in Swift is facilitated through XCTest, which follows the
similar test paradigms that you will notice in most major languages.
[1,2,3,4,5].map({i in i * 2})
As well as covering the basics of unit testing, the XCTest framework
$R1: [Int] = 5 values {
includes APIs for performance testing as well as user interface testing.
[0] = 2
[1] = 4 Tst classes are always subclasses of XCTestCase which each
[2] = 6
test inside that class must exist in a function prefixed with test.
[3] = 8
Any number of assertions can be added to your tests using the
[4] = 10
XCTAssert function and its variants.
}
[1,2,3,4,5].map() {
i in i * 2 SWIFT QUICK REFERENCE
} OPERATORS
$R2: [Int] = 5 values {
[0] = 2 Operator Function
[1] = 4
[2] = 6 + Addition operator.
[3] = 8
[4] = 10
Subtraction operator/unary minus (negative
DZO N E .CO M/ RE FCA RDZ
} -
value) operator.
It's also possible to iterate over loops using a for ... in with an
optional where clause: * Multiplication operator.
• Configure App Transport Security to provide secure default < Less than.
behavior
• Validate incoming and outgoing URL handler calls <= Less than or equal to.
• Avoid SQL injection attacks by using prepared statements in >= Greater than or equal to.
cases where SQLite is being used
// Single-line comment.
An unordered collection of data of one
Set
hashable type.
/* Begin multiline comment.
Dictionary A collection of key-value pairs.
*/ End multiline comment.
CONTROL FLOW
DZO N E .CO M/ RE FCA RDZ
LOOPS
VARIABLES
let somevar:
Executes a block, then String =
let somevar: repeat-
Defines an immutable
String = repeats as long as the "something"
while
(constant) variable. Variable let somevar =
let "something" while condition is true.
may be explicitly typed, or "different"
let somevar =
Swift will infer variable type. "different"
Name Type
for element
Executes a block for in
Bool Boolean values true and false. for-in each element in an someArray {
CONDITIONALS
if somevar <
1 {
print("Less
CONTROL TRANSFER
Executes a block than one.")
else once an if statement } else {
print("Not Operator Definition Example
evaluates as false
less than
one.")
} switch
someSwitch {
case 3:
1 {
else if previous conditional print(“One.”)
print("Exactly
fallthrough
evaluates as false, one.")
default:
then executing a } else {
print(“Done.”)
block if its expression print("Greater
than }
is true
one.")
}
for i in 1...10
{
Evaluates conditions if i == 3 {
switch someSwitch { Ends the current
based on case and continue
case 1:
executes a block for iteration of a loop } else {
print("One") continue
the first matching and begins the next print("\(i) is
case 2:
iteration. not
switch case; if no matching print ("Two")
default: three.")
case exists, the switch
print("Not one or }
statement executes
two.") }
the block for the }
default case
Ends execution of
switch someSwitch { a running loop and for i in 1...10
continues running {
case 0:
code immediately if i == 3 {
print("Zero")
break
case 1: after the loop. Also
Evaluates additional } else {
print ("One") break used to pass over
print("\(i) is
where conditionals in switch case let x where
unwanted cases in less
cases. x.isPrime():|
switch statements (as than three.")
print("Prime")
default:
switch cases must be }
exhaustive and must }
print("Not prime")
} not be empty).
• Xcode: Apple’s IDE for developing software for Mac OS, iOS, • Swift Language
WatchOS and tvOS. swiftlang.eu/community
developer.apple.com/xcode/downloads • Swift Meetups
• RunSwift: Browser-based “code bin” which lets you test your code. meetup.com/find/?keywords=Swift&radius=Infinity
runswiftlang.com
MISC
• Apple Swift Resources
developer.apple.com/swift/resources
LIBRARIES
• Swift Toolbox • Swift Package Manager
swifttoolbox.io swift.org/package-manager
• CocoaPods: Dependency manager for Swift projects. • Github’s Trending Swift Repositories: Track trending Swift
github.com/felixgr/secure-ios-app-dev
• Wolg/awesome-swift: A curated list of Swift frameworks,
• Ponemon Institute's Mobile and IoT Application Security
libraries and software. Testing Study:
github.com/Wolg/awesome-swift securityintelligence.com/10-key-findings-from-the-ponemon-
institutes-mobile-iot-application-security-testing-study
James has been a Zone Leader at DZone since 2008, and has written many Refcardz, as well as a book on Backbone.js.
DZone, Inc.
DZone communities deliver over 6 million pages each 150 Preston Executive Dr. Cary, NC 27513
month to more than 3.3 million software developers, 888.678.0399 919.678.0300
architects and decision makers. DZone offers something for
Copyright © 2018 DZone, Inc. All rights reserved. No part of this publication
everyone, including news, tutorials, cheat sheets, research
may be reproduced, stored in a retrieval system, or transmitted, in any form
guides, feature articles, source code and more. "DZone is a or by means electronic, mechanical, photocopying, or otherwise, without
developer’s dream," says PC Magazine. prior written permission of the publisher.