美文网首页
Swift 方法

Swift 方法

作者: 霸_霸霸 | 来源:发表于2019-10-28 15:50 被阅读0次

1、不带返回值的方法

func methodWithoutReturnValue(a : Int) {
    print(a)
}

2、带返回值的方法

func methodWithReturnValue(a : Int) -> Int {
    return a + 10
}

3、带参数标签的方法,只显示参数标签,不显示参数名

    func writeLetterTo(to aName : String, and bName : String) {
        
        //code
    }

//调用
    self.writeLetterTo(to: "Jack", and: "Peyton")

4、带有默认参数值的方法

    func printTwoValue(a : Int, b : Int = 10)  {
        //code
    }

    //有2种调用方法
    //1. 只给a赋值,b使用默认的10
    printTwoValue(a: 2)
    //2. 给a,b都赋值,不使用b的默认值
    printTwoValue(a: 2, b: 3)

5、带有可变参数的方法

    func printNames(_ names : String...)  {
        for index in names {
            print(index)
        }
    }

    //调用
    self.printNames("Jack", "Lucy", "Michael")

6、通过inout关键字在方法内部修改外面变量(不是对象)的值
我们知道外部的变量,在作为参数时,是拷贝一份全新的值,赋给方法的参数,所以,我们在方法中对参数进行操作的时候,是不会改变外部变量的值的;Swift提供了inout关键字,允许我们在方法内部修改外部变量的值,例子:

var a = 10
var b = 20
func exchangeOutValues(a : inout Int, b : inout Int) {
    //交换啊a,b的值
    let c = a
    a = b
    b = c
}

//调用
exchangeOutValues(a: &a, b: &b)

a 和 b 的值会互换

7、方法也是对象,它的类型由参数和返回值决定

    //定义一个method
    var method : (Int, Int) -> Int

    //一个方法的实现
    func addTwoIntsValue(a : Int, b : Int) -> Int {
        return a + b    
    }
    //给method赋值
    method = addTwoIntsValue(a:b:)
    //调用
    print(method(10, 20))

8、方法作为方法的参数

    //声明一个方法doSomething,其参数为:类型为(Int) -> Int的方法
    func doSomething(method : (Int) -> Int) {
        print(method(10))
    }
    //调用doSomething方法
    self.doSomething { (a : Int) -> Int in
        return a + 1
    }

结果为 11
上面调用doSomething的时候,方法作为参数的形式很像是闭包

9、方法作为方法的返回值

    //doSomething方法返回一个类型为 (Int)->Int 的方法
    func doSomething() -> (Int) -> Int {
        return self.returnMethod(a:)
    }
    
    //这个方法将作为doSomething方法的返回值
    func returnMethod(a : Int) -> Int {
        return a + 1
    }

    //调用doSomething方法,用一个新的方法接收doSomething方法的返回值
    let method = self.doSomething()

    //调用最终的方法
    print(method(100))

结果为101

类方法和实例方法

1、实例方法

1.1 实例方法的声明

    func 方法名(参数标签 参数名 : 参数类型) -> 返回值类型 {
        方法体
    }

上面的例子都是实例方法,不再赘述

1.2 实例方法的继承

声明两个类ClassA,ClassB继承自ClassA

class ClassA : NSObject {
    override init() {
        super.init()
    }
            
    func instanceMethod()  {
        print("instant method A")
    }
}

class ClassB : ClassA {
    override init() {
        super.init()   
    }
    //override表示重写父类的方法
    override func instanceMethod() {
        print("instant method B")
    }
}

调用实例方法

    let a = ClassA.init()
    let b = ClassB.init()
        
    a.instanceMethod()
    b.instanceMethod()

结果显而易见
instant method A
instant method B

2、类方法

2.1、如何声明类方法

一般情况下,我们是通过static关键字来声明类方法,但是如果一个类方法需要在子类中重写,那就必须使用class关键字

//不可被子类重写
static func classMethod1() {
    print("1")
}
//可被子类重写
class func classMethod2() {
    print("2")
}

2.2、类方法如何继承

class ClassA : NSObject {
    static func classMethod1() {
        print("1")
    }
    class func classMethod2() {
         print("2")
    }
}

class ClassB : ClassA {
    override class func classMethod2() {
        print("4")
    }
}

classMethod2是通过class关键字声明的,所以可以在ClassB中进行重写;如果你要对classMethod1进行重写,会报错
调用

ClassA.classMethod1()
ClassA.classMethod2()
ClassB.classMethod1()
ClassB.classMethod2()

结果
1
2
1
4

相关文章

  • Swift 类方法和实例方法

    Swift方法声明 Swift方法调用

  • NSButton仿UIButton功能

    swift方法参考 CustomButton.swift Utilities.swift OC方法参考 《macO...

  • swift语法--11getter&setter方法

    swift语法--11getter&setter方法 swift中的setter方法 但在Swift开发中,我们用...

  • Quartz2D - 1

    开源框架CorePlot oc oc使用方法: Swift swift 使用context的方法:

  • OC和Swift混编遇到的一些小麻烦

    一. OC中调用swift类中的方法时,编译器有时找不到方法声明 OC调用swift类中的方法,swift类需要有...

  • 从零学习Swift 08: 继承体系

    一: 方法 方法的定义: 同 OC 一样, Swift 也有实例方法和对象方法: 在 Swift 中,类,结构体,...

  • Swift基础--方法

    Swift基础--方法 swift中方法分为实例方法和静态方法,方法时于默写特定类型相关联的函数.类,结构体,枚举...

  • swift方法

    oc中只有类可以定义方法,swift类/结构体/枚举都能定义方法 swift的类型方法类似oc的类方法 实例方法(...

  • oc Swift 混编

    oc Swift 混编 oc 项目 混编Swift1.1 oc 调用 Swift 的类 和 方法步骤: ...

  • Flutter 高阶函数

    Swift 转 Dart,有些方法还是有通用的。 1,forEach 此方法提供遍历。 2,map 和Swift ...

网友评论

      本文标题:Swift 方法

      本文链接:https://www.haomeiwen.com/subject/musbvctx.html