为什么Swift类型安全? [重复]

时间:2022-08-18 16:01:45

This question already has an answer here:

这个问题在这里已有答案:

As more-or-less a Ruby monoglot, this has never been clear to me... The answers to this question explain how type-safety functions in Swift (and generally, I am guessing), but doesn't answer 'why'? Surely there is some performance or security benefit, but what is it exactly? Or is the benefit purely the strictness of the value later on -- the developer has the benefit of knowing some function will definitely perform (.each on an Array, for instance) or when run.

作为一个或多或少的Ruby monoglot,这对我来说从来都不清楚......这个问题的答案解释了Swift中的类型安全功能(通常,我在猜测),但不回答'为什么' ?当然有一些性能或安全性好处,但究竟是什么呢?或者纯粹是后来的价值的严格性 - 开发人员的好处是知道一些函数肯定会执行(例如,对数组执行)或运行时。

What trade-offs am I unknowingly adopting when I do this in, say, Ruby

当我在Ruby中执行此操作时,我在不知不觉中采用了哪些权衡取舍

x = Float.new
x = 3
x = "a"
x = [a, "a", ['a'], {a: 'a'}]

versus what benefits am I receiving in it's equivalent in Swift:

与我在Swift中获得的相同的好处相比:

var v: String
var x = 3
var y = "a"
var z = [
  a, 
  "a", 
  ['a'], 
  ['a': 'a']
]

?

Apologies if this is too basic or redundant -- I didn't find a similar question searching SO.

如果这是太基本或多余的道歉 - 我没有找到类似的搜索SO的问题。

1 个解决方案

#1


3  

Type-Safety is not only helpful for run-time, but also for compile-time.
It can help sticking to certain rules, and making code more obvious.

Type-Safety不仅对运行时有用,而且对编译时也有用。它可以帮助坚持某些规则,并使代码更加明显。

Swift does still not check for types at run-time, but doing a check at compile-time will already help eliminating a lot of errors. In ObjC you could do something like this:

Swift在运行时仍然没有检查类型,但是在编译时进行检查已经有助于消除很多错误。在ObjC中你可以这样做:

NSString *string = (NSString *)@123;

Does it throw an error? No. Does it make sense to warn the developer when compiling? Of course!

它会引发错误吗?在编译时警告开发人员是否有意义?当然!

Type-safety can make sense in many ways, so let's look at some examples.

类型安全在许多方面都有意义,所以让我们看一些例子。


Examples

Beware, some of this is pseudo code

请注意,其中一些是伪代码

Remember using id for every possible thing? Those days lie in the past!

还记得每个可能的东西使用id吗?那些日子就在过去!

Which one is more obvious?

哪个更明显?

var numbers: Array<Int> = [
    1, 2, 3, 4
]

or

要么

NSArray *numbers = @[1, 2, 3, 4];

var fontAttributes: Dictionary<String, FontAttribute> = [
    Font.Weight : FontWeight.Bold,
    Font.Decoration : FontDecoration.None,
]

or

要么

NSDictionary *fontAttributes = @{
    Font.Weight : FontWeight.Bold,
    Font.Decoration : FontDecoration.None,
}

var predicate = Predicate<Person>({ person in person.name == "John" })

or

要么

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name == %@", @"John"];

#1


3  

Type-Safety is not only helpful for run-time, but also for compile-time.
It can help sticking to certain rules, and making code more obvious.

Type-Safety不仅对运行时有用,而且对编译时也有用。它可以帮助坚持某些规则,并使代码更加明显。

Swift does still not check for types at run-time, but doing a check at compile-time will already help eliminating a lot of errors. In ObjC you could do something like this:

Swift在运行时仍然没有检查类型,但是在编译时进行检查已经有助于消除很多错误。在ObjC中你可以这样做:

NSString *string = (NSString *)@123;

Does it throw an error? No. Does it make sense to warn the developer when compiling? Of course!

它会引发错误吗?在编译时警告开发人员是否有意义?当然!

Type-safety can make sense in many ways, so let's look at some examples.

类型安全在许多方面都有意义,所以让我们看一些例子。


Examples

Beware, some of this is pseudo code

请注意,其中一些是伪代码

Remember using id for every possible thing? Those days lie in the past!

还记得每个可能的东西使用id吗?那些日子就在过去!

Which one is more obvious?

哪个更明显?

var numbers: Array<Int> = [
    1, 2, 3, 4
]

or

要么

NSArray *numbers = @[1, 2, 3, 4];

var fontAttributes: Dictionary<String, FontAttribute> = [
    Font.Weight : FontWeight.Bold,
    Font.Decoration : FontDecoration.None,
]

or

要么

NSDictionary *fontAttributes = @{
    Font.Weight : FontWeight.Bold,
    Font.Decoration : FontDecoration.None,
}

var predicate = Predicate<Person>({ person in person.name == "John" })

or

要么

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name == %@", @"John"];