How to get started with Apple's Swift programming language (Chinese version) 2. How to get started with swift
Apple Swift programming language tutorial [Chinese version]
Directory
1 Overview
2 Swift getting started
3 simple value
4 control flow
5. Functions and closures
6 objects and Classes
7 enumeration and Structure
Next article: Apple Swift programming language getting started tutorial [Chinese Version], last sharedKnowledge about getting started with Swift, followed by information about the simple value.
3 simple value
Use let to define constants and var to define variables. The constant value does not need to be specified during compilation, but must be assigned at least once. This means that you can use constants to name a value. You find that only one confirmation is required, but it is used in multiple places.
Var myVariable = 42
MyVariable = 50
Let myConstant = 42
Note
Gashero note
The constant definition here is similar to the variable in functional programming languages. It cannot be modified after a value assignment. It is beneficial to use more.
A constant or variable must belong to the same type as the value assignment. Therefore, you do not need to strictly define the type. A constant or variable can be created by providing a value, and the compiler can infer its type. In the above example, the compilation will infer that myVariable is an integer type, because its initial value is an integer.
Note
Gashero note
The type is bound to the variable name and belongs to the static type language. It facilitates static optimization. It is different from Python and JavaScript.
If the initialization value does not provide enough information (or there is no initialization value), you can write the type after the variable name, separated by a colon.
Let imlicitInteger = 70
Let imlicitDouble = 70.0
Let explicitDouble: Double = 70
Note
Exercise
Create a constant of the Float type and the value is 4.
Values are never implicitly converted to other types. If you want to convert a value to a different type, you must construct an instance of the desired type.
Let label = "The width is"
Let width = 94
Let widthLabel = label + String (width)
Note
Exercise
What error will you get when you try to delete the String Conversion of the last row?
There is also a simpler way to include a value in a string: Write the value in parentheses and place it in front of parentheses with a backslash. For example:
Let apples = 3
Let oranges = 5 // by gashero
Let appleSummary = "I have \ (apples) apples ."
Let fruitSummary = "I have \ (apples + oranges) pieces of fruit ."
Note
Exercise
Use () to include a floating point number to calculate the string, and contain someone's name to greet.
Create an array and dictionary using square brackets "[]", and access its elements through the index or key in square brackets.
Var shoppingList = ["catfish", "water", "tulips", "blue paint"]
ShoppingList [1] = "bottle of water"
Var occupations = ["Malcolm": "Captain", "Kaylee": "Mechanic",]
Occupations ["Jayne"] = "Public Relations"
To create an empty array or dictionary, use the initialization syntax:
Let emptyArray = String [] ()
Let emptyDictionary = Dictionary <String, Float> ()
If the type information cannot be inferred, you can write an empty array as "[]" and an empty dictionary as "[:]". For example, you can set a variable to be known and input parameters to the function:
ShoppingList = [] // go shopping and buy something by gashero
What development environment is used for Apple's new Swift language? Fees? When can I learn?
1. What is Swift?
Go to Baidu! Du Niang tells you that it is Apple's latest programming language, which is more efficient and simple than c, c ++, and objc. Able to develop ios and mac-related apps! It is a language that Apple will vigorously promote in the future!
2. What opportunities does Swift bring to you?
At the beginning, you thought it was too difficult for objc to learn about ios and give up pulling, or everyone in the it industry is engaged in android, and you are also engaged in android. Now you finally have the opportunity to start with ios! Swift legends are easy to learn. Do you want to increase your skills for android? Raise your salary? Come on!
3. Install the Swift environment!
1. You need an Apple Computer. (Small threshold)
2. Your system must be 10.9.3 or above. (Upgrade slowly)
3. You need to download xcode 6.0 beta. (Download it from the Developer Center! Threshold)
Xcode 6.0 beta
: Adcdownload.apple.com/..ta.dmg
The above three are required! Other diaosi systems such as xp, win7, and ubuntu have nothing to do with Swift. I love the threshold.
4. Simple Swift code writing and running!
Recently, Apple released a new programming language, Swift. Can anyone learn from me?
I like to learn it. I just sent it out. It must be a 0-level Foundation .... It's easy for people who know java to learn.