新建xcode项目,语言选择Swift
建立项目后可以直接Command+R运行。
修改程序ViewController.swift
//
// ViewController.swift
// H?ello swift
//
// Created by 谢厂节 on 15/6/9.
// Copyright (c) 2015年 WHR. All rights reserved.
//
import UIKit
class 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=50
let constantVariable=100 //定义一个常量
left double:Double = 10 //指定常量类型
String(constantVariable) //类型转换,这比OC方便多了
let merge="The first one is \(a)" //字符串中包含值
var list=["a","b","c","d"] //数组定义
list[1] = "b1" //数组可以直接赋值了
let emptyArray=String[]() //定义空数组
let emptyDictionary=Dictionary<String,list>() //定义空词典
var cannil :String? = "abc" //可选
for item in list{
}
遍历词典和数组
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)"
}
函数允许嵌套
原文地址:http://blog.csdn.net/xundh/article/details/46425305