美文网首页
Swift 4.0学习记录 ---> The Basics

Swift 4.0学习记录 ---> The Basics

作者: henu_Larva | 来源:发表于2018-01-22 16:01 被阅读3次
    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            
            /// Swift 4.0学习记录 ---> The Basics
            print(self.test1(str: "测试函数1_返回字符串"))
            self.testNoParams()
            self.specialString()
            self.testTypealias()
            self.testBool(isTrue: true)
            self.testTuples()
            self.testOptional()
            
            do {
                try throwAnError()
                // no error was thrown
            } catch {
                // an error was thrown
            }
            
            /// self .testAssert()
            self.testPreconditions()
        }
        
        /// 前置条件语句,作用类似断言
        func testPreconditions() {
            let index = 1
            precondition(index > 0, "Index must be greater than zero.")
        }
        
        /// 断言
        func testAssert() {
            let age = -3
            /// 判断等式是否成立,不成立时会打印后面的描述信息并终止程序运行
            assert(age >= 0, "A person's age can't be less than zero.")
        }
        
        /// 捕捉异常
        func throwAnError() throws {
            let arr:NSArray = NSArray()
            print(arr.firstObject!)
        }
        
        /// 可选类型
        func testOptional() {
            /// 可选类型在定义时,在类型后面添加‘?’,用以表明该对象可能为空
            /// 在使用可选类型对象时,可以通过强制解包(unwarp)用以表明使用该对象时确认其不为空
            
            let possibleString:String? = "An optional string"
            let forcedString:String = possibleString!
            print(forcedString)
            
            let assumedString:String! = "An implicitly unwarpped optional string"
            let implicitString:String = assumedString
            print(implicitString)
        }
        
        /// 元组的使用
        func testTuples() {
            let http404Error = (404,"Not found")
            let (statusCode,statusMsg) = http404Error
            print("The statusCode is \(statusCode),the error message is \(statusMsg)")
            
            /// or
            print("The statusCode is \(http404Error.0),the error message is \(http404Error.1)")
            
            /// or
            let (justStatusCode,_) = http404Error
            print("The status code is \(justStatusCode)")
            
            /// or
            let http200Status = (statusCode:200,description:"OK")
            print("The code is \(http200Status.statusCode),the message is \(http200Status.description)")
        }
        
        /// 布尔类型的使用及注意事项
        func testBool(isTrue:Bool) {
            if isTrue {
                print("isTrue")
            } else {
                print("isFalse")
            }
            
            // 下面是错误的分支判断方法(Object C 语法内可以使用此方式)
    //        let i = 1
    //        if i {
    //            print("is an error syntax")
    //        }
            
            // 如果要通过非布尔类型的数据进行分支判断,则应使用恒等式进行判断,如下:
            let j = 1
            if j == 1 {
                print("j equal 1")
            }
        }
        
        /// 类型替换
        func testTypealias() {
            typealias DJUInt8 = UInt8
            print("The max of UInt8 is \(DJUInt8.max),the min of that is \(DJUInt8.min)")
        }
        
        /// 特殊字符
        func specialString() {
            let cat = "🐱"
            print(cat)
        }
        
        /// 不带参数无返回值的函数
        func testNoParams() {
            var str:String = ""
            str = "1" + "2" /// 字符串拼接
            print(str)
            print("str === \(str)")
        }
        
        /// 带参数及返回值的函数
        func test1(str:String) -> String {
            
            return str
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:Swift 4.0学习记录 ---> The Basics

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