斯威夫特山地车_斯威夫特vsKotlin-哪个更好?

斯威夫特山地车

I have been using Swift for the last few years and Kotlin for a few months now. Recently while working, I have been finding myself thinking, “Why doesn’t the other language work like this?”. This question prompted me to write this story and figure out which one is better. Now, when I ask the question “Which is better?”, I am not necessarily looking at which is faster or more performant. I am looking more at which has a more pleasant experience to use.

最近几年我一直在使用Swift,而几个月前我一直在使用Kotlin。 最近在工作时,我发现自己在想:“为什么其他语言不能这样工作?” 这个问题促使我写这个故事并弄清楚哪个更好。 现在,当我问“哪个更好?”这个问题时,我并不一定要看哪个更快或更高效。 我正在寻找更多具有更愉快使用体验的产品。

The experience of using a language is, of course, subjective. I would be very interested to see what other people think about the comparisons that I make. I also want to say that this will by no means cover everything to do with the languages. This story is more of a quick rundown of the differences and similarities of the bits I use the most.

使用语言的经验当然是主观的。 我很想知道其他人对我所做的比较有何看法。 我还想说,这绝不会涵盖与语言有关的所有内容。 这个故事更多是关于我最常使用的位的异同的快速总结。

变数 (Variables)

Let’s start with variables. In Swift you have var and let, and in Kotlin you have var and val. In both languages var is the keyword for a mutable variable, let and val are the keywords for immutable values.

让我们从变量开始。 在Swift中,您具有varlet ,在Kotlin中,您具有varval 。 在两种语言中, var是可变变量的关键字, letval是不可变值的关键字。

I think Swift has Kotlin beat here. This is purely down to the fact that when quickly reading through code, var and val look very similar. With var and let I can easily see what is what without having to read too closely.

我认为Swift在这里击败了Kotlin。 这纯粹是因为在快速阅读代码时, varval看起来非常相似。 使用varlet我可以很容易地看到什么是什么,而不必仔细阅读。

Winner: Swift

获奖者:斯威夫特

打印 (Print)

Both languages use the print function to output data. However, they both use different syntax to output variables.

两种语言都使用print功能来输出数据。 但是,它们都使用不同的语法来输出变量。

// Swift
print("Data: \(myVariable)")
print("Data: \(myObject.data)")// Kotlin
print("Data: $myVariable")
print("Data: ${myObject.data}")

I much prefer the Kotlin syntax here, it is easier to type and cleaner to read.

我在这里更喜欢Kotlin语法,它更易于键入且更易于阅读。

Winner: Kotlin

优胜者:Kotlin

有条件的 (Conditionals)

Let’s have a look at two very basic if statements.

让我们看一下两个非常基本的if语句。

// Swift
if nameVar == "Swift" {}// Kotlin
if (nameVar == "Kotlin") {}

The only difference between these is that in Swift, you don’t need the brackets. You can use brackets in Swift, but they are not enforced. Some people prefer using brackets, some don’t, personally I am indifferent to them.

两者之间的唯一区别是,在Swift中,您不需要括号。 您可以在Swift中使用方括号,但不强制使用。 有些人喜欢使用括号,有些人则不喜欢,就我个人而言,他们对此并不在乎。

Sometimes when programming, you want a nice one-line conditional for something where an if statement would be overkill. For example, when assigning a value to a variable. For this, you typically use the ternary operator. Swift has this operator, but Kotlin doesn’t. However, in Kotlin, you can get around this with a one-line if statement.

有时在编程时,您需要一个不错的单行条件来处理if语句会过分使用的情况。 例如,在为变量分配值时。 为此,通常使用三元运算符。 Swift具有此运算符,但Kotlin没有。 但是,在Kotlin中,您可以使用单行if语句解决此问题。

// Swift
let swift = hasTernary ? "yes" : "no"// Kotlin
val kotlin = if (hasTernary) "yes" else "no"

The ternary operator makes the Swift code cleaner and a bit more concise.

三元运算符使Swift代码更简洁一些。

Winner: Swift

获奖者:斯威夫特

