DERIVED UNITS Combining measurements to describe physical properties.
Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer,...
Transcript of Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer,...
![Page 1: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/1.jpg)
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
Using locale-appropriate measurements in your app
App Frameworks #WWDC16
Session 238
Measurements and Units
Daphne Larose Software Engineer, Foundation
![Page 2: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/2.jpg)
Introduction
Measurements pop up all the timeMeasurements should be in preferred unitsNew API makes it easy to do the right thing for everyone
![Page 3: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/3.jpg)
Jammin’ in the Streetz
![Page 4: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/4.jpg)
![Page 5: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/5.jpg)
Goals
FunLots of emojisAvailable in multiple countries
![Page 6: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/6.jpg)
Key Elements of the Game
Jam sessionsTracks• Total time• Distance traveled• Number of dance movements performed• Rate of travel
![Page 7: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/7.jpg)
Key Elements of the Game
Jam sessionsTracks• Total time• Distance traveled• Number of dance movements performed• Rate of travel
![Page 8: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/8.jpg)
!
![Page 9: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/9.jpg)
!
![Page 10: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/10.jpg)
!
![Page 11: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/11.jpg)
!5
![Page 12: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/12.jpg)
!5
![Page 13: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/13.jpg)
!feet5
![Page 14: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/14.jpg)
Creating Measurements Easily
![Page 15: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/15.jpg)
NEW// Measurement
public struct Measurement<UnitType : Unit> : Comparable, Equatable {
![Page 16: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/16.jpg)
NEW// Measurement
public struct Measurement<UnitType : Unit> : Comparable, Equatable {
public let unit: UnitType
![Page 17: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/17.jpg)
NEW// Measurement
public struct Measurement<UnitType : Unit> : Comparable, Equatable {
public let unit: UnitType
public var value: Double
![Page 18: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/18.jpg)
NEW// Measurement
public struct Measurement<UnitType : Unit> : Comparable, Equatable {
public let unit: UnitType
public var value: Double
public init(value: Double, unit: UnitType)
}
![Page 19: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/19.jpg)
! Distance to go Distance traveled
![Page 20: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/20.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
![Page 21: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/21.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
![Page 22: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/22.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
![Page 23: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/23.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
value: 11, unit: .feet
![Page 24: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/24.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
let tripleDistance = 3 * distanceToGo
![Page 25: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/25.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
let tripleDistance = 3 * distanceToGo
value: 18, unit: .feet
![Page 26: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/26.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
let tripleDistance = 3 * distanceToGo
let halfDistance = distanceToGo / 2
![Page 27: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/27.jpg)
// Calculations With Measurements
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
let tripleDistance = 3 * distanceToGo
let halfDistance = distanceToGo / 2
value: 3, unit: .feet
![Page 28: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/28.jpg)
Properties of a Unit
![Page 29: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/29.jpg)
Properties of a Unit
Symbol “ft”
![Page 30: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/30.jpg)
Properties of a Unit
Symbol “ft”
Dimension “Foot is a unit of length”
![Page 31: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/31.jpg)
Properties of a Unit
Symbol “ft”
Dimension “Foot is a unit of length”
Equivalence 1ft = 0.348m
![Page 32: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/32.jpg)
// Unit
public class Unit : NSObject, NSCopying {
public let symbol : String
public init(symbol: String)
}
NEW
![Page 33: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/33.jpg)
Dimension
![Page 34: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/34.jpg)
Dimension
Categories of units
![Page 35: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/35.jpg)
Dimension
Categories of unitsExpressed with different units• Length: km, m, ft, mi, etc.
![Page 36: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/36.jpg)
Dimension
Categories of unitsExpressed with different units• Length: km, m, ft, mi, etc.
Always has a base unitlet meter = UnitLength.baseUnit
![Page 37: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/37.jpg)
Dimension
Categories of unitsExpressed with different units• Length: km, m, ft, mi, etc.
Always has a base unitlet meter = UnitLength.baseUnit
Can perform conversions• km ⇆ ft, m ⇆ mi
![Page 38: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/38.jpg)
// Dimension
public class Dimension : Unit, NSCoding {
NEW
![Page 39: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/39.jpg)
// Dimension
public class Dimension : Unit, NSCoding {
public var converter : UnitConverter { get }
NEW
![Page 40: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/40.jpg)
// Dimension
public class Dimension : Unit, NSCoding {
public var converter : UnitConverter { get }
public init(symbol: String, converter: UnitConverter)
NEW
![Page 41: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/41.jpg)
// Dimension
public class Dimension : Unit, NSCoding {
public var converter : UnitConverter { get }
public init(symbol: String, converter: UnitConverter)
public class var baseUnit : Dimension
NEW
}
![Page 42: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/42.jpg)
Abstract unitsDimension
Instances as units
![Page 43: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/43.jpg)
Abstract unitsDimension
Instances as unitsSingletons for most common units
![Page 44: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/44.jpg)
Abstract unitsDimension
Instances as unitsSingletons for most common unitsInternational System of Units
![Page 45: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/45.jpg)
public class UnitLength : Dimension { /* Base unit - meters */ public class var kilometers: UnitLength { get } public class var meters: UnitLength { get } public class var feet: UnitLength { get } public class var miles: UnitLength { get }
... }
Abstract unitsDimension
Instances as unitsSingletons for most common unitsInternational System of Units
![Page 46: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/46.jpg)
Provided subclassesDimension
UnitAcceleration UnitElectricCurrent UnitIlluminance
UnitAngle UnitElectricPotentialDifference UnitMass
UnitArea UnitElectricResistance UnitPower
UnitConcentrationMass UnitEnergy UnitPressure
UnitDispersion UnitFrequency UnitSpeed
UnitDuration UnitFuelEfficiency UnitTemperature
UnitElectricCharge UnitLength UnitVolume
NEW
![Page 47: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/47.jpg)
// Implicit Conversion
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
let totalDistance = distanceTraveled + distanceToGo
value: 11, unit: .feet
![Page 48: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/48.jpg)
// Implicit Conversion
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
// let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
// let totalDistance = distanceTraveled + distanceToGo
![Page 49: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/49.jpg)
// Implicit Conversion
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
// let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
// let totalDistance = distanceTraveled + distanceToGo
let distanceToGo = Measurement(value: 6, unit: UnitLength.kilometers)
![Page 50: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/50.jpg)
// Implicit Conversion
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
// let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
// let totalDistance = distanceTraveled + distanceToGo
let distanceToGo = Measurement(value: 6, unit: UnitLength.kilometers)
let totalDistance = distanceTraveled + distanceToGo
![Page 51: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/51.jpg)
// Implicit Conversion
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
// let distanceToGo = Measurement(value: 6, unit: UnitLength.feet)
// let totalDistance = distanceTraveled + distanceToGo
let distanceToGo = Measurement(value: 6, unit: UnitLength.kilometers)
let totalDistance = distanceTraveled + distanceToGo
value: 6001.524, unit: .meters
![Page 52: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/52.jpg)
// Comparison Operators
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.kilometers)
var distanceMarker : String
if (distanceTraveled > distanceToGo) {
distanceMarker = "Almost there!"
} else if (distanceTraveled < distanceToGo) {
distanceMarker = "Barely started!"
} else {
distanceMarker = "Halfway!"
}
![Page 53: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/53.jpg)
// Comparison Operators
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 6, unit: UnitLength.kilometers)
var distanceMarker : String
if (distanceTraveled > distanceToGo) {
distanceMarker = "Almost there!"
} else if (distanceTraveled < distanceToGo) {
distanceMarker = "Barely started!"
} else {
distanceMarker = "Halfway!"
}
![Page 54: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/54.jpg)
// Comparison Operators
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 5, unit: UnitLength.kilometers)
var distanceMarker : String
if (distanceTraveled > distanceToGo) {
distanceMarker = "Almost there!"
} else if (distanceTraveled < distanceToGo) {
distanceMarker = "Barely started!"
} else {
distanceMarker = "Halfway!"
}
print(distanceMarker)
![Page 55: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/55.jpg)
// Comparison Operators
let distanceTraveled = Measurement(value: 5, unit: UnitLength.feet)
let distanceToGo = Measurement(value: 5, unit: UnitLength.kilometers)
var distanceMarker : String
if (distanceTraveled > distanceToGo) {
distanceMarker = "Almost there!"
} else if (distanceTraveled < distanceToGo) {
distanceMarker = "Barely started!"
} else {
distanceMarker = "Halfway!"
}
print(distanceMarker)
“Barely started!”
![Page 56: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/56.jpg)
Unit Definition
![Page 57: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/57.jpg)
Unit Definition
In terms of base unit
![Page 58: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/58.jpg)
Unit Definition
In terms of base unitMethods to define conversion
![Page 59: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/59.jpg)
Unit Definition
In terms of base unitMethods to define conversionConvert within dimension
![Page 60: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/60.jpg)
Creating Units
![Page 61: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/61.jpg)
Creating Units
Only define custom unitsConversion handled implicitly
![Page 62: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/62.jpg)
// Create Custom Units on the Fly
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
![Page 63: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/63.jpg)
// Create Custom Units on the Fly
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
![Page 64: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/64.jpg)
Conversion NEW
![Page 65: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/65.jpg)
Conversion
baseUnit unit
NEW
![Page 66: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/66.jpg)
Conversion
baseUnit unitUnitConverter• baseUnitValue(fromValue value:)• value(fromBaseUnitValue baseUnitValue:)
NEW
![Page 67: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/67.jpg)
Conversion
baseUnit unitUnitConverter• baseUnitValue(fromValue value:)• value(fromBaseUnitValue baseUnitValue:)
UnitConverterLinear• baseUnitValue = value * coefficient + constant• value = (baseUnitValue - constant)/coefficient
NEW
![Page 68: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/68.jpg)
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
![Page 69: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/69.jpg)
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
baseUnitValue = 30 * jamzValue
jamzValue = baseUnitValue/30
![Page 70: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/70.jpg)
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
![Page 71: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/71.jpg)
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
baseUnitValue = 0.75 * hopzValue
hopzValue = baseUnitValue/0.75
![Page 72: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/72.jpg)
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let glidez = UnitLength(symbol: “glidez", converter: UnitConverterLinear(coefficient: 1.5))
![Page 73: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/73.jpg)
// “Jammin’ in the Streetz” Game - Custom Units
let jamz = UnitDuration(symbol: “jamz", converter: UnitConverterLinear(coefficient: 30))
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let glidez = UnitLength(symbol: “glidez", converter: UnitConverterLinear(coefficient: 1.5))
baseUnitValue = 1.5 * glidezValue
glidezValue = baseUnitValue/1.5
![Page 74: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/74.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
![Page 75: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/75.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
![Page 76: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/76.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
1 💪 = 1 💪
![Page 77: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/77.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",
converter: UnitConverterLinear(coefficient: 4))
![Page 78: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/78.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",
converter: UnitConverterLinear(coefficient: 4))
1 🤖 = 4 💪
![Page 79: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/79.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",
converter: UnitConverterLinear(coefficient: 4))
static let cabbagePatch = UnitDanceMove(symbol: "👼",
converter: UnitConverterLinear(coefficient: 3))
![Page 80: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/80.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",
converter: UnitConverterLinear(coefficient: 4))
static let cabbagePatch = UnitDanceMove(symbol: "👼",
converter: UnitConverterLinear(coefficient: 3))
1 👼 = 3 💪
![Page 81: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/81.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",
converter: UnitConverterLinear(coefficient: 4))
static let cabbagePatch = UnitDanceMove(symbol: "👼",
converter: UnitConverterLinear(coefficient: 3))
static let jazzHands = UnitDanceMove(symbol: "👐",
converter: UnitConverterLinear(coefficient: 2))
}
![Page 82: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/82.jpg)
// “Jammin’ in the Streetz” Game - Custom Dimension
public class UnitDanceMove : Dimension {
static let wackyArmMovements = UnitDanceMove(symbol: "💪",
converter: UnitConverterLinear(coefficient: 1))
static let robot = UnitDanceMove(symbol: "🤖",
converter: UnitConverterLinear(coefficient: 4))
static let cabbagePatch = UnitDanceMove(symbol: "👼",
converter: UnitConverterLinear(coefficient: 3))
static let jazzHands = UnitDanceMove(symbol: "👐",
converter: UnitConverterLinear(coefficient: 2))
}1 👐 = 2 💪
![Page 83: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/83.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
![Page 84: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/84.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
![Page 85: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/85.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength> .hopz
![Page 86: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/86.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
![Page 87: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/87.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration> .jamz
![Page 88: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/88.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
![Page 89: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/89.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove> .robot
![Page 90: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/90.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
public var danceRate : Measurement<UnitSpeed> {
![Page 91: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/91.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
public var danceRate : Measurement<UnitSpeed> { .metersPerSecond
![Page 92: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/92.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
public var danceRate : Measurement<UnitSpeed> {
let stepsInMeters = stepsTaken.converted(to: .meters)
![Page 93: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/93.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
public var danceRate : Measurement<UnitSpeed> {
let stepsInMeters = stepsTaken.converted(to: .meters)
let jamTimeInSeconds = jamTime.converted(to: .seconds)
![Page 94: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/94.jpg)
// “Jammin’ in the Streetz” Game - Jam Session
public struct JamSession {
public var stepsTaken : Measurement<UnitLength>
public var jamTime : Measurement<UnitDuration>
public var danceMoves : Measurement<UnitDanceMove>
public var danceRate : Measurement<UnitSpeed> {
let stepsInMeters = stepsTaken.converted(to: .meters)
let jamTimeInSeconds = jamTime.converted(to: .seconds)
return Measurement(value: (stepsInMeters.value / jamTimeInSeconds.value),
unit: .metersPerSecond)
}
}
![Page 95: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/95.jpg)
Formatting Measurements
![Page 96: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/96.jpg)
!5 km
![Page 97: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/97.jpg)
Formatting Is Hard
Country Expected String
![Page 98: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/98.jpg)
Formatting Is Hard
Country Expected String
Canada “5 km”
![Page 99: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/99.jpg)
Formatting Is Hard
Country Expected String
Canada “5 km”
China “5 “
![Page 100: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/100.jpg)
Formatting Is Hard
Country Expected String
Canada “5 km”
China “5 “
Egypt “٥ كم“
![Page 101: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/101.jpg)
Formatting Is Hard
Country Expected String
Canada “5 km”
China “5 “
Egypt “٥ كم“
United States “3.1 mi”
![Page 102: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/102.jpg)
Let Us Do the Work
![Page 103: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/103.jpg)
Let Us Do the Work
New formatter
![Page 104: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/104.jpg)
Let Us Do the Work
New formatterMeasurements and units
![Page 105: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/105.jpg)
Let Us Do the Work
New formatterMeasurements and unitsLocale-aware formatting
![Page 106: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/106.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
NEW
![Page 107: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/107.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
NEW
![Page 108: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/108.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
public var unitStyle: Formatter.UnitStyle
NEW
![Page 109: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/109.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
public var unitStyle: Formatter.UnitStyle
@NSCopying public var locale: Locale!
NEW
![Page 110: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/110.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
public var unitStyle: Formatter.UnitStyle
@NSCopying public var locale: Locale!
@NSCopying public var numberFormatter: NumberFormatter!
NEW
![Page 111: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/111.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
public var unitStyle: Formatter.UnitStyle
@NSCopying public var locale: Locale!
@NSCopying public var numberFormatter: NumberFormatter!
public func string(from measurement: Measurement<Unit>) -> String
NEW
![Page 112: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/112.jpg)
// MeasurementFormatter
public class MeasurementFormatter : Formatter, NSSecureCoding {
public var unitOptions: MeasurementFormatter.UnitOptions
public var unitStyle: Formatter.UnitStyle
@NSCopying public var locale: Locale!
@NSCopying public var numberFormatter: NumberFormatter!
public func string(from measurement: Measurement<Unit>) -> String
public func string(from unit: Unit) -> String
NEW
}
![Page 113: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/113.jpg)
Unit Options
![Page 114: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/114.jpg)
Unit Options
Formats preferred unit of locale by defaultTakes purpose into account
![Page 115: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/115.jpg)
Unit Options
UnitOptions Measurement Locale Example String
![Page 116: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/116.jpg)
Unit Options
UnitOptions Measurement Locale Example String
.providedUnit value: 5, unit: .kilometers “en_US” “5 km”
![Page 117: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/117.jpg)
Unit Options
UnitOptions Measurement Locale Example String
.providedUnit value: 5, unit: .kilometers “en_US” “5 km”
.naturalScale value: 1000, unit: .meters “fr_FR” “1 km”
![Page 118: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/118.jpg)
Unit Options
UnitOptions Measurement Locale Example String
.providedUnit value: 5, unit: .kilometers “en_US” “5 km”
.naturalScale value: 1000, unit: .meters “fr_FR” “1 km”
.temperatureWithoutUnit value: 90, unit: .fahrenheit “en_US” “90°“
![Page 119: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/119.jpg)
// “Jammin’ in the Streetz” Game - Formatting Units
let formatter = MeasurementFormatter()
![Page 120: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/120.jpg)
// “Jammin’ in the Streetz” Game - Formatting Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
![Page 121: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/121.jpg)
// “Jammin’ in the Streetz” Game - Formatting Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
![Page 122: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/122.jpg)
// “Jammin’ in the Streetz” Game - Formatting Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
“3.1 mi”
![Page 123: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/123.jpg)
// “Jammin’ in the Streetz” Game - Formatting Custom Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
![Page 124: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/124.jpg)
// “Jammin’ in the Streetz” Game - Formatting Custom Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let hopzDistance = Measurement(value: 1000, unit: hopz)
![Page 125: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/125.jpg)
// “Jammin’ in the Streetz” Game - Formatting Custom Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
![Page 126: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/126.jpg)
// “Jammin’ in the Streetz” Game - Formatting Custom Units
let formatter = MeasurementFormatter()
let distance = Measurement(value: 5, unit: UnitLength.kilometers)
let result = formatter.string(from: distance)
let hopz = UnitLength(symbol: “hopz", converter: UnitConverterLinear(coefficient: 0.75))
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
“0.466 mi”
![Page 127: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/127.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
![Page 128: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/128.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hopz)
![Page 129: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/129.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
![Page 130: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/130.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
“1000 hopz”
![Page 131: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/131.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
let robotDance = Measurement(value: 30, unit: UnitDanceMove.robot)
![Page 132: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/132.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
let robotDance = Measurement(value: 30, unit: UnitDanceMove.robot)
let resultingRobotDances = formatter.string(from: robotDance)
![Page 133: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/133.jpg)
// “Jammin’ in the Streetz” Game - Formatting Provided Unit
formatter.unitOptions = [.providedUnit]
let hopzDistance = Measurement(value: 1000, unit: hopz)
let resultingHopz = formatter.string(from: hopzDistance)
let robotDance = Measurement(value: 30, unit: UnitDanceMove.robot)
let resultingRobotDances = formatter.string(from: robotDance)
“30 🤖”
![Page 134: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/134.jpg)
DemoDisplaying locale-aware measurements in your app
Peter Hosey Software Engineer, Foundation
![Page 135: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/135.jpg)
Summary
![Page 136: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/136.jpg)
Summary
New model objects
![Page 137: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/137.jpg)
Summary
New model objectsMeasurementFormatter for formatting
![Page 138: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/138.jpg)
Summary
New model objectsMeasurementFormatter for formattingPowerful localization for free
![Page 139: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/139.jpg)
More Information
https://developer.apple.com/wwdc16/238
![Page 140: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/140.jpg)
Related Sessions
Internationalization Best Practices Mission Tuesday 9:00AM
What’s New in Cocoa Nob Hill Tuesday 11:00AM
What’s New in Foundation for Swift Mission Tuesday 4:00PM
![Page 141: Measurements and Units - Apple Inc. · Measurements and Units Daphne Larose Software Engineer, Foundation. Introduction Measurements pop up all the time Measurements should be in](https://reader034.fdocuments.us/reader034/viewer/2022050602/5fa9d3152feb0d5e682ece0a/html5/thumbnails/141.jpg)