Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Kotlin 就像Swift ?看看 Kotlin 与 Swift大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

文章转载自 开源中国社区 [http://www.oschina.net] 链接:www.oschina.net/news/85013/swift-is-like-kotlin(点击尾部阅读原文前往) 原文:http://nilhcem.com/swift-is-like-kotlin/ 一位国外的程序员认为 Swift 的语法与 Kotlin 相似,并整理了一些 Swift 和 Kotlin 的
@H_489_6@
@H_489_6@

文章转载自 开源中国社区 @H_301_9@[http://www.oschina.net]

链接www.oschina.net/news/85013/swift-is-like-kotlin(点击尾部阅读原文前往)

原文:http://nilhcem.com/swift-is-like-kotlin/


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

Kotlin 就像Swift ?看看 Kotlin 与 Swift

BASICS

Hello World

Swift


print("Hello, world!")

Kotlin

println("Hello, world!") 

变量和常量

var @H_103_106@myVariable = 42

@H_755_116@myVariable = 50

let @H_755_116@myConstant 42

50

val @H_755_116@myConstant 显式类型

let explicitDouble: Double = 70


val explicitDouble: Double = 70.0

强制类型转换

let label = "The width is "

let width 94

let widthLabel = label + String(width)

val label "The width is "

val width 94

val widthLabel + width

字符串插值

let apples 3

let oranges 5

let fruitSumMary = @H_618_272@"I have (apples + oranges) " +

                   @H_618_272@"pieces of fruit."

val apples 3

val oranges 5

val fruitSumMary "I have ${apples + oranges} " 范围操作符

let names = @H_696_325@["Anna"@H_696_325@, "Alex""Brian""Jack"@H_696_325@]

let count names.count

for i in 0..

val names = arrayOf@H_696_325@()

val count .count()

for (i in 0..count - 1) {

    println(@H_618_272@"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)

for index in 1...5 @H_696_325@{

    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

for @H_696_325@(index in 1..5@H_696_325@) "$index times 5 is ${index * 5}"数组

var shoppingList "catfish""water"Box-sizing: border-Box !important; word-wrap: break-word !important; font-weight: inherit !important; color: rgb(51,

    @H_618_272@"tulips"Box-sizing: border-Box !important; word-wrap: break-word !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(51, @H_618_272@"blue paint"]

shoppingList[] "bottle of water"

val shoppingList )

映射

var Occupations [

    @H_618_272@"Malcolm": @H_618_272@"Captain""Kaylee""Mechanic"Box-sizing: border-Box !important; word-wrap: break-word !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(51,

]

Occupations[@H_618_272@"Jayne""Public relations"

val Occupations = @H_825_376@mutableMapOf@H_696_325@(

    @H_618_272@"Malcolm" to @H_618_272@"Kaylee" to @H_618_272@"Mechanic"

)

空集合

let emptyArray [String@H_696_325@]()

let emptyDictionary = [String: Float]()

val emptyArray ()

val emptymap = @H_643_351@mapOf()

FUNCTIONS

函数

func greet@H_696_325@(_ name: Box-sizing: border-Box !important; word-wrap: break-word !important; font-weight: bold !important; color: rgb(128,_ day) -> String @H_696_325@{

    return @H_618_272@"Hello (Name), today is (day)."

}

greet"Bob""Tuesday"

fun greet@H_696_325@(Box-sizing: border-Box !important; word-wrap: break-word !important; font-weight: inherit !important; color: rgb(0, )"Hello $name, today is $day."

元组返回

func getGasPrices@H_696_325@() -> @H_696_325@(DoubleBox-sizing: border-Box !important; word-wrap: break-word !important; font-weight: inherit !important; color: rgb(51, {

    return 3.59Box-sizing: border-Box !important; word-wrap: break-word !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(51, 3.693.79}

data class GasPrices@H_696_325@(val aBox-sizing: border-Box !important; word-wrap: break-word !important; font-weight: inherit !important; color: rgb(0, val bBox-sizing: border-Box !important; word-wrap: break-word !important; font-weight: inherit !important; color: rgb(0,

     val cDouble)

fun getGasPrices() = GasPrices参数的变量数目(Variable number Of Arguments)

func sumOfnumbersInt@H_696_325@...) Int @H_696_325@{

    var sum 0

    for number in numbers {

        sum += number

    }

    return sum

}

sumOf4259712

fun sumOf@H_696_325@(vararg 0

    for (number in numbers)

 

// sumOf() can also be written in a shorter way:

fun sumOf(vararg numbersInt) .sum函数类型

func @H_825_376@makeIncrementerInt {

    func addOnenumber-> Int {

        return 1 + number

    }

    return addOne

}

let increment = @H_643_351@makeIncrementer()

increment7

fun @H_825_376@makeIncrementer@H_696_325@(): @H_696_325@{

    val addOne = fun)}

val increment // makeIncrementer can also be written in a shorter way:

fun @H_643_351@makeIncrementernumber

let numbers [2019712@H_696_325@.@H_755_116@map { 3 * $0 }

val numbers = listOf* it 排序

var @H_103_106@mutableArray 1532@H_182_875@mutableArray.sort

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

命名参数

func areawidthheight{

    return width * height

}

areawidth: 2height3

fun area= width * height

area(width Box-sizing: border-Box !important; word-wrap: break-word !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(0, height // This is also possible with NAMEd arguments

area)

area(height Box-sizing: border-Box !important; word-wrap: break-word !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(0, width CLASSES

声明

class Shape var numberOfSides 0

    func simpleDescriptionString {

        return @H_618_272@"A shape with (numberOfSides) sides."

    }

0

    fun simpleDescription=

        @H_618_272@"A shape with $numberOfSides sides."

用法

var shape = Shape@H_696_325@()

shape.numberOfSides 7

var shapeDescription .simpleDescription子类

class NamedShape var numberOfSidesInt 0

    let nameString

 

    init{

        self.name = name

    }

 

    func simpleDescription}

 

class Square: NamedShape sideLengthDouble

 

    init.sideLength = sideLength

        super.initname)

        self4

    }

 

    func areaDouble {

        return sideLength * sideLength

    }

 

    override func simpleDescription"A square with sides of length " +

       sideLength + @H_618_272@"."

    }

 

let test = Square5.2"square"test.area

open class NamedShape0

 

    open fun simpleDescriptionclass Square(Bigdecimal:

        NamedShape{

    init {

        numberOfSides }

 

    fun areasideLength.pow)

 

    override fun simpleDescription"A square with sides of length $sideLength."

}

 

val test (Bigdecimal"5.2"),66); Box-sizing: border-Box !important; word-wrap: break-word !important;">类型检查

var @H_103_106@movieCount 0

var songCount 0

 

for item in library {

    if item is @H_643_351@movie {

        @H_755_116@movieCount += 1

    } else if item is Song {

        songCount 0

 

for (item in library{

    if (item is @H_417_237@movie{

        ++@H_755_116@movieCount

    } else if Song++songCount

    模式匹配

let nb 42

switch nb {

    case 0...789: print"single digit")

    case 10"double digits"11...99100...999"triple digits")

    default"four or more digits"

val nb 42

when nb{

    in 0..79 -> println)

    10 )

    in 11..99 100..999 )

    else 类型向下转换

for current in someObjects @H_696_325@{

    if let @H_755_116@movie = current as? @H_643_351@movie {

        print"Movie: '(movie.Name)', " +

            @H_618_272@"dir. (movie.director)")

    (current in someObjects@H_696_325@(current is {

        println"Movie: '${Current.namE}',224) !important;">    @H_618_272@"dir. ${Current.director}"协议

protocol Nameable @H_696_325@{

    func nameString

}

 

func fxT"Name is " x.name())

interface Nameable @H_696_325@{

    fun name()}

 

fun f扩展

extension Double km{ return self * 1_000.0 }

    @H_886_1682@m{ return self cm/ 100.0 @H_659_1030@mmft3.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"

val .kmDouble get= this * 1000

val .Double get= this

val = this 100

val 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"

小贴士:返回上一级搜索Kotlin、“@H_404_2472@Swift” 获取更多相关文章



●本文编号2384,以后想阅读这篇文章直接输入2384即可。

●输入m获取文章目录

推荐↓↓↓
 

Kotlin 就像Swift ?看看 Kotlin 与 Swift

安卓开发 

更多推荐15个技术类公众微信

涵盖:程序人生、算法与数据结构、黑客技术与网络安全、大数据技术、前端开发、Java、Python、Web开发、安卓开发、iOS开发、C/C++、.NET、Linux、数据库、运维等。

大佬总结

以上是大佬教程为你收集整理的Kotlin 就像Swift ?看看 Kotlin 与 Swift全部内容,希望文章能够帮你解决Kotlin 就像Swift ?看看 Kotlin 与 Swift所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
猜你在找的Swift相关文章