切换语句 (Switch Statements)

You are probably thinking “Why is this not in the conditionals section?”. Well, there is a big difference between these statements in Swift and Kotlin, so I thought it deserved its own section.

您可能在想“为什么不在条件部分?”。 好吧,Swift和Kotlin中的这些语句之间有很大的区别,所以我认为它应该放在自己的部分。

Let’s have a look at these statements side by side.

让我们并排看一下这些语句。

// Swift
switch ourNumber {
case 1:
print("One")
case 2, 3:
print("Maybe Two")
print("Maybe Three")
default:
print("Not One, Two, or Three")
}// Kotlin
when (ourNumber) {
1 -> print("One")
2, 3 -> {
print("Maybe Two")
print("Maybe Three")
}
else -> print("Not One, Two, or Three")
}

The first thing you have probably noticed is that in Kotlin, a switch statement uses the when keyword. I think that the Swift code is cleaner, however, I prefer the Kotlin syntax for this. I like the arrow notation and how natural it feels to write. I also prefer how in Kotlin you have a clear distinction between a case with an expression and one with a block.

您可能注意到的第一件事是在Kotlin中,switch语句使用when关键字。 我认为Swift代码更简洁,但是我更喜欢Kotlin语法。 我喜欢箭头符号,喜欢写的感觉。 我还希望在Kotlin中如何区分带表达式的案例和带块的案例。

Winner: Kotlin

优胜者:Kotlin

循环 (Loops)

First, let’s look at for in loops as these are what I use the most.

首先,让我们看一下in循环,因为这是我最常使用的循环。

// Generic array - not language specific
numbers = [1, 2, 3]// Swift
for number in numbers {
print(number)
}// Kotlin
for (number in numbers) {
print(number)
}

Like with the if statements, the only real difference here is that you don’t need to use brackets in Swift.

就像if语句一样,这里唯一真正的区别是您不需要在Swift中使用方括号。

Now, what if we want the index of the array as well?

现在,如果我们还需要数组的索引怎么办?

// Swift
for (number, index) in numbers.enumerated() {
print("\(number) - \(index)")
}// Kotlin
for ((index, number) in numbers.withIndex()) {
print("$number - $index")
}

Again, these are very similar. However, this is now where we start seeing how enforcing brackets can make code more difficult to read.

同样,这些非常相似。 但是,现在这是我们开始看到的括号如何使代码更难阅读。

Both languages are also pretty much the same when it comes to looping for x amount of times.

循环x次数时,两种语言也几乎相同。

// Swift
for index in 1...20 {
print(index)
}// Kotlin
for (index in 1..20) {
print(index)
}

Both of these loops will print the number one through twenty. The only real difference being Swift uses three dots, and Kotlin uses two.

这两个循环都将打印数字1到20。 唯一真正的区别是Swift使用三个点,而Kotlin使用两个点。

While loops have the same story.

While循环具有相同的故事。

// Swift
while minute < 60 {}// Kotlin
while (minute < 60) {}

As you can see, both Swift and Kotlin handle loops in very similar ways.

如您所见,Swift和Kotlin都以非常相似的方式处理循环。

Winner: Draw

优胜者:平局

数组 (Arrays)

Arrays can be as simple as a one-dimensional list of numbers or as complex as a multi-dimensional assortment of objects. Let’s have a look at how Swift and Kotlin handle some basic arrays.

数组可以像一维数字列表一样简单,也可以像对象的多维分类一样复杂。 让我们看一下Swift和Kotlin如何处理一些基本数组。

