苹果的Swift-数据类型的绑定
Apple's Swift-Binding of Data Types
我有一个关于 Swift 编程语言中数据类型绑定的相当简单的问题。我很惊讶没有通过搜索找到关于该主题的任何具体信息,所以我认为最好将我的问题带到这里以获得深入的答案:
- Apple 的 Swift 编程语言如何绑定数据类型?
- 数据类型是否在编译时绑定到变量和参数?
- 它们是绑定到 运行 时间还是两者的结合?
Swift 在编译时绑定数据类型。如果您使用 Xcode 编程 Swift,您可以通过 选项-单击该变量来查找代码中任何变量的类型。
以下是摘自
The Swift Programming Language (Swift 3)(强调我的):
Swift is a type-safe language. A type safe language encourages you to
be clear about the types of values your code can work with. If part of
your code expects a String, you can’t pass it an Int by mistake.
Because Swift is type safe, it performs type checks when compiling
your code and flags any mismatched types as errors. This enables you
to catch and fix errors as early as possible in the development
process.
Type-checking helps you avoid errors when you’re working with
different types of values. However, this doesn’t mean that you have to
specify the type of every constant and variable that you declare. If
you don’t specify the type of value you need, Swift uses type
inference to work out the appropriate type. Type inference enables a
compiler to deduce the type of a particular expression automatically
when it compiles your code, simply by examining the values you
provide.
我有一个关于 Swift 编程语言中数据类型绑定的相当简单的问题。我很惊讶没有通过搜索找到关于该主题的任何具体信息,所以我认为最好将我的问题带到这里以获得深入的答案:
- Apple 的 Swift 编程语言如何绑定数据类型?
- 数据类型是否在编译时绑定到变量和参数?
- 它们是绑定到 运行 时间还是两者的结合?
Swift 在编译时绑定数据类型。如果您使用 Xcode 编程 Swift,您可以通过 选项-单击该变量来查找代码中任何变量的类型。
以下是摘自 The Swift Programming Language (Swift 3)(强调我的):
Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code expects a String, you can’t pass it an Int by mistake.
Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.
Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every constant and variable that you declare. If you don’t specify the type of value you need, Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.