zoukankan      html  css  js  c++  java
  • 变量&流程控制&函数

    Swift初步

    1、第一个swift程序。

      

      You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main function. You also don’t need to write semicolons at the end of every statement.

      不需要import任何库,像输入/输出之类的。代码直接写在全局,全局的第一行代码作为程序的entry point。 行尾也不需要冒号。

    2、Constant & Variable。

      

      let定义常量,var定义变量。若没指定类型,则根据value自动推导。若要强制指定类型,则按如下方式:

      

      swift中无隐式转换,若要将一种类型转换为另一种类型,必须显式转换,如下:

      

      转变为String类型,还可以使用()方法,如下:

      

    3、使用[]来创建array或dictionary。以及引用[]来访问array与dictionary中的元素。

      

      可以像如下方法一样创建空array或dictionary:

      

      

    4、流程控制语句包括if、switch、for-in、while、do-while。对condition的引用可以不加(),但对流程body的引用需要加{}。

       

      其中if的condition必须是带布尔运算符的表达式,不允许为一个int。

     5、optional变量,if&let

      

      if、let、?变量,三者共同作用。

    6、switch支持任意数据类型,以及支持任意的比较。switch的一个case支持多个value,另外每一个case内部不用写break。

      

    7、通过提供 (key,value)给for in,即可遍历dictionary:

      

    5、while语句

      

    6、使用 .. 来表示数列,注意是2个点。

      

    7、函数的定义与使用

       

      使用tuple可使函数返回多个值:

      

      支持变长参数:

      

      函数可以嵌套。嵌套的函数可以访问outer function中的局部变量。

      

    8、函数可以返回函数。

      

      函数可以作为参数传递:

      

    9、函数实际上是一种特殊的Closure,可以用{}来写一个匿名的Closure。使用in来分隔函数参数、返回类型与body。

      

      Closure可以写得更紧凑. When a closure’s type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement.

      

      作为函数最后一个参数的Closure可以在函数调用自的()之后定义,另外可以使用$N来引用参数:

      

  • 相关阅读:
    TensorFlow------学习篇
    汉语分词工具的研发-----
    SVM强化学习
    RNN和LSTM系统强化学习———
    CRF学习笔记
    Word2vector---------学习笔记
    FindAllAnagramsinaString
    Java中比较两个字符串是否相等的问题
    15003_特殊数字
    雷林鹏分享:Lua break 语句
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3783737.html
Copyright © 2011-2022 走看看