Swift基础之方法实战

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1.和之前一样

2.代码

1//2//ViewController.swift3//SwitfLesson_exam4//5//Created by 薛雨仑 on 14-10-7.6//Copyright (c) 2014年 Dylan. All rights reserved.7//89import UIKit1011classViewController: UIViewController {1213overridefunc viewDidLoad() {14super.viewDidLoad()1516//1.17//Change Two temper18//2种元组的初始化19var a ="Alice"20var b ="Dylan"21var myType =(a, b)2223var name:String24var family:String25(name, family) = ("Alice","Dylan")2627//test func change28swap(&a, with: &b)29PRintln(a)30println(b)3132//2.33//print String34//test func flexString35println(flexStrings(s1:"Alice", s2:"Dylan"))3637//3.38//sumAny39//test func sumAny40println(sumAny(1,2,3))4142//4.43//countFrom44//test func countFrom45countFrom(from:1, to:5)46}4748/**491. fun change50交换2个任意对象的值51*/52func swap<T>(inout a:T, inout with b:T) {53(a, b) =(b, a)54}5556/**572. func flexStrings58输出字符串59*/60func flexStrings(s1:String ="", s2:String ="") ->String {61returns1 + s2 ==""?"none": s1+s262}6364/**653. func sumAny66*/67func sumAny(anys: Any...) ->String {68returnString(69(anys.map({itemin70switchitem {71case""asString,0asInt:72return-1073caselet sasStringwheres.toInt() >0:74returns.toInt()!75caseisInt:76returnitemasInt77default:78return079}80})as[Int]).reduce(0, combine: {81$0+ $182})83)84}8586/**874. func countFrom88*/89func countFrom(#from: Int, to:Int) {90println(from)91iffrom <to {92countFrom(from: from +1, to: to)93}94}9596overridefunc didReceiveMemoryWarning() {97super.didReceiveMemoryWarning()98//Dispose of any resources that can be recreated.99}100101102}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航