Python for Swift
/ LINE Fukuoka
2018/8/22, LINE Developer meetup in Kyoto #43
● iOS Developer at LINE Fukuoka
● Project: LINE Creators Studio
Self introduction
● iOS Developer at LINE Fukuoka
● Project: LINE Creators Studio
Self introduction
Today’s topic
● Inspection (find unused file, etc)
● Code generation
We use Python for two purposes
● Inspection (find unused file, etc)
● Code generation
We use Python for two purposes
Today’s topic
There is a common saying
among
Swift developers
Good implementation produces
compilation error
before running with problem
● Any string values are acceptable as filename
Cause of runtime error
For example…
read(“path/to/file”)
send(“v2”)
● When you want to send version
● “2”, ”ver2” are acceptable, and caller must assure the input
● Any string values are acceptable as filename
Cause of runtime error
For example…
read(“path/to/file”)
send(“v2”)
● When you want to send version
● “2”, ”ver2” are acceptable, and caller must assure the input
Runtime Error
● Any string values are acceptable as filename
Cause of runtime error
Solutions
read(“path/to/file”)
send(“v2”)
● When you want to send version
● “2”, ”ver2” are acceptable, and caller must assure the input
Android generates R, and we can write like `R.id.filename`
Define original types like enum (※constant string is not enough)
● A code generator like R of Android
● Variable asset is Asset type, and we cannot use String, etc…
● Also we can use for Font, Color, Localizing, …
https://github.com/SwiftGen/SwiftGen
SwiftGen
"lfk" = "LINE Fukuoka ";
"meetup.name" = “Developer Meetup #%d";
"meetup.themeis" = " %@";
enum L10n {
/// LINE Fukuoka
static let lfk = L10n.tr("localizable", "lfk")
enum Meetup {
/// LINE Developer Meetup #%d
static func name(_ p1: Int) -> String {
return L10n.tr("localizable",
"meetup.name", p1)
}
/// %@
static func themeis(_ p1: String) -> String {
return L10n.tr("localizable",
"meetup.themeis", p1)
}
}
}
$ swiftgen strings -t dot-syntax-swift3 path/to/file
SwiftGen output example
● Dependency Injection / @_ishkawa
iOSDC2017
● Safety + Easiness
Is the best thing to do
for Swift
● Tool for code generation by Apple
● used in apple/swift repository
● uses Python
gyb
https://github.com/apple/swift/blob/2a25a8c14fecaaca68d881fcbcba71a85bcc1e9d/validation-test/stdlib/ArraySlice.swift.gyb
● 100+ Analytics events defined on Wiki
● We have to copy&paste these all?
Motivation
● Parse HTML Table
● Generate Swift code
Specifications
track(AnalyticsEvent.ScreenA.event1)
https://qiita.com/_ha1f/items/f318e1614edb6b08ac3f
Please refer Qiita for detail!
● Types help as to be safe!
● Writing small scripts with shell, Python is good…
● We have to write without types to be typed? No! You can use
Swift + Sourcery if you have enough time!
Conclusion
LINE Fukuoka
@_ha1f
https://github.com/ha1f
THANK YOU

Python for Swift

  • 1.
    Python for Swift /LINE Fukuoka 2018/8/22, LINE Developer meetup in Kyoto #43
  • 2.
    ● iOS Developerat LINE Fukuoka ● Project: LINE Creators Studio Self introduction
  • 3.
    ● iOS Developerat LINE Fukuoka ● Project: LINE Creators Studio Self introduction Today’s topic
  • 4.
    ● Inspection (findunused file, etc) ● Code generation We use Python for two purposes
  • 5.
    ● Inspection (findunused file, etc) ● Code generation We use Python for two purposes Today’s topic
  • 6.
    There is acommon saying among Swift developers
  • 7.
    Good implementation produces compilationerror before running with problem
  • 8.
    ● Any stringvalues are acceptable as filename Cause of runtime error For example… read(“path/to/file”) send(“v2”) ● When you want to send version ● “2”, ”ver2” are acceptable, and caller must assure the input
  • 9.
    ● Any stringvalues are acceptable as filename Cause of runtime error For example… read(“path/to/file”) send(“v2”) ● When you want to send version ● “2”, ”ver2” are acceptable, and caller must assure the input Runtime Error
  • 10.
    ● Any stringvalues are acceptable as filename Cause of runtime error Solutions read(“path/to/file”) send(“v2”) ● When you want to send version ● “2”, ”ver2” are acceptable, and caller must assure the input Android generates R, and we can write like `R.id.filename` Define original types like enum (※constant string is not enough)
  • 11.
    ● A codegenerator like R of Android ● Variable asset is Asset type, and we cannot use String, etc… ● Also we can use for Font, Color, Localizing, … https://github.com/SwiftGen/SwiftGen SwiftGen
  • 12.
    "lfk" = "LINEFukuoka "; "meetup.name" = “Developer Meetup #%d"; "meetup.themeis" = " %@"; enum L10n { /// LINE Fukuoka static let lfk = L10n.tr("localizable", "lfk") enum Meetup { /// LINE Developer Meetup #%d static func name(_ p1: Int) -> String { return L10n.tr("localizable", "meetup.name", p1) } /// %@ static func themeis(_ p1: String) -> String { return L10n.tr("localizable", "meetup.themeis", p1) } } } $ swiftgen strings -t dot-syntax-swift3 path/to/file SwiftGen output example
  • 13.
    ● Dependency Injection/ @_ishkawa iOSDC2017 ● Safety + Easiness Is the best thing to do for Swift
  • 14.
    ● Tool forcode generation by Apple ● used in apple/swift repository ● uses Python gyb https://github.com/apple/swift/blob/2a25a8c14fecaaca68d881fcbcba71a85bcc1e9d/validation-test/stdlib/ArraySlice.swift.gyb
  • 15.
    ● 100+ Analyticsevents defined on Wiki ● We have to copy&paste these all? Motivation
  • 16.
    ● Parse HTMLTable ● Generate Swift code Specifications track(AnalyticsEvent.ScreenA.event1)
  • 17.
  • 18.
    ● Types helpas to be safe! ● Writing small scripts with shell, Python is good… ● We have to write without types to be typed? No! You can use Swift + Sourcery if you have enough time! Conclusion
  • 19.
  • 20.