zoukankan      html  css  js  c++  java
  • Swift基础之方法实战

    1.和之前一样

    2.代码

      1 //
      2 //  ViewController.swift
      3 //  SwitfLesson_exam
      4 //
      5 //  Created by 薛雨仑 on 14-10-7.
      6 //  Copyright (c) 2014年 Dylan. All rights reserved.
      7 //
      8 
      9 import UIKit
     10 
     11 class ViewController: UIViewController {
     12 
     13     override func viewDidLoad() {
     14         super.viewDidLoad()
     15         
     16         // 1.
     17         // Change Two temper
     18         // 2种元组的初始化
     19         var a = "Alice"
     20         var b = "Dylan"
     21         var myType = (a, b)
     22         
     23         var name:String
     24         var family:String
     25         (name, family) = ("Alice", "Dylan")
     26         
     27         // test func change
     28         swap(&a, with: &b)
     29         println(a)
     30         println(b)
     31         
     32         // 2.
     33         // print String
     34         // test func flexString
     35         println(flexStrings(s1:"Alice ", s2: "Dylan"))
     36         
     37         // 3.
     38         // sumAny
     39         // test func sumAny
     40         println(sumAny(1, 2, 3))
     41         
     42         // 4.
     43         // countFrom
     44         // test func countFrom
     45         countFrom(from: 1, to: 5)
     46     }
     47     
     48     /**
     49     1. fun change
     50     交换2个任意对象的值
     51     */
     52     func swap<T>(inout a:T, inout with b:T) {
     53         (a, b) = (b, a)
     54     }
     55     
     56     /**
     57     2. func flexStrings
     58     输出字符串
     59     */
     60     func flexStrings(s1:String = "", s2:String = "") ->String {
     61         return s1 + s2 == "" ? "none" : s1+s2
     62     }
     63     
     64     /**
     65     3. func sumAny
     66     */
     67     func sumAny(anys: Any...) ->String {
     68         return String(
     69             (anys.map({item in
     70                 switch item {
     71                 case "" as String, 0 as Int:
     72                     return -10
     73                 case let s as String where s.toInt() > 0:
     74                     return s.toInt()!
     75                 case is Int:
     76                     return item as Int
     77                 default:
     78                     return 0
     79                 }
     80             }) as [Int]).reduce(0, combine: {
     81                 $0 + $1
     82             })
     83         )
     84     }
     85     
     86     /**
     87     4. func countFrom
     88     */
     89     func countFrom(#from: Int, to:Int) {
     90         println(from)
     91         if from < to {
     92             countFrom(from: from + 1, to: to)
     93         }
     94     }
     95 
     96     override func didReceiveMemoryWarning() {
     97         super.didReceiveMemoryWarning()
     98         // Dispose of any resources that can be recreated.
     99     }
    100 
    101 
    102 }
  • 相关阅读:
    sp2010 升级sp2013 用户无法打开网站
    powerviot install in sharepoint 2013
    can not connect cube in performancce dashboard
    westrac server security configure user info
    添加报表服务在多服务器场
    sharepoint 2013 office web app 2013 文档在线浏览 IE11 浏览器不兼容解决方法
    delete job definition
    目前付款申请单内网打开慢的问题
    item style edit in sharepoint 2013
    Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
  • 原文地址:https://www.cnblogs.com/Dylan-Alice/p/Swift_Lesson4.html
Copyright © 2011-2022 走看看