Dave Diienno Dave Diienno - 1 year ago 90
Swift Question

Apple's Swift-Binding of Data Types

I have a fairly straightforward question about the binding of data types in the Swift programming language. I was surprised not to find any concrete information on the subject through my searches so I believe it would be best to bring my question to here for an in-depth answer:

  • How are data types bound in Apple's Swift programming language?

  • Are data types bound to variables and parameters at compile-time?

  • Are they bound to a run-time or a combination of the two?

Answer Source

Swift binds data types at compile time. If you are programming Swift using Xcode, you can find the type of any variable in your code by Option-clicking on that variable.

Here is an excerpt from The Swift Programming Language (Swift 3) (emphasis mine):

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download