SlideShare uma empresa Scribd logo
1 de 100
Baixar para ler offline
CGFloat
2016/9/28 Hirohito Kato
• try! Swift 2016 

Swift (@RGfox)

• ( ) 

• CGFloat
• Swift
•
•
CGFloat
• Core Graphics
•
• SpriteKit/SceneKit UIKit
•
CGFloat
• double / float typedef
• CPU
• double
• CGFloat
• NSNumber CGFloat
• double
• iOS iPhone5 or iPhone5S 

…
double
20XX Swift
CGFloat
CGFloat ≠ Float
CGFloat ≠ Double
CGFloat “is” CGFloat
let value1: CGFloat = 240 // OK
let value1: CGFloat = 240 // OK
let value2: CGFloat = 80.0 // OK
let a = CGFloat(10)
let a = CGFloat(10)
let b = 30
let a = CGFloat(10)
let b = 30
let value = a + b // ERROR!
let a = CGFloat(10)
let b = 30
let value = a + b // ERROR!
let value = a + CGFloat(b) // OK
let pos = CGPoint(x: 160, y: 80.0)
let pos = CGPoint(x: 160, y: 80.0)
// OK
let pos = CGPoint(x: 160, y: 80.0)
// OK
let a = 160
let pos = CGPoint(x: 160, y: 80.0)
// OK
let a = 160
let pos = CGPoint(x: a, y: 80.0)
let pos = CGPoint(x: 160, y: 80.0)
// OK
let a = 160
let pos = CGPoint(x: a, y: 80.0)
// ERROR!
let pos = CGPoint(x: 160, y: 80.0)
// OK
let a = 160
let pos = CGPoint(x: a, y: 80.0)
// ERROR!
let x = 32
let x = 32
let y = 120.0
let x = 32
let y = 120.0
let w = Float(44.0)
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: x,
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: x,
y: y,
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: x,
y: y,
width: w,
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: x,
y: y,
width: w,
height: h)
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: x,
y: y,
width: w,
height: h)
// ERROR!
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: x,
y: y,
width: w,
height: h)
// ERROR!
let x = 32
let x = 32
let y = 120.0
let x = 32
let y = 120.0
let w = Float(44.0)
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: CGFloat(x),
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: CGFloat(x),
y: CGFloat(y),
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: CGFloat(x),
y: CGFloat(y),
width: CGFloat(w),
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: CGFloat(x),
y: CGFloat(y),
width: CGFloat(w),
height: h)
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: CGFloat(x),
y: CGFloat(y),
width: CGFloat(w),
height: h)
// OK
let x = 32
let y = 120.0
let w = Float(44.0)
let h = CGFloat(24)
view.rect = CGRect(x: CGFloat(x),
y: CGFloat(y),
width: CGFloat(w),
height: h)
// OK
•
•
•
• Swift Objective-C
Float/Double CGFloat
•
•
• /
• …
CGFloat
Float Double =
…
extension Double {
var cgf:CGFloat { return CGFloat(self) }
}
let y = 1 + 10.0.cgf // OK, yay!
extension Double {
var cgf:CGFloat { return CGFloat(self) }
}
extension Int {
var cgf:CGFloat { return CGFloat(self) }
}
extension Float {
var cgf:CGFloat { return CGFloat(self) }
}
…
extension Double {
var c:CGFloat { return CGFloat(self) }
}
extension Int {
var c:CGFloat { return CGFloat(self) }
}
extension Float {
var c:CGFloat { return CGFloat(self) }
}
extension Float80 {
var c:CGFloat { return CGFloat(self) }
}
extension UInt {
var c:CGFloat { return CGFloat(self) }
}
extension UInt8 {
var c:CGFloat { return CGFloat(self) }
}
extension UInt16 {
var c:CGFloat { return CGFloat(self) }
}
extension UInt32 {
var c:CGFloat { return CGFloat(self) }
}
extension UInt64 {
var c:CGFloat { return CGFloat(self) }
}
extension Int8 {
var c:CGFloat { return CGFloat(self) }
}
extension Int16 {
var c:CGFloat { return CGFloat(self) }
}
extension Int32 {
var c:CGFloat { return CGFloat(self) }
}
extension Int64 {
var c:CGFloat { return CGFloat(self) }
}
extension UInt64 {
var c:CGFloat { return CGFloat(self) }
}
extension MyNumberType {
var c:CGFloat { return CGFloat(self) }
}
extension MyNumberTypeB {
var c:CGFloat { return CGFloat(self) }
}
:
…
…
•
• …
• 

