美文网首页
Swift基础

Swift基础

作者: 我想走走 | 来源:发表于2016-12-21 11:46 被阅读18次
    //
    //  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..<count {
               print("第\(i + 1)个人交 \(names[i])")
           }
           
           //逻辑与
           let first = true
           let second = false
           if first && second {
               print("成功了亲")
           } else {
               print("失败了亲")
           }
           //逻辑与
           let haseKey = false
           let hasValue = true
           if haseKey || hasValue {
               print("峰峰")
           } else {
               print("不好")
           }
           //组合逻辑
           //使用括号来明确优先级
           if (first && second) || haseKey || hasValue {
               print("你好,Man")
           } else {
               print("一点都不好对我")
           }
           /*
    字符串字面量可以包含以下特殊字符:
    转移特殊字符 \0 (空字符)、\\(反斜线)、\t (水平制表符)、\n (换行符)、\r (回车符)、\" (双引号)、\' (单引号)。
    单字节 Unicode 标量,写成 \xnn,其中 nn 为两位十六进制数。
    双字节 Unicode 标量,写成 \unnnn,其中 nnnn 为四位十六进制数。
    四字节 Unicode 标量,写成 \Unnnnnnnn,其中 nnnnnnnn 为八位十六进制数。
           */
           //双引号
           let wiseWords = "\"我是要成为海贼王的男人\" - 路飞"
           print(wiseWords)
           //初始化空字符串
           var emptyString = ""
           var anotherEmptyString = String()//初始化String实例
           emptyString = "峰峰"
           anotherEmptyString = "小蓉"
           if emptyString.isEmpty {
               print("这是一个空字符串")
           } else {
               print("....")
           }
           
           //字符串的可变性
           var variableString = "Horse"
           variableString += " and carriage"
           print(variableString)
          
           let yenSign: Character = "¥"
           
           //字符串可以通过相加减进行拼接
           //字符串插入值
           let multiplier = 3
           let message = "\(multiplier) times 2.5 is \(Double(multiplier)*2.5)"
           print(message)
           
           
           //比较字符串
           let quotation = "We're a lot alike, you and I."
           let sameQuotation = "We're a lot alike, you and i"
           if quotation == sameQuotation {
               print("我们两个是一样的")
           } else {
               print("我们两个是不一样的")
           }
           //前缀、后缀相等
           let romeoAndJuliet = [
               "Act 1 Scene 1: Verona,A public place",
               "Act 1 Scene 2: Capulet's mansion",
               "Act 1 Scene 3: A room in Capulet's mansion",
               "Act 1 Scene 4: A street outside Capulet's mansion",
               "Act 1 Scene 5: The Great Hall in Capulet's mansion",
               "Act 2 Scene 1: Outside Capulet's mansion",
               "Act 2 Scene 2: Capulet's orchard",
               "Act 2 Scene 3: Outside Friar Lawrence's cell",
               "Act 2 Scene 4: A street in Verona",
               "Act 2 Scene 5: Capulet's mansion",
               "Act 2 Scene 6: Friar Lawrence's cell" ]
           var act1 = 0
           for scene in romeoAndJuliet {
               if scene.hasPrefix("Act") {
                   act1 += 1
               }
           }
           print("There are \(act1) scenes in Act 1")
           
           
           //大写小写字符串
           let normal = "Could you help me, please?"
           let shouty = normal.uppercased() // shouty 值为 "COULD YOU HELP ME, PLEASE?"
           let whispered = normal.lowercased() // whispered 值为 "could you help me, please?"
           print(shouty)
           
           
           //数组
           var shopping = ["eggs","milk"]
           //判断数组是否为空
           if shopping.isEmpty {
               print("空")
           }else {
               print("非空")
           }
           //往数组末尾添加一个元素
           shopping.append("Flour")
           //或者
           shopping += ["Chocolate Spread", "Cheese", "Butter"]
           
           let removestring = shopping.removeFirst()
           print(removestring)
           
           
           //字典
           //var airports: Dictionary<String, String> = ["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<Int, String>()
           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基础

          本文链接:https://www.haomeiwen.com/subject/qdlqvttx.html