Swift基础

//
//  ViewController.swift
//  Swift基础运算符
//
//  Created by mibo02 on 16/12/21.
//  Copyright © 2016年 mibo02. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

   override func viewDidLoad() {
       super.viewDidLoad()
       //var 是变量 let 指常量
       //1.赋值运算符
       //2.数值运算
       let str = "hello," + "world"
       print(str)
       
       let contentHeight = 40
       let hasHeader = true
       var rowHeight = contentHeight
       if hasHeader {
           rowHeight = rowHeight + 50
       } else {
           rowHeight = rowHeight + 20
       }
       print(rowHeight)
       
       //区间运算符
       //1.闭区间运算符
       for index in 1...5 {
           //包含了1到5之间的所有数字
           print(index * 5)
       }
       
       //2.半闭区间
       let names = ["Anna","Alex","Brian","Fengfeng"];
       let count = names.count;
       for i in 0.. = ["TYO": "Tokyo", "DUB": "Dublin"]
       
       var airports = ["TYO": "Tokyo", "DUB": "Dublin"]
       //往字典中添加元素
       airports["fengfeng"] = "好人"
       print(airports)
       //删除字典中的元素
       airports["TYO"] = nil;
       print(airports)
       let removedValue = airports.removeValue(forKey: "DUB")
       print(airports)
       //字典初始化
       var namesOFIntegers = Dictionary()
       print(namesOFIntegers)
       //或者
       namesOFIntegers[16] = "six"
       namesOFIntegers = [:]
       
       //函数调用
       print(sayhello(personName: "fengfengaima"))
       print(halfOpenRangeLength(start: 4, end: 10))
       print(sayHelloWorld())
       print(sayGoodbye(personName: ""))
       
   }
   
   //函数
    func sayhello(personName:String) -> String {
       return "Hello, \(personName)"
   }
   //多参数函数
   func halfOpenRangeLength(start: Int, end:Int) -> Int {
       return end  - start
   }
   
   //无参函数
   func sayHelloWorld() -> String {
       return "hello world"
   }
   //没有返回值的函数
   func sayGoodbye(personName:String) {
       print("这个函数没有返回值")
   }
   
   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
   }


}

你可能感兴趣的:(Swift基础)