把語言化爲行動,比把行動化爲語言困可貴多。 —— 高爾基ios
———————————————————————————————————————————程序員
另附連接:這是一些基本的動做用法(本身下載在電腦上運行)小程序
百度雲連接:http://pan.baidu.com/s/1pK8BUbTswift
連接來了:http://www.oschina.net/translate/best-swift-tutorials-with-examples數組
這是程序員基礎一個語言時第一個程序,哈哈,如今看到這個都以爲親切。框架
import Foundation ide
var str = "HelloWorld" 函數
print(str) spa
第一句:import Foundation.net
表示引入Foundation框架。什麼是Foundation框架?
Foundation是OS X和iOS應用程序開發的基礎框架,它包括了一些基本的類,如數字、字符串、數組、字典等。
Cocoa框架是OS X開發須要幾本框架它包括:AppKit和Foundation框架。UIKit框架是iOS圖形用戶界面開發須要框架,包括常見的視圖和視圖控制器等。
第二句:var str ="Hello World"
聲明str變量,var表示聲明變量。
第三句:print (str)
print(_:)是一個函數,可以將變量或常量輸出到控制檯。
首先你要選擇新建一個項目(即第二項):
主要程序ViewController.swift(主要代碼實現要寫在 XX.swift文件中)內容以下:
設計的界面圖:
這是計算結果:
import UIKit
extension String
{
// subscript operator override
subscript(index:Int) -> Character?
{
var cur = 0
for c in self {
if cur == index {
return c
}
}
// return nil
let ret:Character?
return ret
}
}
class ViewController: UIViewController {
var operand1: Int = 0; // left operand
var operand2: Int = 0; // right operand
var operator: Character = "#"; // operator:+-*/=
@IBOutlet var resultLabel : UILabel = nil // output result
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func onClick(sender : UIButton) {
println("Click" + sender.titleForState(UIControlState.Normal));
var label = sender.titleForState(UIControlState.Normal);
var c:Character = label[0]!
switch c{
case "+","-","*","/":
operator = c
case "=":
var result = 0
switch operator {
case "+":
result = operand1 + operand2
case "-":
result = operand1 - operand2
case "*":
result = operand1 * operand2
case "/":
result = operand1 / operand2
default:
break
}
resultLabel.text = "\(result)"
// clear status
operator = "#"
operand1 = result
operand2 = 0
break
default:
if operator=="#" {
let tmp = label.toInt()!
operand1 = operand1*10 + tmp
resultLabel.text = "\(operand1)"
}
else {
let tmp = label.toInt()!
operand2 = operand2*10 + tmp
resultLabel.text = "\(operand2)"
}
}
}
// 其實這個ACTION能夠不單獨提出來,都放到ONCLICK函數裏處理
@IBAction func clearClick(sender : UIButton) {
operand1 = 0
operand2 = 0
operator = "#"
resultLabel.text = "0"
}