Top Banner
SWIFT WRITING SAFER CODE WITH
22

Writing Safer Code With Swift

Apr 12, 2017

Download

Technology

Gage Herrmann
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Writing Safer Code With Swift

SWIFTWRITING SAFER CODE WITH

Page 2: Writing Safer Code With Swift

WRITING SAFER CODE WITH SWIFT

OVERVIEW▸ What Is Swift?▸ Basic Syntax▸ Why Do We Need Safety?▸ How Can Swift Help?

Page 3: Writing Safer Code With Swift

WHAT IS SWIFT?

Page 4: Writing Safer Code With Swift

WHAT IS SWIFT?

▸ General-purpose, multi-paradigm, programming language▸ Object oriented programming▸ Functional programming▸ Imperative programming

▸ Open source as of December 2015▸ Designed by Apple

A PROGRAMMING LANGUAGE

Page 5: Writing Safer Code With Swift

WHAT IS SWIFT?

NOT JUST APPLE DEVICES▸ Web development

▸ Kitura▸ Official linux binaries▸ Swift For Windows

Page 6: Writing Safer Code With Swift

WHAT IS SWIFT?

BASIC SYNTAXlet hello = "Hello, World!"

Page 7: Writing Safer Code With Swift

WHAT IS SWIFT?

BASIC SYNTAXstruct Hello {

let name: String func message() -> String { return "Hello, \(self.name)!” }}

let helloSFDevs = Hello(name: "SF Devs")

helloSFDevs.message()

Page 8: Writing Safer Code With Swift

WHAT IS SWIFT?

BASIC SYNTAXlet temperatureInFahrenheit = 40

if temperatureInFahrenheit <= 32 { print("It's very cold. Consider wearing a scarf.") } else { print("It's not that cold. Wear a t-shirt.") }

Page 9: Writing Safer Code With Swift

WHAT IS SWIFT?

BASIC SYNTAXfor index in 1...5 { print("\(index) times 5 is \(index * 5)") }

let names = ["Anna", "Alex", "Brian", "Jack"]

for name in names { print("Hello, \(name)!") }

Page 10: Writing Safer Code With Swift

WHAT IS SWIFT?

OTHER BASIC STUFF▸ switch, guard▸ while, repeat-while▸ guard▸ enum, var▸ exception throwing/catching▸ closures (AKA lambdas, anonymous functions)

Page 11: Writing Safer Code With Swift

WHY DO WE NEED SAFETY?

Page 12: Writing Safer Code With Swift

WHY DO WE NEED SAFETY?

TO PROTECT YOURSELF…▸ Spec changes

▸ Brittle code

▸ Bugs

▸ Runtime errors

▸ Null Reference Exceptions

▸ Parsing errors

▸ We want to minimize these these things

Page 13: Writing Safer Code With Swift

WHY DO WE NEED SAFETY?

…FROM YOURSELF

Page 14: Writing Safer Code With Swift

HOW CAN SWIFT HELP?

Page 15: Writing Safer Code With Swift

HOW CAN SWIFT HELP?

▸ let vs. var▸ higher order functions▸ ? operator▸ if let▸ guard

Page 16: Writing Safer Code With Swift

HOW CAN SWIFT HELP?

LET VS. VAR

Page 17: Writing Safer Code With Swift

HOW CAN SWIFT HELP?

HIGHER ORDER FUNCTIONS

Page 18: Writing Safer Code With Swift

HOW CAN SWIFT HELP?

? OPERATOR

Page 19: Writing Safer Code With Swift

OTHER COOL STUFF

PRODUCTION JSON PARSINGguard

let id = json["Id"] as? Int, let lotId = json["LotId"] as? Int, let customerJson = json["Customer"] as? NSDictionary, let vehiclesJson = json["Vehicles"] as? [NSDictionary], let createdBy = json["CreatedBy"] as? String, let createdOnStr = json["CreatedOn"] as? String, let modifiedOnStr = json["ModifiedOn"] as? String else { return Result.Failure("Could not parse purchase json: \(json)")} guard let createdOn = toNSDate(.ISO8601, dateString: createdOnStr) else { return Result.Failure("Could not parse createdOn date: \(createdOnStr)")} guard let modifiedOn = toNSDate(.ISO8601, dateString: modifiedOnStr) else { return Result.Failure("Could not parse modifiedOn date: \(modifiedOnStr)")}

Page 20: Writing Safer Code With Swift

OTHER COOL STUFF

ARGO▸ https://github.com/thoughtbot/Argo

Page 21: Writing Safer Code With Swift

WRITING SAFER CODE WITH SWIFT

LEARN MORE▸ Official Documentation

▸ https://developer.apple.com/library/ios/documentation/Swift

▸ https://www.raywenderlich.com/▸ https://realm.io/news/swift-summit/

Page 22: Writing Safer Code With Swift

WRITING SAFER CODE WITH SWIFT

ANY QUESTIONS?