Swift
OK
let a: Float = 190.0
let b: Int = 3
let value = CGFloat(a) + CGFloat(b)
CGFloat
public struct CGFloat {
public init()
public init(_ value: Float)
public init(_ value: Double)
public init(_ value: Float80)
public init(_ value: CGFloat)
public init(_ value: UInt8)
public init(_ value: Int8)
:
• CGFloat
• Int
…
Protocol
Protocol =
• API
•
• Java/C# Interface Ruby mix-in C++
• ( ) protocol
• extension
• /
protocol
extension Int: MyCustomProtocol {}
Swift protocol
Swift protocol
• init
•
protocol CGFConvertible {
init (_ value: Int)
init (_ value: Float)
init (_ value: Double)
init (_ value: CGFloat)
}
//
extension Int: CGFConvertible {}
extension Float: CGFConvertible {}
extension Double: CGFConvertible {}
extension CGFloat: CGFConvertible {}
Generic
•
CGFConvertible 

T
•
•
… Swift 

extension CGFConvertible {
fileprivate func convert
<T: CGFConvertible>() -> T {
return T(self)
}
}
func checkType<T>(_ x: T) {
switch x {
case _ as CGFloat: print("(x) is a CGFloat")
case _ as Float: print("(x) is a Float")
case _ as Int: print("(x) is a Int")
case _ as Double: print("(x) is a Double")
default: print("(x) is unknown..")
}
}
Swift Workaround
RTTI
extension CGFConvertible {
fileprivate func convert<T: CGFConvertible>() -> T {
switch self {
case let x as CGFloat: return T(x)
case let x as Float: return T(x)
case let x as Int: return T(x)
case let x as Double: return T(x)
default:
fatalError("Unsupported format")
}
}
public var c: CGFloat { return convert() }
}
K
…
•
• CGFloat
• Int,Float,Double
let value: Double = Double(CGFloat(3) + 5.cgf)
• / …
let a = CGFloat(10)
let b = 30
let value = a + b // ERROR!
let value = a + 3.14 // ERROR!
CGFConvertible
CGFConvertible
Double
…
…
let a = 3
let x: CGFloat = a + CGFloat(4.2) // OK
let y: CGFloat = a * 4.2 // OK, yay!
extension CGFConvertible {
private typealias CombineType = (Double,Double) -> Double
fileprivate func operate<T:CGFConvertible, V:CGFConvertible>
(b:T, combine: CombineType) -> V {
let x:Double = self.convert()
let y:Double = b.convert()
return combine(x,y).convert()
}
}
func + <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible>
(lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: + )
}
func - <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible>
(lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: - )
}
a.
protocol CGFConvertible {
init (_ value: Int)
init (_ value: Float)
init (_ value: Double)
init (_ value: CGFloat)
}
extension Int: CGFConvertible {}
extension Float: CGFConvertible {}
extension Double: CGFConvertible {}
extension CGFloat: CGFConvertible {}
extension CGFConvertible {
fileprivate func convert<T: CGFConvertible>() -> T
{ return T(x) }
}
•
b.
extension CGFConvertible {
private typealias CombineType = (Double,Double) -> Double
fileprivate func operate<T:CGFConvertible, V:CGFConvertible>
(b:T, combine: CombineType) -> V {
let x:Double = self.convert()
let y:Double = b.convert()
return combine(x,y).convert()
}
}
func + <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible>
(lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: + )
}
func - <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible>
(lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: - )
}
func * <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible>
(lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: * )
}
•
let a = 3
let x: CGFloat = a + CGFloat(4.2) // OK
let y: CGFloat = a * 4.2 // OK, yay!
CGFloat ≠ Float/Double/…
• Objective-C
•
•
•
• /
•
• Swift
• CGFloat
• CGFloat
• Round up a CGFloat in Swift
• Swift
Fighting history of CGFloat in Swift

Mais conteúdo relacionado

Destaque

