kotlin 为什么swift_kotlin swift - CSDN
  • KotlinSwift的一些异同点

    Kotlin和Swift的一些异同点
    这里写图片描述

    展开全文
  • 一位国外的程序员认为 Swift 的语法与 Kotlin 相似,并整理了一些 SwiftKotlin 的对比,下面是一些例子,大家不妨也看看。 BASICS Hello World Swift print(“Hello, world!”) Kotlin println(“Hello, world!...

    一位国外的程序员认为 Swift 的语法与 Kotlin 相似,并整理了一些 Swift 和 Kotlin 的对比,下面是一些例子,大家不妨也看看。

    BASICS

    Hello World

    Swift

    print(“Hello, world!”)

    Kotlin

    println(“Hello, world!”)

    变量和常量

    Swift

    var myVariable = 42
    myVariable = 50
    let myConstant = 42

    Kotlin

    var myVariable = 42
    myVariable = 50
    val myConstant = 42

    显式类型

    Swift

    let explicitDouble: Double = 70

    Kotlin

    val explicitDouble: Double = 70.0

    强制类型转换

    Swift

    let label = "The width is "
    let width = 94
    let widthLabel = label + String(width)

    Kotlin

    val label = "The width is "
    val width = 94
    val widthLabel = label + width

    字符串插值

    Swift

    let apples = 3
    let oranges = 5
    let fruitSummary = "I have (apples + oranges) " +
    “pieces of fruit.”

    Kotlin

    val apples = 3
    val oranges = 5
    val fruitSummary = "I have ${apples + oranges} " +
    “pieces of fruit.”

    范围操作符

    Swift

    let names = [“Anna”, “Alex”, “Brian”, “Jack”]
    let count = names.count
    for i in 0…<count {
    print(“Person (i + 1) is called (names[i])”)
    }
    // Person 1 is called Anna
    // Person 2 is called Alex
    // Person 3 is called Brian
    // Person 4 is called Jack

    Kotlin

    val names = arrayOf(“Anna”, “Alex”, “Brian”, “Jack”)
    val count = names.count()
    for (i in 0…count - 1) {
    println(“Person ${i + 1} is called ${names[i]}”)
    }
    // Person 1 is called Anna
    // Person 2 is called Alex
    // Person 3 is called Brian
    // Person 4 is called Jack

    包罗广泛的范围操作符(Inclusive Range Operator)

    Swift

    for index in 1…5 {
    print("(index) times 5 is (index * 5)")
    }
    // 1 times 5 is 5
    // 2 times 5 is 10
    // 3 times 5 is 15
    // 4 times 5 is 20
    // 5 times 5 is 25

    Kotlin

    for (index in 1…5) {
    println("$index times 5 is ${index * 5}")
    }
    // 1 times 5 is 5
    // 2 times 5 is 10
    // 3 times 5 is 15
    // 4 times 5 is 20
    // 5 times 5 is 25

    BASICS
    数组

    Swift

    var shoppingList = [“catfish”, “water”,
    “tulips”, “blue paint”]
    shoppingList[1] = “bottle of water”

    Kotlin

    val shoppingList = arrayOf(“catfish”, “water”,
    “tulips”, “blue paint”)
    shoppingList[1] = “bottle of water”

    映射

    Swift

    var occupations = [
    “Malcolm”: “Captain”,
    “Kaylee”: “Mechanic”,
    ]
    occupations[“Jayne”] = “Public Relations”

    Kotlin

    val occupations = mutableMapOf(
    “Malcolm” to “Captain”,
    “Kaylee” to “Mechanic”
    )
    occupations[“Jayne”] = “Public Relations”

    空集合

    Swift

    let emptyArray = String
    let emptyDictionary = String: Float

    Kotlin

    val emptyArray = arrayOf()
    val emptyMap = mapOf<String, Float>()

    FUNCTIONS
    函数

    Swift

    func greet(_ name: String,_ day: String) -> String {
    return “Hello (name), today is (day).”
    }
    greet(“Bob”, “Tuesday”)

    Kotlin

    fun greet(name: String, day: String): String {
    return “Hello $name, today is $day.”
    }
    greet(“Bob”, “Tuesday”)

    元组返回

    Swift

    func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
    }

    Kotlin

    data class GasPrices(val a: Double, val b: Double,
    val c: Double)
    fun getGasPrices() = GasPrices(3.59, 3.69, 3.79)

    参数的变量数目(Variable Number Of Arguments)

    Swift

    func sumOf(_ numbers: Int…) -> Int {
    var sum = 0
    for number in numbers {
    sum += number
    }
    return sum
    }
    sumOf(42, 597, 12)

    Kotlin

    fun sumOf(vararg numbers: Int): Int {
    var sum = 0
    for (number in numbers) {
    sum += number
    }
    return sum
    }
    sumOf(42, 597, 12)

    // sumOf() can also be written in a shorter way:
    fun sumOf(vararg numbers: Int) = numbers.sum()

    函数类型

    Swift

    func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
    return 1 + number
    }
    return addOne
    }
    let increment = makeIncrementer()
    increment(7)

    Kotlin

    fun makeIncrementer(): (Int) -> Int {
    val addOne = fun(number: Int): Int {
    return 1 + number
    }
    return addOne
    }
    val increment = makeIncrementer()
    increment(7)

    // makeIncrementer can also be written in a shorter way:
    fun makeIncrementer() = fun(number: Int) = 1 + number

    映射

    Swift

    let numbers = [20, 19, 7, 12]
    numbers.map { 3 * $0 }

    Kotlin

    val numbers = listOf(20, 19, 7, 12)
    numbers.map { 3 * it }

    排序

    Swift

    var mutableArray = [1, 5, 3, 12, 2]
    mutableArray.sort()

    Kotlin

    listOf(1, 5, 3, 12, 2).sorted()

    命名参数

    Swift

    func area(width: Int, height: Int) -> Int {
    return width * height
    }
    area(width: 2, height: 3)

    Kotlin

    fun area(width: Int, height: Int) = width * height
    area(width = 2, height = 3)

    // This is also possible with named arguments
    area(2, height = 2)
    area(height = 3, width = 2)

    CLASSES
    声明

    Swift

    class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
    return “A shape with (numberOfSides) sides.”
    }
    }

    Kotlin

    class Shape {
    var numberOfSides = 0
    fun simpleDescription() =
    “A shape with $numberOfSides sides.”
    }

    用法

    Swift

    var shape = Shape()
    shape.numberOfSides = 7
    var shapeDescription = shape.simpleDescription()

    Kotlin

    var shape = Shape()
    shape.numberOfSides = 7
    var shapeDescription = shape.simpleDescription()

    子类

    Swift

    class NamedShape {
    var numberOfSides: Int = 0
    let name: String

    init(name: String) {
        self.name = name
    }
    
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
    

    }

    class Square: NamedShape {
    var sideLength: Double

    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        self.numberOfSides = 4
    }
    
    func area() -> Double {
        return sideLength * sideLength
    }
    
    override func simpleDescription() -> String {
        return "A square with sides of length " +
           sideLength + "."
    }
    

    }

    let test = Square(sideLength: 5.2, name: “square”)
    test.area()
    test.simpleDescription()

    Kotlin

    open class NamedShape(val name: String) {
    var numberOfSides = 0

    open fun simpleDescription() =
        "A shape with $numberOfSides sides."
    

    }

    class Square(var sideLength: BigDecimal, name: String) :
    NamedShape(name) {
    init {
    numberOfSides = 4
    }

    fun area() = sideLength.pow(2)
    
    override fun simpleDescription() =
        "A square with sides of length $sideLength."
    

    }

    val test = Square(BigDecimal(“5.2”), “square”)
    test.area()
    test.simpleDescription()

    类型检查

    Swift

    var movieCount = 0
    var songCount = 0

    for item in library {
    if item is Movie {
    movieCount += 1
    } else if item is Song {
    songCount += 1
    }
    }

    Kotlin

    var movieCount = 0
    var songCount = 0

    for (item in library) {
    if (item is Movie) {
    ++movieCount
    } else if (item is Song) {
    ++songCount
    }
    }

    模式匹配

    Swift

    let nb = 42
    switch nb {
    case 0…7, 8, 9: print(“single digit”)
    case 10: print(“double digits”)
    case 11…99: print(“double digits”)
    case 100…999: print(“triple digits”)
    default: print(“four or more digits”)
    }

    Kotlin

    val nb = 42
    when (nb) {
    in 0…7, 8, 9 -> println(“single digit”)
    10 -> println(“double digits”)
    in 11…99 -> println(“double digits”)
    in 100…999 -> println(“triple digits”)
    else -> println(“four or more digits”)
    }

    类型向下转换

    Swift

    for current in someObjects {
    if let movie = current as? Movie {
    print("Movie: ‘(movie.name)’, " +
    “dir. (movie.director)”)
    }
    }

    Kotlin

    for (current in someObjects) {
    if (current is Movie) {
    println("Movie: ‘${current.name}’, " +
    “dir. ${current.director}”)
    }
    }

    协议

    Swift

    protocol Nameable {
    func name() -> String
    }

    func f<T: Nameable>(x: T) {
    print("Name is " + x.name())
    }

    Kotlin

    interface Nameable {
    fun name(): String
    }

    fun f<T: Nameable>(x: T) {
    println("Name is " + x.name())
    }

    扩展

    Swift

    extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
    }
    let oneInch = 25.4.mm
    print(“One inch is (oneInch) meters”)
    // prints “One inch is 0.0254 meters”
    let threeFeet = 3.ft
    print(“Three feet is (threeFeet) meters”)
    // prints “Three feet is 0.914399970739201 meters”

    Kotlin

    val Double.km: Double get() = this * 1000
    val Double.m: Double get() = this
    val Double.cm: Double get() = this / 100
    val Double.mm: Double get() = this / 1000
    val Double.ft: Double get() = this / 3.28084

    val oneInch = 25.4.mm
    println(“One inch is $oneInch meters”)
    // prints “One inch is 0.0254 meters”
    val threeFeet = 3.0.ft
    println(“Three feet is $threeFeet meters”)
    // prints “Three feet is 0.914399970739201 meters”

    原文地址:https://www.oschina.net/news/85013/swift-is-like-kotlin?from=groupmessage&isappinstalled=1

    展开全文
  • [Android]聊聊KotlinSwift

    千次阅读 2017-05-21 19:15:39
    Kotlin什么我们知道,Apple在2014年发布了全新开发语言Swift.经过三年多的发展, 这门语言已经发展到3.0版本,预计在今年6月会发布4.0版本,笔者从2.0开始关注它,期间踩过不上坑,不过到现在已经发展相对稳定,...

    Kotlin是什么

    我们知道,Apple在2014年发布了全新开发语言Swift.经过三年多的发展, 这门语言已经发展到3.0版本,预计在今年6月会发布4.0版本,笔者从2.0开始关注它,期间踩过不上坑,不过到现在已经发展相对稳定,现在很多公司包括大厂在自家的项目中应用到Swfit进行开发。

    那么Kotlin是什么鬼?它也是一种开源的编程语言,早就存在了,只不过大家对它不是很熟悉,哪怕做Android开发的程序员,知道的也很少。Google 2017 IO 大会上,Kotlin正式被选为作为Android开发的官方语言,Kotlin是一们基于JVM的语言,可以被编译成java的字节码, 跟java有很好的兼容性,而且相比于java更安全。

    Kotlin和Swift在语法上的比较

    自从Kotlin正式拿到门票后,大家应该对这门语言比较好奇,我也一样,刚好就看到了这篇国外人撰写的文章,国内热心人翻译了过来,简单的比较了一下Kotlin和Swift 的语言比较。作为学习和交流,我就转载过来了。

    原文链接
    本文只简单列出3个地方的相似性。具体大家参考原文,非常容易看懂。

    • Hello World
      Swift
    print("Hello,world!")

    Kotlin

    println("Hello,world!")
    • 常量和变量
      Swift
      常量用let
    var myVariable = 42
    myVariable = 50
    let myConstant = 42

    kotlin
    常量用val

    var myVariable = 42
    myVariable = 50
    val myConstant = 42
    
    • 数组

    Swift

    var shoppingList = ["catfish", "water",
        "tulips", "blue paint"]
    shoppingList[1] = "bottle of water"

    kotlin

    val shoppingList = arrayOf("catfish", "water",
        "tulips", "blue paint")
    shoppingList[1] = "bottle of water"
    展开全文
  • Swift 的语法与 Kotlin 有很多相似之处,整理了一些 SwiftKotlin 的对比,下面是一些例子,大家不妨来看看。可以说掌握了kotlin 再去看swift简直爽的不要不要的。学习成本大大降低。 Hello World 变量和...
        

    Swift 的语法与 Kotlin 有很多相似之处,整理了一些 Swift 和 Kotlin 的对比,下面是一些例子,大家不妨来看看。可以说掌握了kotlin 再去看swift简直爽的不要不要的。学习成本大大降低。


    Hello World

    6098829-4d08c650f6ab2981

    变量和常量

    6098829-e83c2400b3d14a12

    显式类型

    6098829-2cdc3477b0666fb2

    强制类型转换

    6098829-d37455d4ed070be1

    字符串插值

    6098829-415e45aab4cd3650

    范围操作符

    6098829-d123a430b72c106a

    区域操作符(for while等)

    6098829-553f2b5dbca376a7

    数组

    6098829-5f29dd502bf77758

    map

    6098829-7b9b09fcfa3b8d28

    空集合

    6098829-971ef99654154329

    函数

    6098829-405bf77a40e139bd

    返回值

    6098829-189bfc749cc6f5fc

    参数的变量数目

    6098829-e65e30c30287bb7d

    函数类型

    6098829-c2ea90cf7697d43b

    遍历

    6098829-46430900b311e3bb

    排序

    6098829-a3780661e5a12e49

    命名参数

    6098829-68202c8f0a758914

    类的声明

    6098829-a88811fa49c5bffa

    类的用法

    6098829-caee11085ad61ebf

    子类

    6098829-94923926b23e6156

    类型检查

    6098829-c5afc92810803813

    模式匹配

    6098829-bbf71e5aaf6211f5

    类型向下转换

    6098829-f1c370475cb2e50e

    协议

    6098829-df900490407080ef

    扩展

    6098829-d06ba5beaf01ba8f

    最后放一张完整的图

    6098829-0d95e1c214a4d2a1

    更多干货请看 公众号 Flutter那些事,精彩干货等你来看。

    展开全文
  • Kotlin, Android的Swift

    千次阅读 2016-02-27 16:55:32
    Kotlin, Android的Swift苹果已经用Swift代替Objective-C,一种古老的语言,来进行iOS的开发了。明显Android开发也有这个趋势。虽然现在已经可以选择Scala或者Groovy等基于JVM的语言开发Android应用来尝尝鲜,但是...
  • 圆栗子 发自 凹非寺 量子位 出品 | 公众号 QbitAI苹果的Swift和安卓开发者喜欢的Kotlin,是近年来增长最快的两种编程语言。据分析公司RedMonk统计,...
  • 行业分析公司Redmonk发布了2018年第一季度编程语言排行榜。排名靠前的编程语言应该不会让人吃惊,前5名分别是...排名4到9的编程语言分别是C++、CSS、Ruby和C,Swift和Objective-C排名第10。Swift的位次值得一提,特...
  • swift-is-like-kotlin, 比较Swift语法与 Kotlin Swift就像 Kotlin页面:http://nilhcem.github.io/swift-is-like-kotlin/以编程方式灵感来自于 swift-is-like-go,并从 swiftislikescala 视觉灵感灵感
  • Kotlin VS Swift

    千次阅读 2017-10-24 09:26:07
    对比KotlinSwift
  • 今天在CSDN首页偶然看到一个贴子JetBrains正式发布Kotlin 1.0:JVM和Android上更好用的语言 看完后,感觉Kotlin语法很简洁,有一系列动态语言的特点,Lambda表达式,高阶函数,闭包,甚至有静态语言的类型检查,最...
  • Kotlin 君和 Swift 君在一个团队一起开发已经很久了,由于平台的差异性,他们经常会进行一些技术上的交流(PK),「Kotlin vs. Swift」系列就是他们在互相切磋时的语录。内容会由简及深,慢慢深入。 技术漫谈 Swift:...
  • kotlin swift 重点 (Top highlight)The war for supremacy between iOS and Android has been going on for a decade now. Irrespective of the ecosystem you choose, developers can’t stop comparing the two ...
  • Kotlin-Android的Swift

    2015-12-02 23:52:39
    Kotlin-Android的SwiftKotlin介绍最近在学习一门新的语言Kotlin, Google拟用Kotlin取代Java进行Android开发。众所周知,苹果用swift取代Objective-C进行Swift开发,Kotlin对于Android就好比swfit对于iOS一样。不过...
  • 学习工具介绍:由于属于语法学习,本人推荐 Swift:在Xcode新建playground进行学习,直观迅速。 Kotlin:用官网上的在线编译工具即可. 传送门
  • 一位国外的程序员认为 Swift 的语法与 Kotlin 相似,并整理了一些 SwiftKotlin 的对比,下面是一些例子,大家不妨也看看。 BASICS Hello World Swift print("Hello, world!") Kotlin println("Hello, world...
  • Kotlin是一门与Swift类似的静态类型JVM语言,由JetBrains设计开发并开源。与Java相比,Kotlin的语法更简洁、更具表达性,而且提供了更多的特性,比如,高阶函数、操作符重载、字符串模板。它与Java高度可互操作,...
  • 为什么放弃java学习Kotlin

    万次阅读 多人点赞 2017-05-19 22:21:52
    Kotlin简介Kotlin早已是Android开发中的热门话题了,github上越来越多的项目是用Kotlin开发。但是这门语言一直处于很尴尬的境地,一方面自己不遗余力的挖掘自己在Android开发方面的作用,一方面却不是官方支持的语言...
  • Kotlin是个什么东东?

    2019-06-12 14:48:32
    仿佛是一夜之间,突然就好多人都在讨论Kotlin,一下子就把我弄懵逼了,这是个什么东东??然后各种百度,看各种大佬的文章,最后整理成了自己的理解。这篇文章就来简单讲一下,不对的地方,欢迎大家指出来。
  • 百度发音:kotlin——科特林 前言: 2017年5月18日 - 今天的I/O大会上谷歌宣布Kotlin正式成为Android的官方语言。 其实在去年我就开发在了解Kotlin,只是由于项目的原因,还有一些人根本不知道他的存在...
1 2 3 4 5 ... 20
收藏数 3,991
精华内容 1,596
关键字:

kotlin 为什么swift