首页 > 代码库 > Swift学习——Language Guide 基础
Swift学习——Language Guide 基础
The Basics
Swift is a new programming language for iOS and OS X app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C.
Swift是用于开发IOS和Mac OS系统应用的新的编程语言,如果你曾经使用过C或者OC开发IOS程序,那你会发现他们和Swift之间有很多共同点
Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers; Double and Float for floating-point values; Bool for Boolean values; and String for textual data. Swift also provides powerful versions of the two primary collection types, Array and Dictionary, as described in Collection Types.
Swift提供了几种基本数据类型,Int,Double,Float, Bool, String, 还提供了集合 Array 和 Dictionary
Like C, Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values cannot be changed. These are known as constants, and are much more powerful than constants in C. Constants are used throughout Swift to make code safer and clearer in intent when you work with values that do not need to change.
和C一样,Swift提供了变量和常量
In addition to familiar types, Swift introduces advanced types not found in Objective-C. These include tuples, which enable you to create and pass around groupings of values. Tuples can return multiple values from a function as a single compound value.
与OC不同的是,Swift还提供了元组,这样函数可以有多个返回值
Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes. Optionals are safer and more expressive than nil pointers in Objective-C and are at the heart of many of Swift’s most powerful features.
Swift还提供了可选类型,对于可以为空的值可以进行判断,而且不一定要用于基础数据类型,也可以用于类(在OC中使用nil进行判断),可选类型(?标志)使得这个语言更加安全了
Optionals are an example of the fact that Swift is a type safe language. Swift helps you to be clear about the types of values your code can work with. If part of your code expects a String, type safety prevents you from passing it an Int by mistake. This enables you to catch and fix errors as early as possible in the development process.
可选类型还可以避免你的变量引起类型错误,让你更早的发现编程中的Bug