English for programmers
English for programmersEnglish for programmers
English for programmersHirohito Kato
 
A4でまとめるClean architecture概要
A4でまとめるClean architecture概要A4でまとめるClean architecture概要
A4でまとめるClean architecture概要Hirohito Kato
 
プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集tecopark
 
知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)Ken Morishita
 
Visual studioとそのライバル
Visual studioとそのライバルVisual studioとそのライバル
Visual studioとそのライバルTadahiro Ishisaka
 
Digitization-software is eating the world
Digitization-software is eating the worldDigitization-software is eating the world
Digitization-software is eating the worldKenji Hiranabe
 
160625 cloud samurai_adds_migration_160625
160625 cloud samurai_adds_migration_160625160625 cloud samurai_adds_migration_160625
160625 cloud samurai_adds_migration_160625wintechq
 
デザイン・制作をはじめる前に
取り組む事
デザイン・制作をはじめる前に
取り組む事デザイン・制作をはじめる前に
取り組む事
デザイン・制作をはじめる前に
取り組む事kenji goto
 
メガネ型デバイスの未来について考える
メガネ型デバイスの未来について考えるメガネ型デバイスの未来について考える
メガネ型デバイスの未来について考えるSho Okada
 
HTML5 Conference 2013 HybridCast
HTML5 Conference 2013 HybridCastHTML5 Conference 2013 HybridCast
HTML5 Conference 2013 HybridCastSatoshi Shoda
 
新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>
新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>
新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>Satoru Itabashi
 
Jenkins実践入門 第二版 What's New
Jenkins実践入門 第二版 What's NewJenkins実践入門 第二版 What's New
Jenkins実践入門 第二版 What's NewMasanori Satoh
 
開発者の方向けの Sql server(db) t sql 振り返り
開発者の方向けの Sql server(db) t sql 振り返り開発者の方向けの Sql server(db) t sql 振り返り
開発者の方向けの Sql server(db) t sql 振り返りOda Shinsuke
 
KPTAふりかえり体験研修のご紹介
KPTAふりかえり体験研修のご紹介KPTAふりかえり体験研修のご紹介
KPTAふりかえり体験研修のご紹介ESM SEC
 
Rdra4越境アジャイル
Rdra4越境アジャイルRdra4越境アジャイル
Rdra4越境アジャイルZenji Kanzaki
 
ピクト図解(R)表記ルールver1.0
ピクト図解(R)表記ルールver1.0ピクト図解(R)表記ルールver1.0
ピクト図解(R)表記ルールver1.0PICTO ZUKAI
 
Fitnesse を用いたテストの効率化について
Fitnesse を用いたテストの効率化についてFitnesse を用いたテストの効率化について
Fitnesse を用いたテストの効率化についてtecopark
 
AD設計の基礎から読み解くIaaS On AD
AD設計の基礎から読み解くIaaS On ADAD設計の基礎から読み解くIaaS On AD
AD設計の基礎から読み解くIaaS On ADNaoki Abe
 

Destaque (20)

English for programmers
English for programmersEnglish for programmers
English for programmers
 
A4でまとめるClean architecture概要
A4でまとめるClean architecture概要A4でまとめるClean architecture概要
A4でまとめるClean architecture概要
 
プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集プログラミングで言いたい聞きたいこと集
プログラミングで言いたい聞きたいこと集
 
知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)
 
Visual studioとそのライバル
Visual studioとそのライバルVisual studioとそのライバル
Visual studioとそのライバル
 
Digitization-software is eating the world
Digitization-software is eating the worldDigitization-software is eating the world
Digitization-software is eating the world
 
160625 cloud samurai_adds_migration_160625
160625 cloud samurai_adds_migration_160625160625 cloud samurai_adds_migration_160625
160625 cloud samurai_adds_migration_160625
 
Rdra in 東京
Rdra in 東京Rdra in 東京
Rdra in 東京
 
デザイン・制作をはじめる前に
取り組む事
デザイン・制作をはじめる前に
取り組む事デザイン・制作をはじめる前に
取り組む事
デザイン・制作をはじめる前に
取り組む事
 
メガネ型デバイスの未来について考える
メガネ型デバイスの未来について考えるメガネ型デバイスの未来について考える
メガネ型デバイスの未来について考える
 
