SlideShare a Scribd company logo
1 of 100
Download to read 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

More Related Content

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 
Billing in a supermarket c++
Billing in a supermarket c++Billing in a supermarket c++
Billing in a supermarket c++varun arora
 
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
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsRup Chowdhury
 
Deepak Soni ,BCA Third Year
Deepak Soni ,BCA Third YearDeepak Soni ,BCA Third Year
Deepak Soni ,BCA Third YearDezyneecole
 
Anomalies in X-Ray Engine
Anomalies in X-Ray EngineAnomalies in X-Ray Engine
Anomalies in X-Ray EnginePVS-Studio
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019corehard_by
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?勇浩 赖
 
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-Gota Kakehi
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to GriffonJames Williams
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationKevin Keraudren
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads France
 
C Programming :- An Example
C Programming :- An Example C Programming :- An Example
C Programming :- An Example Atit Gaonkar
 

Similar to 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
 
Billing in a supermarket c++
Billing in a supermarket c++Billing in a supermarket c++
Billing in a supermarket c++
 
Vcs15
Vcs15Vcs15
Vcs15
 
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
 
Scala.io
Scala.ioScala.io
Scala.io
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
 
Deepak Soni ,BCA Third Year
Deepak Soni ,BCA Third YearDeepak Soni ,BCA Third Year
Deepak Soni ,BCA Third Year
 
Anomalies in X-Ray Engine
Anomalies in X-Ray EngineAnomalies in X-Ray Engine
Anomalies in X-Ray Engine
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
Kinect勉強会 Vol.3 -ぼくのかんがえた最強のNUI-
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to Griffon
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimization
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIView
 
C Programming :- An Example
C Programming :- An Example C Programming :- An Example
C Programming :- An Example
 

More from 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
 

More from 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
 

Recently uploaded

chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 

Recently uploaded (20)

chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 

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