Swift语言特性及基本数据类型

Today ,苹果刚刚发布了Swift语言,我们来看下Swift的几个主要特性:

我们提供的服务有:成都网站设计、成都网站建设、微信公众号开发、网站优化、网站认证、东河ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的东河网站制作公司

        1         Safe

    • func configureLabels(labels: UILabel[]) {

    •    let labelTextColor = UIColor.greenColor()

    •    for label in labels {

    •        // label inferred to be UILabel

    •        label.textColor = labelTextColor

    •    }

    • }

    • 2        Modern

    • let cities = ["London", "San Francisco", "Tokyo", "Barcelona", "Sydney"]

    • let sortedCities = sort(cities) { $0 < $1 }

    • if let indexOfLondon = find(sortedCities, "London") {

         println("London is city number\(indexOfLondon + 1) in the list")

      }

      3        Powerful:

  • let size = (20, 40)

  • switch size {

  • case let (width, height) where width == height:

  •    println("square with sides\(width)")

  • case (1..10, 1..10):

  •    println("small rectangle")

  • case let (width, height):

  •    println("rectangle with width\(width) and height\(height)")

  • }

 

                  4            Interactive 

                  5              Fast。

-------------------------------------

基本数据类型:

 1 简单属性:

                  let:  恒定常量

                  var:可变常量

                        1.1 定义常量不用指定其固定类型,编译器会处理,如需更多识别,请添加前缀:

                        let implicitInteger = 70

                        let implicitDouble = 70.0

                        let explicitDouble: Double = 70

                        println("int:\(implicitInteger)  double:\(implicitDouble) explicitDoubledss:\(explicitDouble)");

2  类型之间的转化,必须是显示的:

                        let label ="width is :";

                        let width = 54;

                        let widthLabel = label + String(width)

                        println("LabelWidth:\(widthLabel)");

 3    使用反斜杠在String中插入内容,代码参考2 

 4   使用中括号创建数组和字典:

    

        var shoppingList = ["catfish","water","tulips","blue paint"]

        shoppingList[1] ="bottle of water"

        var occupations = [

           "Malcolm":"Captain",

           "Kaylee":"Mechanic",

        ]

        occupations["Jayne"] ="Public Relations"

        println("array is\(shoppingList)")

        println("dictionary is :\(occupations)")

        //create Empty 

        let emptyArray = String[]()

        let emptyDictionary = Dictionary()

下一篇文章将介绍控制语句


标题名称:Swift语言特性及基本数据类型
分享路径:http://scyanting.com/article/jisegs.html