HTML5 Conference 2013 HybridCast
HTML5 Conference 2013 HybridCastHTML5 Conference 2013 HybridCast
HTML5 Conference 2013 HybridCast
 
新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>
新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>
新規ビジネスデザイン研修 DYA2  テキスト<サンプル版>
 
Jenkins実践入門 第二版 What's New
Jenkins実践入門 第二版 What's NewJenkins実践入門 第二版 What's New
Jenkins実践入門 第二版 What's New
 
開発者の方向けの Sql server(db) t sql 振り返り
開発者の方向けの Sql server(db) t sql 振り返り開発者の方向けの Sql server(db) t sql 振り返り
開発者の方向けの Sql server(db) t sql 振り返り
 
KPTAふりかえり体験研修のご紹介
KPTAふりかえり体験研修のご紹介KPTAふりかえり体験研修のご紹介
KPTAふりかえり体験研修のご紹介
 
Rdra4越境アジャイル
Rdra4越境アジャイルRdra4越境アジャイル
Rdra4越境アジャイル
 
ピクト図解(R)表記ルールver1.0
ピクト図解(R)表記ルールver1.0ピクト図解(R)表記ルールver1.0
ピクト図解(R)表記ルールver1.0
 
Fitnesse を用いたテストの効率化について
Fitnesse を用いたテストの効率化についてFitnesse を用いたテストの効率化について
Fitnesse を用いたテストの効率化について
 
Ad設計
Ad設計Ad設計
Ad設計
 
AD設計の基礎から読み解くIaaS On AD
AD設計の基礎から読み解くIaaS On ADAD設計の基礎から読み解くIaaS On AD
AD設計の基礎から読み解くIaaS On AD
 

Semelhante a Fighting history of CGFloat in Swift

Functional Systems @ Twitter
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ TwitterC4Media
 
The Ring programming language version 1.6 book - Part 86 of 189
The Ring programming language version 1.6 book - Part 86 of 189The Ring programming language version 1.6 book - Part 86 of 189
The Ring programming language version 1.6 book - Part 86 of 189Mahmoud Samir Fayed
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeMicrosoft Tech Community
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeMicrosoft Tech Community
 
The Ring programming language version 1.8 book - Part 12 of 202
The Ring programming language version 1.8 book - Part 12 of 202The Ring programming language version 1.8 book - Part 12 of 202
The Ring programming language version 1.8 book - Part 12 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 14 of 210
The Ring programming language version 1.9 book - Part 14 of 210The Ring programming language version 1.9 book - Part 14 of 210
The Ring programming language version 1.9 book - Part 14 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 63 of 88
The Ring programming language version 1.3 book - Part 63 of 88The Ring programming language version 1.3 book - Part 63 of 88
The Ring programming language version 1.3 book - Part 63 of 88Mahmoud Samir Fayed
 
Deepak Soni ,BCA Third Year
Deepak Soni ,BCA Third YearDeepak Soni ,BCA Third Year
Deepak Soni ,BCA Third YearDezyneecole
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019corehard_by
 
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-Gota Kakehi
 
C Programming :- An Example
C Programming :- An Example C Programming :- An Example
C Programming :- An Example Atit Gaonkar
 
The Ring programming language version 1.5.1 book - Part 8 of 180
The Ring programming language version 1.5.1 book - Part 8 of 180The Ring programming language version 1.5.1 book - Part 8 of 180
The Ring programming language version 1.5.1 book - Part 8 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 9 of 184
The Ring programming language version 1.5.3 book - Part 9 of 184The Ring programming language version 1.5.3 book - Part 9 of 184
The Ring programming language version 1.5.3 book - Part 9 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 9 of 185
The Ring programming language version 1.5.4 book - Part 9 of 185The Ring programming language version 1.5.4 book - Part 9 of 185
The Ring programming language version 1.5.4 book - Part 9 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 59 of 189
The Ring programming language version 1.6 book - Part 59 of 189The Ring programming language version 1.6 book - Part 59 of 189
The Ring programming language version 1.6 book - Part 59 of 189Mahmoud Samir Fayed
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfcontact32
 
Cocos2d実践編 1.0.0rc
Cocos2d実践編 1.0.0rcCocos2d実践編 1.0.0rc
Cocos2d実践編 1.0.0rcYuichi Higuchi
 
