
上QQ阅读APP看书,第一时间看更新
Type safety and type inference
Swift has a strong emphasis on types. Classes, enums, structs, protocols, functions, and closures can become types and be used in program composition.
Swift is a type-safe language, unlike languages such as Ruby and JavaScript. As opposed to type-variant collections in Objective-C, Swift provides type-safe collections. Swift automatically deducts types by the type-inference mechanism, a mechanism that is present in languages such as C# and C++ 11. For instance, constString in the following example is inferred as String during compile time, and it is not necessary to annotate the type:
let constString = "This is a string constant"