zoukankan      html  css  js  c++  java
  • swift语言点评一

    一、变量定义

    1、常量与变量

    Use let to make a constant and var to make a variable.

    2、类型与推测

     However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type.

    3、类型转换与字符串

    Write the value in parentheses, and write a backslash () before the parentheses.

    1. let appleSummary = "I have (apples) apples."

    4、集合类型:

    1. let emptyArray = [String]()
    2. let emptyDictionary = [String: Float]()

    总结:(1)类型写在变量后,(2)类型可推断,(3)便捷的字符串化操作。

    二、控制流

    1、控制流综述

    Use if and switch to make conditionals, and use for-inwhile, and repeat-while to make loops.

    2、optional value ?

    An optional value either contains a value or contains nil to indicate that a value is missing.

    3、default value ??

    4、if let 判断是否为非空

    You can use if and let together to work with values that might be missing.

    5、if 后只能跟bool

    6、swich 可以使用任意值

    7、

    let x where x.hasSuffix("pepper")

    将switch值赋给x,如果复合范式,最终会赋给x

    where vegetable.hasSuffix可以用本体;

    8、for-in 使用了类型推断技术

    for (kind, numbers) in interestingNumbers 可以用于字典;

    总结:

    if的特别性、?、??、switch支持任意类型。

    三、函数与闭包

    1、函数形式

    func greet(person: String, day: String) -> String{}

    简化表示:()->(){}

    2、no argument label

    参量无名调用;向c++靠近;

    3、多输出函数

    func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int)

    4、高阶函数

    5、闭包及其简化。

    总结:

    Functions are a first-class type

    三、类

    1、类的初始化函数化

    1. var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")

    2、 属性:willSet and didSet getter and a setter

    3、 If the value before the ? is nil, everything after the ? is ignored and the value of the whole expression is nil.

    总结:类的使用函数化,属性的访问控制便捷化。

    四、枚举与结构

    1、类与结构的区别

    One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.

    2、枚举Use the init?(rawValue:) initializer

    Use the init?(rawValue:) initializer to make an instance of an enumeration from a raw value. It returns either the enumeration case matching the raw value or nil if there is no matching Rank

    if let convertedRank = Rank(rawValue: 3)

    3、枚举值的直接引用

    1. let hearts = Suit.hearts

    4、结构型枚举

    1. enum ServerResponse {
    2. case result(String, String)
    3. case failure(String)
    4. }
    5. let success = ServerResponse.result("6:00 am", "8:09 pm")
    6. let failure = ServerResponse.failure("Out of cheese.")

    五、协议与扩展

    与oc语言无区别;

    扩展可以针对任何类型。

    六、异常捕获

    Another way to handle errors is to use try? to convert the result to an optional. If the function throws an error, the specific error is discarded and the result is nil. Otherwise, the result is an optional containing the value that the function returned.

    1. let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
    2. let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")

    七、defer

    Use defer to write a block of code that is executed after all other code in the function, just before the function returns.

    八、范型

    <>

  • 相关阅读:
    Ajax_原生ajax写法、理解异步请求、js单线程+事件队列、封装原生ajax
    Ajax_实现动态网站的技术、php语法、php接口、前端渲染和后端渲染
    Ajax_Apache访问资源文件的权限配置、资源存放路径配置、配置虚拟主机、动态网站静态网站区别
    Ajax_Ajax、客户端、服务器端的认识与比较、服务器环境的搭建wamp
    jquery_jquery插件、jqueryUI使用方法、自定义jquery插件(一个插件其实就是一个功能)
    jquery_jquery事件绑定和解绑、链式编程、jquery中的主动遍历each、多库共存
    jquery_jquery动态创建元素及应用
    jquery_jquery动画
    jquery_jquery样式操作、链式编程
    jquery_认识jquery好处、选择器、小案例
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8675620.html
Copyright © 2011-2022 走看看