The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210Mahmoud Samir Fayed
 

Semelhante a Fighting history of CGFloat in Swift (20)

Functional Systems @ Twitter
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ Twitter
 
The Ring programming language version 1.6 book - Part 86 of 189
The Ring programming language version 1.6 book - Part 86 of 189The Ring programming language version 1.6 book - Part 86 of 189
The Ring programming language version 1.6 book - Part 86 of 189
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
 
How to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ CodeHow to Adopt Modern C++17 into Your C++ Code
How to Adopt Modern C++17 into Your C++ Code
 
The Ring programming language version 1.8 book - Part 12 of 202
The Ring programming language version 1.8 book - Part 12 of 202The Ring programming language version 1.8 book - Part 12 of 202
The Ring programming language version 1.8 book - Part 12 of 202
 
The Ring programming language version 1.9 book - Part 14 of 210
The Ring programming language version 1.9 book - Part 14 of 210The Ring programming language version 1.9 book - Part 14 of 210
The Ring programming language version 1.9 book - Part 14 of 210
 
The Ring programming language version 1.3 book - Part 63 of 88
The Ring programming language version 1.3 book - Part 63 of 88The Ring programming language version 1.3 book - Part 63 of 88
The Ring programming language version 1.3 book - Part 63 of 88
 
Deepak Soni ,BCA Third Year
Deepak Soni ,BCA Third YearDeepak Soni ,BCA Third Year
Deepak Soni ,BCA Third Year
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
 
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
 
C Programming :- An Example
C Programming :- An Example C Programming :- An Example
C Programming :- An Example
 
The Ring programming language version 1.5.1 book - Part 8 of 180
The Ring programming language version 1.5.1 book - Part 8 of 180The Ring programming language version 1.5.1 book - Part 8 of 180
The Ring programming language version 1.5.1 book - Part 8 of 180
 
The Ring programming language version 1.5.3 book - Part 9 of 184
The Ring programming language version 1.5.3 book - Part 9 of 184The Ring programming language version 1.5.3 book - Part 9 of 184
The Ring programming language version 1.5.3 book - Part 9 of 184
 
The Ring programming language version 1.5.4 book - Part 9 of 185
The Ring programming language version 1.5.4 book - Part 9 of 185The Ring programming language version 1.5.4 book - Part 9 of 185
The Ring programming language version 1.5.4 book - Part 9 of 185
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184
 
The Ring programming language version 1.6 book - Part 59 of 189
The Ring programming language version 1.6 book - Part 59 of 189The Ring programming language version 1.6 book - Part 59 of 189
The Ring programming language version 1.6 book - Part 59 of 189
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
Cocos2d実践編 1.0.0rc
Cocos2d実践編 1.0.0rcCocos2d実践編 1.0.0rc
Cocos2d実践編 1.0.0rc
 
The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210
 

Mais de Hirohito Kato

functional programming & c++
functional programming & c++functional programming & c++
functional programming & c++Hirohito Kato
 
Linux device driver for dummies
Linux device driver for dummiesLinux device driver for dummies
Linux device driver for dummiesHirohito Kato
 
subversion hacks (create a commit template)
subversion hacks (create a commit template)subversion hacks (create a commit template)
subversion hacks (create a commit template)Hirohito Kato
 
Git flowの活用事例
Git flowの活用事例Git flowの活用事例
Git flowの活用事例Hirohito Kato
 
yidev第七回勉強会:「Assets Library手習い」発表資料
yidev第七回勉強会:「Assets Library手習い」発表資料yidev第七回勉強会:「Assets Library手習い」発表資料
yidev第七回勉強会:「Assets Library手習い」発表資料Hirohito Kato
 

Mais de Hirohito Kato (6)

functional programming & c++
functional programming & c++functional programming & c++
functional programming & c++
 
Linux device driver for dummies
Linux device driver for dummiesLinux device driver for dummies
Linux device driver for dummies
 
subversion hacks (create a commit template)
subversion hacks (create a commit template)subversion hacks (create a commit template)
subversion hacks (create a commit template)
 
Git flowの活用事例
Git flowの活用事例Git flowの活用事例
Git flowの活用事例
 
