First Program
New Xcode project, language selection Swift
Once the project is established, it can be run directly command+r.
Modify the program Viewcontroller.swift
////Viewcontroller.swift//H?ello Swift////Created by Xie Han Festival on 15/6/9.//Copyright (c) 2015 WHR. All rights reserved.//ImportUIKitclassViewcontroller:uiviewcontroller {OverrideFunc Viewdidload () {Super. Viewdidload () print ("Hello Swift")additional setup after loading the view, typically from a nib.}OverrideFunc didreceivememorywarning () {Super. Didreceivememorywarning ()//Dispose of any resources, can be recreated.}}
After running, output Hello Swift.
You can see that the overall framework is very similar to OBJECTC and the syntax is different. It is strange that every line is useless;
Variable and logical syntax variable definition
varA= the //Variant type, VAR is used to define variablesA= - Letconstantvariable= - //define a constantLeft double:double =Ten //Specify constant typeString(constantvariable)//Type conversion, which is much more convenient than OC LetMerge="The first one is \ (a)" the//string contains a valuevarlist=["a","B","C","D"]//Array definitionlist[1] ="B1" //array can be directly assigned to a value Letemptyarray=String[]()//define an empty array Letemptydictionary=dictionary<String,list> ()//define an empty dictionaryvarCannil:String? ="ABC" //Optional
Cycle
foriteminlist{}遍历词典和数组for (itemin array{ forinitem{ }}
Conditions
ifa>10 { //苹果为什么不搞和c java 类似的语法呢}ifa=b{}
Branch
switchstr{ case"a": case"b": default:}
Cycle
whilei<10{ i=i+1;}fori0...3{ print(i)}fori=0;i<3;++i{ print(i)}
Function
func hello(name:String)->String{ return"Hello \(name)"}
function allows nesting
Swift Learning notes a basic data type and simple statement syntax