美文网首页
Swift 基本语法(一)— 值类型

Swift 基本语法(一)— 值类型

作者: Eddiegooo | 来源:发表于2019-09-30 23:29 被阅读0次

    Swift 与 OC 语言的区别

    1.Swift 面对协议编程、 面向函数编程、面向对象编程。 函数成了一等公民,可以当参数、返回值等。 闭包。
    2.Swift是一种类型安全的语言。
    3.值类型增强: 枚举、元组、结构体都是值类型。 平时的Int Double等数据类型也是用Struct实现,都是值类型。
    4.枚举功能增强。 可以是浮点型、字符串。 还可以拥有属性、方法。支持协议、扩展、泛型等。
    5.协议、扩展更丰富。 协议可以用于值类型。(枚举、结构体)

    Swift 的基本语法

    1.变量声明用: var、常量声明用: let
    可以在一行中定义多个常量、变量,用逗号隔开即可。


    常量变量.png

    2.Swift数值类型
    有8、16、32、64位之分。Int8、Int32; 但是可以通用Int,自动和当前平台位数一致。 可以用max 、min获取对应最大最小值。
    Double 有15位的精度,float 6位精度。 推荐使用Double。 Bool : true false。 和1、0是不用的。


    数值范围.png

    3.元组Tuple
    不管是可变类型还是常量类型,元组创建后,是不可以添加、删除元素的。
    修改值但是不能改变原类型。除非是any类型。
    元组也可以用于函数当参数、返回值。 用元组可以给一个函数返回多个返回值。

    //元组  可以定义多个数据类型, 用()包含, 可以指定名称,也可以省略,下面访问的时候有点区别。
    let errorMessage = (errorCode:404, errorMsg:"Not Found",   303, "reAddress")
    print(errorMessage.errorCode)  //访问第一个元素   404
    print(errorMessage.3)  //访问第四个元素   reAddress
    

    4.Swift的可选项 optional
    Swift里nil不是指针,是值缺失的一种特殊类型,任何类型的可选项都可以设置成nil.
    只有可选项可以赋值成nil,其它类型会报错。
    Optional 展开。 用!隐式展开。
    Optional 是Swift里的一个枚举类型. none = nil ; some = 具体的值

    //var name: String = nil  //非可选项赋值nil 报错。。
    var name: String? = nil
    var name1: String? = "Eddiegooo"
    var age: Optional<Int> = 3
    //使用
    let counts = name1.unsafelyUnwrapped.count   //这样可以不解包 没问题
    if name1 != nil {
        let count = name1?.count
    }
    

    5.Swift 字符串
    初始化。 多行字符串""" 三个双引号包裹。 特殊字符加\

    //初始化
    var name = "Swift Sting"
    var name1 = String()
    if name1.isEmpty {
        print("This string is empty")
    }
    var moreLineString = """
    This is more lines String。
       为了好看 我给他设置成几行显示;\
    在swift里是很方便的啊
    """
    print(moreLineString) //(在第二行末尾加上反斜杠,就不会自动换行了) 注意在定义字符串的时候,你怎么写空行、空格都是有效的
    //swift5 新特性  扩展字符串.  包裹在##里面,里面的特殊字符不起作用。 直接显示\n字符串
    var newString = #"new Sting \n in swift 5"#
    newString = #"new Sting \#n in swift 5"#   //加上与前后#号个数相同这样子里面的\n就起作用了。 
    

    字符串的可变性

    var welCome = "Hello"
    welCome += " World" //拼接字符串操作
    //遍历字符串字符
    for c in welCome {
        print(c)
    }
    //字符串插值操作
    let num = 6
    print("\(welCome) , age = \(num)")
    

    字符串索引

    var customString = "HelloWorld"
    print(customString[customString.startIndex])  //但是不可以使用证书下标访问[1]  报错
    //插入字符
    customString.insert("!", at: customString.endIndex)
    print(customString)
    customString.insert(contentsOf: " Welcome to China", at: customString.index(before: customString.endIndex))
    print("new Sting = \(customString)")
    customString.insert(contentsOf: " ", at: customString.index(customString.startIndex, offsetBy: 5))
    print(customString)
    //删除
    customString.remove(at: customString.index(before: customString.endIndex))
    let range = customString.index(customString.endIndex, offsetBy: -5)..<customString.endIndex
    customString.removeSubrange(range)
    print(customString)
    

    子字符串 subString。 和原字符串公用同一块内存。

    var welCome = "hello, world"
    var welCome1 = "world"
    
    let index = welCome.index(of: ",") ?? welCome.endIndex
    let subSting = welCome[..<index]  //注意 这是subSting类型
    print(subSting)
    let newString = String(subSting) //这就将subSting转化为Sting类型了
    //字符串比较
    print(welCome == welCome1)
    print(welCome.hasPrefix("hello"))
    print(welCome1.hasSuffix("ld"))
    

    未完下篇继续。

    相关文章

      网友评论

          本文标题:Swift 基本语法(一)— 值类型

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