yidev第七回勉強会:「Assets Library手習い」発表資料
yidev第七回勉強会:「Assets Library手習い」発表資料yidev第七回勉強会:「Assets Library手習い」発表資料
yidev第七回勉強会:「Assets Library手習い」発表資料
 
Mosquito Attack
Mosquito AttackMosquito Attack
Mosquito Attack
 

Último

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 

Último (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 

Fighting history of CGFloat in Swift

  • 2. • try! Swift 2016 
 Swift (@RGfox)
 • ( ) 

  • 4.
  • 5.
  • 6. CGFloat • Core Graphics • • SpriteKit/SceneKit UIKit •
  • 7. CGFloat • double / float typedef • CPU • double
  • 8. • CGFloat • NSNumber CGFloat • double • iOS iPhone5 or iPhone5S 

  • 10.
  • 13.
  • 17.
  • 18.
  • 19. let value1: CGFloat = 240 // OK
  • 20. let value1: CGFloat = 240 // OK let value2: CGFloat = 80.0 // OK
  • 21.
  • 22. let a = CGFloat(10)
  • 23. let a = CGFloat(10) let b = 30
  • 24. let a = CGFloat(10) let b = 30 let value = a + b // ERROR!
  • 25. let a = CGFloat(10) let b = 30 let value = a + b // ERROR! let value = a + CGFloat(b) // OK
  • 26.
  • 27. let pos = CGPoint(x: 160, y: 80.0)
  • 28. let pos = CGPoint(x: 160, y: 80.0) // OK
  • 29. let pos = CGPoint(x: 160, y: 80.0) // OK let a = 160
  • 30. let pos = CGPoint(x: 160, y: 80.0) // OK let a = 160 let pos = CGPoint(x: a, y: 80.0)
  • 31. let pos = CGPoint(x: 160, y: 80.0) // OK let a = 160 let pos = CGPoint(x: a, y: 80.0) // ERROR!
  • 32. let pos = CGPoint(x: 160, y: 80.0) // OK let a = 160 let pos = CGPoint(x: a, y: 80.0) // ERROR!
  • 33.
  • 34. let x = 32
  • 35. let x = 32 let y = 120.0
  • 36. let x = 32 let y = 120.0 let w = Float(44.0)
  • 37. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24)
  • 38. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: x,
  • 39. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: x, y: y,
  • 40. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: x, y: y, width: w,
  • 41. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: x, y: y, width: w, height: h)
  • 42. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: x, y: y, width: w, height: h) // ERROR!
  • 43. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: x, y: y, width: w, height: h) // ERROR!
  • 44.
  • 45. let x = 32
  • 46. let x = 32 let y = 120.0
  • 47. let x = 32 let y = 120.0 let w = Float(44.0)
  • 48. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24)
  • 49. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: CGFloat(x),
  • 50. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: CGFloat(x), y: CGFloat(y),
  • 51. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(w),
  • 52. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(w), height: h)
  • 53. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(w), height: h) // OK
  • 54. let x = 32 let y = 120.0 let w = Float(44.0) let h = CGFloat(24) view.rect = CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(w), height: h) // OK
  • 55.
  • 56.
  • 59.
  • 61.
  • 62.
  • 63. extension Double { var cgf:CGFloat { return CGFloat(self) } } let y = 1 + 10.0.cgf // OK, yay!
  • 64. extension Double { var cgf:CGFloat { return CGFloat(self) } } extension Int { var cgf:CGFloat { return CGFloat(self) } } extension Float { var cgf:CGFloat { return CGFloat(self) } }
  • 65.
  • 66. extension Double { var c:CGFloat { return CGFloat(self) } } extension Int { var c:CGFloat { return CGFloat(self) } } extension Float { var c:CGFloat { return CGFloat(self) } } extension Float80 { var c:CGFloat { return CGFloat(self) } } extension UInt { var c:CGFloat { return CGFloat(self) } } extension UInt8 { var c:CGFloat { return CGFloat(self) } } extension UInt16 { var c:CGFloat { return CGFloat(self) } } extension UInt32 { var c:CGFloat { return CGFloat(self) } } extension UInt64 { var c:CGFloat { return CGFloat(self) } } extension Int8 { var c:CGFloat { return CGFloat(self) } } extension Int16 { var c:CGFloat { return CGFloat(self) } } extension Int32 { var c:CGFloat { return CGFloat(self) } } extension Int64 { var c:CGFloat { return CGFloat(self) } } extension UInt64 { var c:CGFloat { return CGFloat(self) } } extension MyNumberType { var c:CGFloat { return CGFloat(self) } } extension MyNumberTypeB { var c:CGFloat { return CGFloat(self) } } : …
  • 68.
  • 69. Swift
  • 70.
  • 71. OK let a: Float = 190.0 let b: Int = 3 let value = CGFloat(a) + CGFloat(b)
  • 72. CGFloat public struct CGFloat { public init() public init(_ value: Float) public init(_ value: Double) public init(_ value: Float80) public init(_ value: CGFloat) public init(_ value: UInt8) public init(_ value: Int8) :
  • 75. Protocol = • API • • Java/C# Interface Ruby mix-in C++
  • 76. • ( ) protocol • extension • / protocol extension Int: MyCustomProtocol {} Swift protocol
  • 77. Swift protocol • init • protocol CGFConvertible { init (_ value: Int) init (_ value: Float) init (_ value: Double) init (_ value: CGFloat) } // extension Int: CGFConvertible {} extension Float: CGFConvertible {} extension Double: CGFConvertible {} extension CGFloat: CGFConvertible {}
  • 80. • … Swift 
 extension CGFConvertible { fileprivate func convert <T: CGFConvertible>() -> T { return T(self) } }
  • 81. func checkType<T>(_ x: T) { switch x { case _ as CGFloat: print("(x) is a CGFloat") case _ as Float: print("(x) is a Float") case _ as Int: print("(x) is a Int") case _ as Double: print("(x) is a Double") default: print("(x) is unknown..") } } Swift Workaround RTTI
  • 82. extension CGFConvertible { fileprivate func convert<T: CGFConvertible>() -> T { switch self { case let x as CGFloat: return T(x) case let x as Float: return T(x) case let x as Int: return T(x) case let x as Double: return T(x) default: fatalError("Unsupported format") } } public var c: CGFloat { return convert() } } K
  • 83. … • • CGFloat • Int,Float,Double let value: Double = Double(CGFloat(3) + 5.cgf)
  • 84.
  • 85. • / … let a = CGFloat(10) let b = 30 let value = a + b // ERROR! let value = a + 3.14 // ERROR!
  • 89.
  • 90. let a = 3 let x: CGFloat = a + CGFloat(4.2) // OK let y: CGFloat = a * 4.2 // OK, yay!
  • 91. extension CGFConvertible { private typealias CombineType = (Double,Double) -> Double fileprivate func operate<T:CGFConvertible, V:CGFConvertible> (b:T, combine: CombineType) -> V { let x:Double = self.convert() let y:Double = b.convert() return combine(x,y).convert() } } func + <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible> (lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: + ) } func - <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible> (lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: - ) }
  • 92.
  • 93. a. protocol CGFConvertible { init (_ value: Int) init (_ value: Float) init (_ value: Double) init (_ value: CGFloat) } extension Int: CGFConvertible {} extension Float: CGFConvertible {} extension Double: CGFConvertible {} extension CGFloat: CGFConvertible {} extension CGFConvertible { fileprivate func convert<T: CGFConvertible>() -> T { return T(x) } } •
  • 94. b. extension CGFConvertible { private typealias CombineType = (Double,Double) -> Double fileprivate func operate<T:CGFConvertible, V:CGFConvertible> (b:T, combine: CombineType) -> V { let x:Double = self.convert() let y:Double = b.convert() return combine(x,y).convert() } } func + <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible> (lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: + ) } func - <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible> (lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: - ) } func * <T:CGFConvertible, U:CGFConvertible,V:CGFConvertible> (lhs: T, rhs: U) -> V { return lhs.operate(b: rhs, combine: * ) } •
  • 95. let a = 3 let x: CGFloat = a + CGFloat(4.2) // OK let y: CGFloat = a * 4.2 // OK, yay!
  • 96.
  • 97.
  • 98. CGFloat ≠ Float/Double/… • Objective-C • • • • / • • Swift
  • 99. • CGFloat • CGFloat • Round up a CGFloat in Swift • Swift