Swift Basic 2

传入函数的参数可以简单的用一个数组动态传入

func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf()
sumOf(42, 597, 12)
sumOf(222,3333,44444,555555)

函数可以嵌套使用

func returnFifteen() -> Int {
    var y = 10
    // 嵌套的函数
    func add() {
        y += 5
    }
    // 引用嵌套的函数
    add()
    return y
}
returnFifteen()

一个函数可以返回另一个函数作为它的值

func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}
//返回addOne(number:Int)->Int{....}给increment
var increment = makeIncrementer()
//传入参数
increment(7)

一个函数可以使用另一个函数作为它的传入参数

func hasAnyMatches(list: [Int], condition: Int -> Bool) -> Bool {
    for item in list {
        if condition(item) {
            return true
        }
    }
    return false
}
func lessThanTen(number: Int) -> Bool {
    return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(numbers, lessThanTen)

You can write a closure without a name by surrounding code with braces ({}). Use in to separate the arguments and return type from the body.

var numbers = [20, 19, 7, 12]
//----乘以3方法一
numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})
//----乘以3方法2
let mappedNumbers = numbers.map({ number in 3 * number })
mappedNumbers
//----排序
let sortedNumbers = sorted(numbers) { $0 > $1 }
sortedNumbers

对象和类

// class + 类名 新建一个类
//{ 属性和方法 定义}
class Shape {
    var numberOfSides = 4
    let color = "#3b5998"
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
    func shadow(isShadowOn:Bool) ->String{
        if(isShadowOn){
           return "This is ON"
        }else{
           return "This is OFF"
        }
    }
}

// 首先实例化一个类
// 用class.propert class.method 来引用属性和方法
var shape = Shape()
// 改写属性
shape.numberOfSides = 7
// 调用方法
var shapeDescription = shape.simpleDescription()
var shadow = shape.shadow(true)

//定义类Shape
class Shape {
    //定义属性
    let color = "#000"
    var numberOfSides = 0
    var name: String
    //初始化
    init(name: String) {
        self.name = name
    }
    //定义方法
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

//新建一个实例化Shape
var shape = Shape(name: "Test_Shape")
//设置属性
shape.numberOfSides = 7
//调用方法
var shapeDescription = shape.simpleDescription()

//子类Square继承自Shape
class Square: Shape {
    //添加属性
    var sideLength: Double
    //自定义初始化方法
    init(sideLength: Double, name: String) {
        self.sideLength = sideLength
        super.init(name: name)
        numberOfSides = 4
    }
    //添加的方法
    func area() ->  Double {
        return sideLength * sideLength
    }
    //复写父类方法
    override func simpleDescription() -> String {
        return "A square with sides of length \(sideLength)."
    }
}
//实例化一个Square
let test = Square(sideLength: 5.2, name: "my test square")
//调用方法
test.area()
test.simpleDescription()
时间: 2024-08-24 18:15:42

Swift Basic 2的相关文章

Swift Basic

推荐使用Xcode6 playground的功能来测试,可以很方便看到输出 不需要分号 println("Hello, world!") 简单的赋值方式 使用 let 标记一个常量   var 标记一个变量 var myVariable = 42 myVariable = 50 let myConstant = 42 追加声明数据类型 let implicitInteger = 70 let implicitDouble = 70.0 let explicitDouble: Doubl

计算机基础篇之二

目录 1.1 编程语言 1)低级语言 2)高级语言 1.2 内核kernel 1.3 程序 1.4 进程 1.5 cpu内存空间 1.1 编程语言 1)低级语言 #离机器越近语言 #二进制机器指令 #汇编语言 #ADD,R0,R1,ST... 2)高级语言 #离近人类越近的自然语言 #编译型语言 #c,c++,go... #解释型语言 #c#,python,java,swift,basic... 1.2 内核kernel #是一个通用软件,运行在硬件上,不负责具体的工作,只是协调各个程序,将硬件

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 03 The Basic

import Foundation //*********************************************************************************************** //1.The Basics(基础) //_______________________________________________________________________________________________ //介绍 //Swift 的类型是

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 04 Basic Operators

import Foundation //*********************************************************************************************** //1.Basic Operators(基本操作符) //_______________________________________________________________________________________________ //简介 //操作

在Coxoa和Objextive-C中使用Swift(1)Basic Setup

在Coxoa和Objextive-C中使用Swift(Using Swift with Cocoa and Objective-C) 开始->基本设置 说明:这是一个针对API和开发技术的初步文件.苹果公司提供了这个文档帮助您选择合适技术和编程接口,来构建苹果品牌产品.这个信息使随时变化的,软件实施要根据本文在最终的操作系统软件和最终文件进行测试.本文档的较新版本可能提供关于新的API或技术的细节. Swift的设计提供了对Cocoa和Objective-c的无缝兼容.你可以在Swift中使用O

Swift学习笔记-基本运算符(Basic Operators)-求余运算符

求余运算(a % b)是计算b的多少倍刚刚好可以容入a,返回多出来的那部分(余数). 注意:求余运算(%)在其他语言也叫取模运算.然而严格说来,我们看该运算符对负数的操作结果,"求余"比"取模"更合适些. 我们来谈谈取余是怎么回事,计算9 % 4,你先计算出4的多少倍会刚好可以容入9中: 2倍,非常好,那余数是1(用橙色标出) 在 Swift 中可以表达为: 9 % 4 // 等于 1 为了得到a % b的结果,%计算了以下等式,并输出余数作为结果: a = (b

[Swift]LeetCode227. 基本计算器 II | Basic Calculator II

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, /operators and empty spaces . The integer division should truncate toward zero. Example 1: Input: "3+2*2" Ou

swift语言点评三 - Basic Operators

1.Tuples are compared from left to right, one value at a time, until the comparison finds two values that aren't equal. (1, "zebra") < (2, "apple") You can compare two tuples if they have the same type and the same number of values.

[Swift]LeetCode770. 基本计算器 IV | Basic Calculator IV

Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {"e": 1} (given in terms of evalvars = ["e"] and evalints = [1]), return a list of tokens representing the simplified expression, such as