Skip to content

EnesKaraosman/dart_kotlin_swift_comparison

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 

Repository files navigation

Dart vs Kotlin vs Swift Comparison

Versions: Swift (5.4.2), Dart (2.13.4), Kotlin

Basics, Variables & Constants

Dart Kotlin Swift
// Implicit type = int var myVariable = 5; // Explicit type: String String name = "Kral"; // Dynamic type: dynamic dynamic dinamik = "Dynamic"; dinamik = 5; // Valid // Constant final name = "Enes"; const name = "Enes"; // Anything that isn't known at  // compile time  // should be final over const.

Difference between const and final

// Implicit type = Int var myVariable = 5 // Explicit type: String var name: String = "Kral" // Dynamic type: dynamic var dinamik: dynamic = "String" dinamik = 5 // Valid // Constant const val name = "Enes" val name = "Enes"
// Implicit type = Int var myVariable = 5 myVariable = "a string" // Invalid // Explicit type: String var name: String = "Kral" // Dynamic type: Any var dinamik: Any = "String" dinamik = 5 // Valid // Constant let gender = "Man" gender = "Woman" // Invalid
// Enum enum Color { red, green, blue } var aColor = Color.blue; // aColor has index, hashCode & toString() parameters by default. // Enum cases can not contain associated values like in Swift. // Computed variables & Functions can't be added directly to the enum. // But they can be added via an extension. extension DarkMode on Color { Color get darkModeAlternative { switch (this) { case Color.blue: return Color.red; default: return Color.blue; } } }
// Enum enum class Color {RED, GREEN, BLUE} var aColor = Color.BLUE
// Enum enum MessageKind { case placeholder // Associated values allowed. case image(URL) case text(String) // Computed variables & Static variables allowed. var debugPrint: String { switch self { case .image(let url): return "Url: \(url)" case .text(let text): return "Text: \(text)" default: return "Placeholder" } } // Functions are allowed as well. func debugPrintFoo() -> String { debugPrint } }
typedef GestureTapCallback = void Function();
// Fill
// Type alias typealias TapCallback = () -> Void
// Check type if (myVariable is String) { .. }
// Fill
// Check type if myVariable is String { .. }

Strings

Dart Kotlin Swift
// Declaration var string1 = "String"; var string2 = 'String'; // Multiline var multilineStr = """ Here you can have your string multiline for easier readability; Start & end with 3 quote character. """; // String interpolation var name = "Enes"; print("Hello $name"); print("For objects use ${person.name}");
// Declaration var string1 = "String" // Multiline var multilineStr = """ Here you can have your string multiline for easier readability; Start & end with 3 quote character. """ // String interpolation var name = "Enes"; print("Hello $name"); print("For objects use ${person.name}");
// Declaration let string = "String" // Multiline let multilineStr = """ Here you can have your string multiline for easier readability; Start & end with 3 quote character. """ // String interpolation let name = "Enes" print("Hello \(name)") print("For objects use \(person.name)")

Functions

Dart Kotlin Swift
// Declaration String greet(String name) { return "Hey $name"; } // Optional named parameters void greet(String name) { } greet("Enes"); // Named parameters void greet({required String name}) { } greet(name: "Enes") // Default parameters void greet({String name = "Enes"}) { } greet(); greet(name: "Kral"); // Var args // N/A // List can be used to achieve // similar behaviour // or visit `VarArgs` package below. // Closure/Lambda var square = (int x) => x * x; square(4);

VarArgs Package

// Declaration fun greet(name: String): String { return "Hey $name" } // Optional named parameters fun greet(name: String = "No Name") { } greet("Enes"); // Named parameters fun greet(name: String) { } greet("Enes") // Default parameters fun greet(name: String = "Enes") { } greet(); greet(name: "Kral");
// Declaration func greet(name: String) -> String { // return keyword is optional if body contains single (code) line return "Hey \(name)" } // Optional named parameters func greet(_ name: String) { } greet("Enes") // Named parameters func greet(name: String) { } greet(name: "Enes") // Default parameters func greet(name: String = "Enes") { } greet() greet(name: "Adam") // Var args func greet(_ names: String...) { } greet("Enes", "Egemen", "Ferid", "Serkan") // Closure/Lambda var square: (Int) -> Int = { $0 * $0 } var square = { (x: Int) -> Int in return x * x } square(4)

