"The Analects of Confucius Weiling" has a classic dialogue: "Son Gong for benevolence." Zi Yue: 工欲善其事, its prerequisite. ......』。 For a programmer, a good tool does not necessarily produce high-quality code, but a good tool is self-evident in its ability to improve development efficiency. To use Swift as an iOS developer, the only option is Xcode6, where kids can get Xcode6 download links from the website below, and find official Swift books, sample codes and videos on this page.
Xcode6 Download: https://developer.apple.com/swift/resources/
Next we can start creating a project, Swift01:
The above see is the starter program "Hello, World". The next thing to do is to either build the entire project or simply run the project and view the results in the console, as shown in:
Well, it's all that simple, but we're definitely not going to talk about Hello World, so let's get a sense of Swift with the code below.
Import Foundationvar A:int = 123//Define a variable of type Int alet b = 321///define constant B by type inference to Intprintln ("\ (a) + \ (b) = \ (A + b)")/ /the \ () in the string can put variables, constants, expressions//output: 123 + 321 = 444//from 1 to 100 sum var = 0for var i = 1; I <= 100; ++i {//For loop sum + = I}println ("sum = \ (sum)")//output: sum = 5050for ch in "Hello" {// Each character of Hello is looped println (ch)}var randomage = arc4random ()% 50//Call the function in the foundation to generate a random number of print ("You" re \ (randomage) yea rs-old! ") var myage:uint32 = 33;if randomage > MyAge {//IF-ELSE branch structure println (" older Than me! ")} else {println ("Younger than me!")} Display the prompt according to the current system time//[tween] time and date operation in Apple's programming language is really troublesome var currdate = NSDate ()//Create Time Date Object var MyCal = Nscalendar (Calendaridentifier:nsgregoriancalendar)//Create calendar object (Gregorian Gregorian calendar)///The following two lines of code take out the hourly portion of the current time date var MyComp: nsdatecomponents = Mycal.components (Nscalendarunit.hourcalendarunit, fromdate:currdate) var hour = MYCOMP.HOURSWITCH Hour {case 7, 8:println ("Enjoy Your breakfast!") Case one, 13:println ("A Good lunch!") Case, 19:println ("It's a good time for dinner!") Default:println ("Do something to make yourself happy.")} var myarray:int[] = [12, 35, 96, 47, 5, 23, 68, 3, 68]myarray + = [5], 81]PRINTLN (myArray)//output: [+] 2, 81]//defines the function that finds the largest and smallest elements in the array//Declaration function is Func, and findmaxandmin is the parameter list in the () function name (), the parameter is an array of type int,//The return value is a tuple (compound value containing multiple data), The return value that contains two integers//functions is the Func findmaxandmin (x:int[]), which specifies its type after the list of function arguments (int, int) {var min:int = x[0] var max:int = x[0] for var index = 1; Index < X.count; ++index {if x[index] > max {max = X[index]} else if X[index] < min {min = X[index]}} retur N (min, max)}println (Findmaxandmin (MyArray))//Output: (5, 96)//define a dictionary type (key-value pair mapping) var mydic = [1001: "Apple", 1002: "Banana", 1003: "Grape"]for (no, name) in Mydic {println ("key = \ (no), value = \ (name)")}mydic[1003] = "Strawberry" println (mydic) Output: [1001:apple, 1002:banana, 1003:strawberry]var myFruit1 = mydic[1002]//Bananavar myFruit2 = mydic[2001]// Nilif MyFruit2 = = Nil {println (myFruit1)}else {println (myFruit2)}//the function as a function parameter (lambda function) func repeat (Count:int, Cal Lback: (), ()) {for i in 0..count {callback (); }}//Call repeat function output 10 times Hello, World!repeat ({println ("Hello, world!")})
Getting Started with swift development diagrams