Playground編輯器模式
var string = “hello” + “” + “world”;
打印輸出: helloWorld
// (a…<b) :半閉區運算符。 (a…b) :閉區間運算符
for i in 0…<10 {
string += “(i)”;
}
打印輸出: helloWorld0123456789
for i in 0…<20 {
var j = i % 4;
}
打印輸出:20次 0123
加上類型標註
var string:String = “hello” + “” + “world”;
打印輸出: helloWorld
let color = UIColor.blue;
let attribStr = NSAttributedString(string: string ,attributes:[NSAttributedString.Key.foregroundColor:color, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 32)]);
Quick look 所支持的類型
1.顏色——UIColor類型的對象
2.字符串——包括無格式(String)和帶屬性的(NSAttributedString)
3.圖像———UIImage
4.視圖———各種視圖對象,例如UISlider,UIButton等
5.Array和Dictionary —— 列表顯示數組和字典對象;
6.Points、rects和sizes —— 點矩形和大小的信息
7.貝塞爾曲線———顯示所繪製的曲線
8.URLs ——— 雖然URL是一個鏈接地址,但通過Quick Look 可以查找該鏈接的實際內容
9.Class 和 Struct ——— 類和結構,在Quick Look中會顯示類和結構的屬性信息
《The Swift Programming Language》書籍
Product name :應用程序名稱
Organizetion Name :組織名稱
Organizetion Identifier :組織標識
Bundle Identifier : 項目標識
Language : 開發語言
Device : 設備
Use Core Data 是否支持在項目中使用數據庫特性
故事板包含兩個主要內容:場景(Scene)和過渡(Segue)
過渡類(UIStoryboardSegue)包含三個屬性:sourceViewController、destinationViewController和identifier
push segue (推送過渡)
model segue (模態過渡)
Pop-over segue (彈出過渡)
custom segue (自定義過渡)
計算器(calculator)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelResult: UILabel!
var firstOperand: Double = 0.0 // 第一操作數
var secondOperand: Double = 0.0 //第二操作數
var resultOperand: Double = 0.0 //值
var decimalPointFlag: Bool = false // 標記是否輸入了小數點
var isSecond: Bool = false // 是否輸入第二操作數
var operatorFlag: String = “” // 操作符
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func buttonTap(_ sender: UIButton) {
// labelResult 中默認是0,如果開始輸入數字,則先清除0
if labelResult.text == "0" || (isSecond && secondOperand == 0.0){
labelResult.text = "";
}
// 將用戶錄入的數添加到labelResult中
// labelResult.text = labelResult.text + sender.titleLabel
if let value =
sender.currentTitle{
if (isSecond && resultOperand > 0.0){
if secondOperand == 0 {
labelResult.text = "" + value;
}else
{
labelResult.text = labelResult.text! + value;
}
}else
{
labelResult.text = labelResult.text! + value;
}
}
if isSecond {
secondOperand = (labelResult.text! as NSString).doubleValue
}else
{
// 將labelResult.text中的字符串轉化爲單精度數
firstOperand = (labelResult.text! as NSString).doubleValue
}
// 在控制檯中輸出轉換完的值
print(firstOperand);
}
@IBAction func decimalPointTap(_ sender: UIButton) {
if !decimalPointFlag {
labelResult.text = labelResult.text! + ".";
}
if isSecond {
secondOperand = NSString(string: labelResult.text!).doubleValue;
}else
{
firstOperand = NSString(string:labelResult.text!).doubleValue;
}
decimalPointFlag = !decimalPointFlag;
}
@IBAction func operatorTap(_ sender: UIButton) {
if firstOperand != 0 {
isSecond = true;
decimalPointFlag = false
switch sender.currentTitle!{
case "+":
operatorFlag = "+"
case "-":
operatorFlag = "-"
case "×":
operatorFlag = "×"
case "÷":
operatorFlag = "/"
default:
operatorFlag = ""
}
}
}
@IBAction func resultTap() {
// 確保第二操作數有值
if isSecond {
// 除數不能爲0
if operatorFlag == "/" && secondOperand == 0 {
print("Error:除數不能爲0.")
return;
}
var result: Double = 0.0
switch operatorFlag {
case "+":
result = firstOperand + secondOperand;
case "-":
result = firstOperand - secondOperand;
case "×":
result = firstOperand * secondOperand;
case "/":
result = firstOperand / secondOperand;
default:
result = 0.0
}
labelResult.text = result.description;
print("第一操作數:\(firstOperand)");
print("操作符:\(operatorFlag)");
print("第二操作數:\(secondOperand)");
print("結果:\(result)");
resultOperand = result;
isSecond = true;
firstOperand = result;
secondOperand = 0.0;
}
}
@IBAction func clear() {
//label顯示爲0
labelResult.text = "0";
// 第一操作數清零
firstOperand = 0.0;
// 第二操作數清零
secondOperand = 0.0;
//小數點標記設置爲假
decimalPointFlag = false;
// 第二操作數標記設置爲假
isSecond = false;
// 操作符清空
operatorFlag = "";
resultOperand = 0.0;
}
}