// Swift
let numbersArray = [1, 2, 3]
let keyValueArray = [
["key1": "value1", "key2": "value2"],
["key1": "value3", "key2": "value4"],
["key1": "value5", "key2": "value6"]
]
var emptyNumbersArray = [Int]()
var emptyKeyValueArray = [[String: String]]()print(numbersArray[0]) // 1
print(keyValueArray[0]) // ["key1": "value1", "key2": "value2"]
print(keyValueArray[0]["key1"]) // value1
print(emptyNumbersArray) // []
print(emptyKeyValueArray) // []// Kotlin
val numbersArray = listOf(1, 2, 3)
val keyValueArray = listOf(
mapOf("key1" to "value1", "key2" to "value 2"),
mapOf("key1" to "value3", "key2" to "value 4"),
mapOf("key1" to "value5", "key2" to "value 6")
)
var emptyNumbersArray = listOf<Int>()
var emptyKeyValueArray = listOf<Map<String, String>>()print(numbersArray[0]) // 1
print(keyValueArray[0]) // {key1=value1, key2=value 2}
print(keyValueArray[0]["key1"]) // value1
print(emptyNumbersArray) // []
print(emptyKeyValueArray) // []

I prefer the syntax Swift uses for arrays. I find that it is cleaner, especially when creating empty arrays. I also like that is less wordy, I find this makes it quicker to type and easier to understand where in the arrays the data types correspond to.

我更喜欢Swift用于数组的语法。 我发现它更干净,尤其是在创建空数组时。 我也很喜欢这样做,因为我发现这样做可以更快地键入数据,并且更容易理解数据类型在数组中的对应位置。

Winner: Swift

获奖者:斯威夫特

线程数 (Threads)

If you are using Swift and Kotlin for app development, then you probably find yourself moving between threads fairly regularly. Let’s have a look at how each language handles moving to an async thread and running code on the main thread.

如果您使用Swift和Kotlin进行应用程序开发,那么您可能会发现自己定期地在线程之间移动。 让我们看一下每种语言如何处理移至异步线程并在主线程上运行代码的情况。

// Swift
DispatchQueue.global(qos: .userInitiated).async {
// Run some async task
DispatchQueue.main.async {
// Run some task on the main thread
}
}// Kotlin
val thread = Thread {
// Run some async task
Handler(Looper.getMainLooper()).post {
// Run some task on the main thread
}
}
thread.start()

There are multiple ways of moving between threads and other ways of running async code in both languages. The examples above are what I have found to be the most succinct ways of running code on another thread and running code on the main thread.

两种语言都有多种在线程之间移动的方式以及其他运行异步代码的方式。 上面的示例是我发现的在另一个线程上运行代码和在主线程上运行代码的最简洁的方式。

I prefer how Swift handles this, the syntax is clearer and more descriptive as to what is going on.

我更喜欢Swift的处理方式,它对正在发生的事情的语法更清晰,更具描述性。

Winner: Swift

获奖者:斯威夫特

结论 (Conclusion)

Let’s review the categories and which language won what.

让我们回顾一下类别以及哪种语言赢得了什么。

Variables: Swift

变量:Swift

Print: Kotlin

打印:Kotlin

Conditionals: Swift

有条件的:斯威夫特

Switch Statements: Kotlin

转换声明:Kotlin

Loops: Draw

循环:绘制

Arrays: Swift

数组:Swift

Threads: Swift

线程:斯威夫特

There we have it, Swift won more of the categories. This definitely reflects my experiences developing with both languages. I find that I always enjoy writing Swift code more than Kotlin code. Swift has a nice flow to it which I find makes it quick to write, read, and debug.

有了我们,Swift赢得了更多类别。 这肯定反映了我使用两种语言进行开发的经验。 我发现我总是比编写Kotlin代码更喜欢编写Swift代码。 Swift具有很好的流程,我发现它使编写,读取和调试变得很快。

You may be wondering what the point was to this comparison, especially as the majority of users of each language are using them because they are either developing for iOS or Android. However, don’t forget that both Swift and Kotlin can be used for server-side programming as well.

您可能想知道进行这种比较的目的是什么,特别是由于每种语言的大多数用户都在使用它们,因为他们正在为iOS或Android开发。 但是,请不要忘记,Swift和Kotlin都可以用于服务器端编程。

Let me know what you prefer and why. It would be interesting to hear other peoples views.

让我知道您的喜好以及原因。 听到其他人的意见会很有趣。

翻译自: https://levelup.gitconnected.com/swift-vs-kotlin-which-is-better-696222a49a34

斯威夫特山地车

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值