Control Flow

Dart Kotlin Swift
// if & else(if) if (isRaining()) { you.bringRainCoat(); } else if (isSnowing()) { you.wearJacket(); } else { car.putTopDown(); } // for loops for (var i = 0; i < 5; i++) { print('Flutter ${i + 1}'); } // for-in var obj = ["Android","iOS","Flutter"]; for (var prop in obj) { print(prop); } // for-each var collection = ["Android", "iOS", "Flutter"]; collection.forEach(print); // or var callbacks = []; for (var i = 0; i < 2; i++) { callbacks.add(() => print(i)); } callbacks.forEach((c) => c()); // while while (!isDone()) { doSomething(); } // do-while do { printLine(); } while (!atEndOfPage()); // break while (true) { if (shutDownRequested()) break; processIncomingRequests(); } // continue for (int i = 0; i < candidates.length; i++) { var candidate = candidates[i]; if (candidate.yearsExperience < 5) { continue; } candidate.interview(); } // or candidates .where((c) => c.yearsExperience >= 5) .forEach((c) => c.interview()); // switch-case var option = 1; switch (option) { case 1: print("Android"); break; case 2: print("iOS"); break; default: print("Flutter"); break; } // switch-continue var option = 1; switch (option) { case 1: print("Android"); continue testCase; testCase: case 2: print("iOS"); break; } 
// Fill
// if & else(if) if isRaining() { you.bringRainCoat() } else if isSnowing() { you.wearJacket() } else { car.putTopDown() } // for loops // swift does not have C like for loops for i in 0..<5 { print("Swift \(i + 1)") } // for-in var collection = ["Android", "iOS", "Flutter"] for item in collection { print(item) } // for-each collection.forEach { print($0) } // while while !isDone() { doSomething() } // repeat-while repeat { printLine() } while !atEndOfPage() // break while true { if (shutDownRequested()) break processIncomingRequests() } // continue for candidate in candidates { if candidate.yearsExperience < 5 { continue } candidate.interview() } // or candidates .filter { $0.yearsExperience >= 5 } .forEach { $0.interview() } // switch-case var option = 1 switch option { case 1: print("Android") case 2: // ranges also supported -> case 2...5: // ranges also supported -> case 2..<6: print("iOS") default: print("Flutter") } // Prints "Android" // switch-continue var option = 1 switch option { case 1: print("Android"); fallthrough case 2: print("iOS") default: print("Flutter"); } // Prints "Android" & "iOS"

Collections

Dart Kotlin Swift
// Fill
// Fill
// Fill

Optionals & Nullability

Dart Kotlin Swift
// Declaration String str = null; // Invalid String? str = null; // Valid str?.toUpperCase(); // Safe call String? str = "enes"; str?.toUpperCase(); // "ENES" // Null aware operator int? id = null; var userId = id ?? -1; // -1 int? id = 53; var userId = id ?? -1; // 53
// Fill
// Declaration var str: String = nil // Invalid var str: String? = nil // Valid str?.uppercased() // Safe call var str: String? = "enes"; str?.uppercased() // Optional("ENES") // Nil Coalescing operator var id: Int? = nil var userId = id ?? -1 // -1 var id: Int? = 53; var userId = id ?? -1; // 53

Extensions

Dart Kotlin Swift
// Fill
// Fill
// Fill

Class, Struct, Protocol..

Dart Kotlin Swift
// Fill
// Fill
// Fill

About

This repo contains some kind of cheatsheet to quickly compare the basics of `Dart` `Kotlin` and `Swift`.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •