Swift 学习笔记一 基本数据类型和简单语句语法

第一个程序

新建xcode项目,,语言选择Swift

建立项目后可以直接Command+R运行。 修改程序ViewController.swift

UIKitclass ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()print(“Hello Swift”)// Do any additional setup after loading the view, typically from a nib.}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}

运行后,输出 Hello Swift.

可以看到整体框架与ObjectC很相似,语法有所不同。奇怪的是每行没有用;结尾。

变量及逻辑语法变量定义var a=42//变体类型,var用来定义变量a=left double:Double = (constantVariable) list=[“a”,”b”,”c”,”d”] //数组定义list[emptyArray=cannil :String? = “abc” //可选循环{}遍历词典和数组for (item,numbers) in array{for opt in item{}}条件if a>10 { //苹果为什么不搞和c java 类似的语法呢}if let a=b{}分支 switch str{case “a”:case “b”:default:}循环while i<10{i=i+1;}for i in 0…3{ print(i)}for var i=0;i<3;++i{ print(i)}函数func hello(name:String)->String{ return “Hello \(name)”}

函数允许嵌套

快乐要懂得分享,才能加倍的快乐

Swift 学习笔记一 基本数据类型和简单语句语法

相关文章:

你感兴趣的文章:

标签云: