swift学习之闭包,函数

//
//  ViewController.swift
//  swiftTest
//
//  Created by wupeng on 15/11/23.
//  Copyright © 2015年 wupeng. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let p = Person(name:"wup",numberOfLines:999999)
        let pSimpleDesc = p.simpleDesc();
        print(pSimpleDesc)
        let p2 = Person(name: "nash", numberOfLines: 10000000)
            print(p2.parrams)
        p2.parrams = "nash131313131"
        print(p2.parrams)
        let mathfunc = self.getMathFunc("squre")
        print(mathfunc(5))
        print(self.squre(3))
        print(self.cube(100))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//    func sequre(num:Int) -> Int {
//        return num * num
//    }
//    func cube(num:Int) -> Int {
//        return num * num * num
//    }
//    func getMathFunc(type:String ) -> (Int) ->(Int) {
//        switch (type) {
//        case "sequre":
//            return self.sequre
//        default:
//            return self.cube
//        }
//    }
    func getMathFunc(type:String) -> (Int) ->Int {
        switch type{
            case "squre":
                return  { (num:Int) -> Int in return num * num }
        default:
            return {(num:Int) -> Int in return num * num * num }
        }
    }
    var squre:(Int) -> Int = {(num:Int) -> Int in return num * num }
    var cube :(Int) -> Int = {
        return $0 * $0 * $0
    }
//    func getMathFunc(type:String) -> (Int) ->Int {
//        func squre(num: Int) -> Int {
//            return num * num
//        }
//        func cube(num :Int) -> Int {
//            return num * num * num
//        }
//        switch type {
//        case "squre":
//            return squre
//        default:
//            return cube
//        }
//    }
    func testFunc(name:String , day:String) ->String {
        return "first arg :\(name) and SeconeString :\(day)"
    }
    func sumOf(name:String,numbers:Int...) -> Int {
        var sum = 0
        for number in numbers {
             sum += number
        }
        print(name)
        return sum
    }
    internal func test() {
        let apples = 3;
        let orange = 5;
        let appleSumm = "i have \(apples + orange) apples"
        print(appleSumm)
        
        var shoppingList = ["fish","dog","cat"]
        shoppingList[1] = "preson"
        print(shoppingList)
        var occupations  = ["MalColm":"Person",
            "kaylee":"Cat",]
        print(occupations["MalColm"])
        //        let emptyArray = String[]()
        let emptyDictionary = Dictionary<String,Float>()
        
        //        print(emptyArray)
        
        print(emptyDictionary)
        
        
        let scores = [76,393,3939,39392,2323,1100,8383]
        
        for  score in scores {
            print(score);
        }
        var optionalString:String? = "Hi"
        optionalString = nil
        if optionalString != nil {
            print(optionalString)
        }
        let vagetable = "Red Paper"
        switch vagetable {
        case "apple":
            print("apple")
        case "custome","Oh No":
            print("two")
        case let x where x.hasSuffix("Paper"):
            print("\(x)")
        default:
            print("default")
            
        }
        var m = 2
        repeat {
            m = m*2
            print(m)
        } while m < 100
        
        var n = 1024
        while n > 0 {
            n = n/2
            print(n)
        }
        
    }
}

转载于:https://my.oschina.net/wupengnash/blog/534235

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值