zoukankan      html  css  js  c++  java
  • swift

    import Cocoa


    var str = "Hello, playground" + " and hyt"


    var i=10


    var f=5.0


    f=f+5.0


    let fruits = ["apple", "banana", "orange", "cherry"]


    for some in fruits {
        println("(some)")
        some
    }


    fruits.count
    var another = fruits.map({(some:String) -> String in return some+" 3"})
    another


    func sayHello(name:String) -> String {
        return "Hello (name)."
    }


    sayHello("hyt")


    fruits.map(sayHello);


    var sum = 0
    for num in 1..100 {
        sum += num
    }


    sum


    func getfunc() -> ((Int, Int)->Int) {
        func add(a:Int, b:Int)->Int{ return a+b}
    return add
    }


    getfunc()(1,2)


    var a = [1,2,3,"hyt",2.1]
    a.count
    a.className


    class Phone {
        var type=""
        func what()->String{
    return "I'm iPhone(type)."
        }
    }
    var iPhone = Phone()
    iPhone.type = "4S"
    iPhone.what()


    class Bala : Phone{
        init(type:String){
            super.init()
            self.type=type
        }
    }


    var bala = Bala(type:"5S")
    bala.what()


    protocol ExampleProtocol {
        mutating func adjust()
    }


    extension Double: ExampleProtocol {
        mutating func adjust(){
            self+=1
        }
    }


    var db: Double = 1.10


    db.adjust()
    db


    // simple pattern matching
    let (x, y) = (1,2)
    x
    var dd = x+1


    let (_, z) = ("hyt", "bob")
    z


    str = "hyt/bob".uppercaseString
    str.pathComponents


    str = "HYT.BOB".lowercaseString
    var names = str.componentsSeparatedByString(".")
    names += "app"
    names += ["Anna", "git"]
    str = names[2]
    names.isEmpty
    names = names.reverse()


    str = names.reduce(str, {(a:String, some:String)-> String in return a+","+some})
    str
    str = ""
    names.reduce(str, +)


    var nums = [1,2,3]


    var num = 0;
    nums.reduce(num, +)

    来自:http://www.cocoachina.com/bbs/read.php?tid=204774

  • 相关阅读:
    setprecision和setiosflags函数
    free和malloc的使用
    栈内存与堆内存
    一个教科书一般的“空指针”错误
    孙鑫教程第三章小测试程序
    向函数传递结构体信息
    指针(二)
    指针(一)
    cocos2d场景和UIViewController视图的切换
    UIScrollView 原理详解
  • 原文地址:https://www.cnblogs.com/Songxin/p/3777080.html
Copyright © 2011-2022 走看看