https://github.com/YouXianMing

swift版的元组

swift版的元组

 

说明

元组的内容并不多,使用的话跟普通变量类似,以下是测试源码:

//
//  ViewController.swift
//  Tuples
//
//  Created by YouXianMing on 15/10/12.
//

import UIKit

class ViewController: UIViewController {

    var tuplesValues : (duration : NSTimeInterval, animated : Bool)?
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        unnamedTuples()
        
        namedTuples()
        
        tuplesValues = (duration : 10, animated : true)
        if let _ = tuplesValues {
        
            print(tuplesValues!.duration)
            print(tuplesValues!.animated)
            
        } else {
        
        }
    }
    
    // Unnamed Tuples
    func unnamedTuples() {
    
        let tipAndTotalOne = (4.00, 25.19)
        let tipAndTotalTwo : (Double, Double) = (4.00, 25.19)
        
        print(tipAndTotalOne)
        print(tipAndTotalTwo)
    }
    
    // Named Tuples
    func namedTuples() {
    
        let tipAndTotalNamedOne = (tipAmt : 4.00, total : 25.19)
        print("\(tipAndTotalNamedOne.tipAmt) + \(tipAndTotalNamedOne.total)")
        
        let tipAndTotalNamedTwo : (tipAmt : Double, total : Double) = (4.00, 25.19)
        print("\(tipAndTotalNamedTwo.tipAmt) + \(tipAndTotalNamedTwo.total)")
    }
    
    // Returning Tuples
    func calcTipWithTipPct(tipPct:Double) -> (tipAmt : Double, total : Double) {
        
        let tipAmt     = tipPct * 6
        let finalTotal = tipAmt * 0.56
        
        return (tipAmt, finalTotal)
    }
    
    // animationOptionsWith
    func animationOptionsWith(duration : NSTimeInterval, animated : Bool) {
    
    }
}

 

细节

 

posted @ 2015-10-12 21:32  YouXianMing  阅读(276)  评论(0编辑  收藏  举报