Lessons Learned From Porting To Swift And Kotlin - Mobile Era Oslo, October 2017

Lessons Learned From Porting To Swift And Kotlin - Mobile Era Oslo, October 2017

Video:
https://vimeo.com/237875498

Abstract:
SpotHero has been moving its entire iOS application to Swift and started moving its Android app to Kotlin. Here's some lessons learned in the process in terms of selling management on using a new languages, how to get started, and some tips on things you should do along the way.

(Big thanks to my old crew at SpotHero!)

C4861b1dfdf3bbb21faec4a1acdf183d?s=128

Ellen Shapiro

October 05, 2017
Tweet

Transcript

  1. 1.

    Lessons learned from porting to Swift and Kotlin by Ellen

    Shapiro Mobile Era | Oslo, Norway | October 2017 @designatednerd | justhum.com | designatednerd.com
  2. 2.
  3. 4.

    !

  4. 5.

    !

  5. 6.

    !""

  6. 7.

    !"#

  7. 8.

    !"#

  8. 9.

    !

  9. 10.

    !

  10. 11.
  11. 12.
  12. 13.
  13. 14.
  14. 15.
  15. 16.
  16. 17.
  17. 18.
  18. 19.
  19. 20.
  20. 21.
  21. 22.

    iOS

  22. 24.
  23. 28.
  24. 37.
  25. 38.
  26. 39.
  27. 40.
  28. 46.

    Objective-C NSMutableArray *capitalizedStrings = [NSMutableArray array]; for (NSString *string in

    arrayOfStrings) { NSString *thing = [string capitalized]; [capitalizedStrings addObject: thing]; }
  29. 47.

    Objective-C, but written in Swift var capitalizedStrings = [String]() for

    string in arrayOfStrings { let thing = string.capitalized capitalizedStrings.append(thing) }
  30. 49.

    Swiftier let capitalizedStrings = arrayOfStrings.map { string in let thing

    = string.capitalized return thing } Swiftiest let capitalizedStrings = arrayOfStrings.map { $0.capitalized }
  31. 52.
  32. 53.
  33. 54.
  34. 55.
  35. 56.
  36. 57.
  37. 58.
  38. 64.

    NPE

  39. 67.

    Objective-C JSON Parsing - (instancetype)fromJSONData:(NSData *)data { NSDictionary* jsonObject =

    [NSJSONSerialization jsonObjectWithData:data error:nil]; if (jsonObject == nil]) { return nil; } user.firstName = dictionary[@"first_name"]; user.lastName = dictionary[@"last_name"]; user.age = [dictionary[@"age"] integerValue]; return user; }
  40. 68.

    Objective-C JSON Parsing - (instancetype)fromJSONData:(NSData *)data { NSDictionary* jsonObject =

    [NSJSONSerialization jsonObjectWithData:data error:nil]; if (jsonObject == nil || ![jsonObject isKindOfClass:[NSDictionary class]]) { return nil; } if ([dictionary[@"first_name"] isKindOfClass: [NSString class]]) { user.firstName = dictionary[@"first_name"]; } else { return nil; } if ([dictionary[@"last_name"] isKindOfClass: [NSString class]]) { user.lastName = dictionary[@"last_name"]; } else { return nil; } if ([dictionary[@"age"] isKindOfClass: [NSNumber class]]) { user.age = [dictionary[@"age"] integerValue]; } else { return nil; } return user; }
  41. 69.

    Swift 4 JSON Parsing static func from(jsonData: Data) -> User?

    { let decoder = JSONDecoder() return try? decoder.decode(User.self, for: jsonData) }
  42. 70.

    (This does require some setup) struct User: Codable { let

    firstName: String let lastName: String let age: Int enum CodingKeys: String, CodingKey { case firstName = "first_name", lastName = "last_name", age } }
  43. 79.

    activity_profile.xml <TextView android:id="@+id/textview_username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> ProfileActivity.java TextView mUsernameTextView; @Override

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile_activity); mUsernameTextView = (TextView) findViewById(R.id.textview_username); mUsernameTextView.setText("Hello, User"); }
  44. 80.

    activity_profile.xml <TextView android:id="@+id/textview_username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> ProfileActivity.java @BindView(R.id.textview_username) TextView mUsernameTextView;

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.profile_activity); mUsernameTextView.setText("Hello, User"); }
  45. 81.

    activity_profile.xml <TextView android:id="@+id/textview_username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> ProfileActivity.kt import kotlinx.android.synthetic.main.activity_profile.* public

    override fun onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_profile) textview_username.text = "Hello, User" }
  46. 82.

    activity_profile.xml <TextView android:id="@+id/usernameTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> ProfileActivity.kt import kotlinx.android.synthetic.main.activity_profile.* public

    override fun onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_profile) usernameTextView = "Hello, User" }
  47. 84.
  48. 85.
  49. 88.
  50. 92.
  51. 96.

    Unnecessarily One-Liniest let studentNames = students.reduce("") { $0 + "\n"

    + ($1.firstName ?? "") + " " + ($1.lastName ?? "") }
  52. 97.

    ! Borderline Incomprehensible ! array.flatMap { $0 } .map {

    $0.doSomething() } .flatMap { $0 } .filter { $0.isSomething || $0.isSomethingElse } .reduce("") { $0 + $1.description }
  53. 101.
  54. 102.
  55. 103.
  56. 104.
  57. 105.
  58. 106.
  59. 107.
  60. 109.
  61. 110.
  62. 111.
  63. 112.
  64. 114.
  65. 116.

    Obligatory Summary Slide → Honestly assess the pros and cons

    → Start small, then go bonkers once it works → Test the crap out of stuff in the new language → Share your code, your joy, and your pain → Resist desire to rewrite your whole app at once → Ship it!
  66. 117.

    Links → Concurrency In Swift: One Approach https://gist.github.com/lattner/ 31ed37682ef1576b16bca1432ea9f782 →

    Apple's Swift Blog https://developer.apple.com/swift/blog/ → Jetbrains' Kotlin Blog https://blog.jetbrains.com/kotlin/
  67. 119.

    Videos → Anything You Can Do, I Can Do Better

    (Kickstarter team on FP in Kotlin vs. Swift) https://www.youtube.com/watch? v=_DuGaAkQSnM → iOS & Android & Swift & Kotlin (Stuart Kent on Kotlin for iOS Devs) http://www.stkent.com/2017/07/27/ios-and- android-and-swift-and-kotlin.html