func及相关类型

2022/10/17

# 定义

  • 返回值void可以不写
    func test1()
    {
        print("你好");
    }
    
    func test2() -> Void
    {
        print("你好");
    }
    
    test2();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

# 类型

var a: () -> Void = {() -> Void in
    print("1111");
}

a(); // 1111
1
2
3
4
5

# 返回函数

func test() -> () -> Void
{
    print("你好");
    func a()
    {
        print("a函数");
    }
    return a;
}
1
2
3
4
5
6
7
8
9

# 可变参数长度

func test(s: String...)
{
    print(s);
}

test(s: "1", "2", "3");
1
2
3
4
5
6

# 元组入参

func test(people:(name: String, age: Int))
{
    print("\(people.name)的年龄是\(people.age)");
}

test(people: (name: "hdy", age: 18));
1
2
3
4
5
6
func test(people:(name: String, age: Int))
{
    let (name, age) = people;
    print("\(name)的年龄是\(age)");
}

test(people: (name: "hdy", age: 18));
1
2
3
4
5
6
7

# 入参命名

  • 定义外部调用名内部使用名
    // age : 外部调用名
    // a   : 内部使用名
    func test(age a: Int)
    {
        print("年龄是\(a)");
    }
    
    test(age: 18);
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 只写一个默认外部名称和内部名称相同
    func test(age: Int)
    {
        print("年龄是\(age)");
    }
    
    test(age: 18);
    
    1
    2
    3
    4
    5
    6
  • 下划线_代表忽略外部调用名
    func test(_ a: Int)
    {
        print("年龄是\(a)");
    }
    
    test(18);
    
    1
    2
    3
    4
    5
    6
  • 类型传递可以关闭关联命名规则?
    func test(name: String, age: Int) -> Void
    {
        print("\(name)的年龄是\(age)");
    }
    
    let c: (String, Int) -> Void = test;
    c("hdy", 18); // hdy的年龄是18
    
    1
    2
    3
    4
    5
    6
    7

# 入参别名

  • 入参可以直接省略,$0代表第一个入参,可以直接使用
let a: (String, Int) -> String = {"参数1:\($0),参数2:\($1)"}

print(a("你好", 857)); // 参数1:你好,参数2:857
1
2
3

# 匿名函数

  • 直接通过匿名函数传递到变量进行命名赋值,用in分隔入参和实现
    let c = {(name: String, age: Int) -> Void in
        print("\(name)的年龄是\(age)");
    }
    c("hdy", 18); // hdy的年龄是18
    
    1
    2
    3
    4
  • 定义简单函数
    let a: () -> Void = {
      print("匿名函数");
    }
    a(); // 匿名函数
    
    1
    2
    3
    4

# 返回函数

let a = {() -> () -> Void in
  func test() -> Void
  {
    print("高阶函数");
  }
  return test;
}
a()(); // 高阶函数
1
2
3
4
5
6
7
8

# assert

  • 相当于js的 throw。抛出错误停止运行
  • 返回false才会停止运行,第二个参数是提示信息
func test(_ a: Int)
{
    if a < 19
    {
        assert(false, "stop");
    }
    print("18")
}

test(18);
1
2
3
4
5
6
7
8
9
10

# guard

  • 相当于否定的if
    func test(param: String?)
    {
        guard let value = param else
        {
            print("param没有值");
            return;
        }
        print("param 为\(value)");
    }
    
    let a: String? = "你好";
    test(param: a); // param 为你好
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
  • 声明的值可以在代码块外面使用








     






    func test(param: String?)
    {
        guard let value = param else
        {
            print("param没有值");
            return;
        }
    
        print("param 为\(value)");
    }
    
    let a: String? = nil;
    
    test(param: a); // param没有值
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

# inout

  • 函数参数默认不能修改
    func test(a: Int)
    {
        a += 1;
        print(a); // error
    }
    
    var p = 1;
    test(a: p);
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 通过inout函数参数修改,需要传入引用
     






     



    func test(a: inout Int)
    {
        a += 1;
        print(a); // 2
    }
    
    var p = 1;
    test(a: &p);
    
    print(p); // 2
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

# 泛型

func getAge<T>(age: T) -> T
{
    return age;
}
    
print(getAge(age: "hdy")); // hdy
print(getAge(age: 18)); // 18
print(getAge(age: [1, 2, 3, 4])); // [1, 2, 3, 4]
1
2
3
4
5
6
7
8

# 尾随闭包

  • 概念:将入参的末尾的闭包函数放到点用写法外面去
    func test(p1: String, p2: (_:String) -> Void)
    {
        p2(p1);
    }
    
    test(p1: "hdy") { s in
        print("hello \(s)");
    }
    // hello hdy
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  • 单个入参尾随必报可以省略括号
    func test(p: (String) -> String)
    {
        var ans = p("hdy");
        print(ans);
    }
    
    test { s -> String in
        return "你好 \(s)";
    }
    // hello hdy
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
上次更新: 9/17/2024