xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Swift Function argument All In One

Swift Function argument All In One

- 外部参数

a single unnamed parameter

https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID160

Swift Methods argument All In One

https://docs.swift.org/swift-book/LanguageGuide/Methods.html

Methods are functions that are associated with a particular type.

方法是与特定类型相关联的函数。

function arguments / methods arguments

import Foundation
// import SwiftUI
// error: no such module 'SwiftUI'
// print("Hello World")

// _ 外部参数名称,可以省略不写 ✅
func test(_ num: Int) -> Int {
   print(num);
   return num;
}
test(333);
// test(num: 333);
// extraneous 无关的
// ❌ error: extraneous argument label 'num:' in call

// 默认外部参数名称与内部参数一致,不可以省略不写 ✅
func test2(num: Int) -> Int {
   print(num);
   return num;
}
test2(num: 666);
// test2(666);
// ❌ error: missing argument label 'num:' in call

// 指定外部参数名称,不可以省略不写 ✅
func test3(int num: Int) -> Int {
   print(num);
   return num;
}
test3(int: 999);
// test3(num: 999);
// ❌ error: incorrect argument label in call (have 'num:', expected 'int:')


import Foundation

func test(_ num: Int) -> Int {
   print("num = \(num)");
   // num = 333
   print(num);
   return num;
}
test(333);

http://online.swiftplayground.run/

errors

Global 'var' declaration requires an initializer expression or an explicitly stated getter

Unnamed parameters must be written with the empty name '_'

    // function
    // 命名参数
    func getColors(colors: [Color]) -> LinearGradient {
      return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing)
    }

    // 未命名参数 ✅,_ 开头
    func getColors2(_ colors: [Color]) -> LinearGradient {
      return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing)
      // '_' can only appear in a pattern or on the left side of an assignment
      // Unnamed parameters must be written with the empty name '_'
    }

 // 命名参数,必须要写参数名称 `colors:`
      
.background(getColors(colors: card.gradientColors))

// 未命名参数,不需要写参数名称 ✅
.background(getColors2(card.gradientColors))

solution

demo

//
//  CardView.swift
//  CardsApp
//
//  Created by 凌晨 on 2022/5/10.
//

import SwiftUI

// global props
var bool = false;
// 调用 `$bool` 是地址引用
// 调用 `bool` 是值引用

// 定义变量时候指定类型,colorset 颜色集合
var gradientColors: [Color] = [Color("Color01"), Color("Color02")];

// 从上到下,线性渐变色
let bglColor = LinearGradient(gradient: Gradient(colors: gradientColors), startPoint: .top, endPoint: UnitPoint.bottom);

// 从左到右,线性渐变色
let bglColor2 = LinearGradient(gradient: Gradient(colors: gradientColors), startPoint: .leading, endPoint: .trailing);

let pink: Color = Color.pink;
let dark = Color.black;

// body
struct CardView: View {
    // scoped props / local props
    // 私有属性,局部属性
    var card: Card;
    // function
    // 命名参数
    // labelName 等于 paramName, colors 即是 labelName, colors 也是 paramName (外部参数名称 === 内部参数名称)
    // getColors(colors: colors) ✅
    func getColors(colors: [Color]) -> LinearGradient {
      return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing);
    }
    // 未命名参数, _ 开头是 labelName, colors 是 paramName
    // getColors2(colors) ✅
    func getColors2(_ colors: [Color]) -> LinearGradient {
      return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing);
      // '_' can only appear in a pattern or on the left side of an assignment
      // Unnamed parameters must be written with the empty name '_'
    }
    // color 是 labelName, colors 是 paramName (外部参数名称,内部参数名称)
    // getColors3(color: colors) ✅
    func getColors3(color colors: [Color]) -> LinearGradient {
      return LinearGradient(gradient: Gradient(colors: colors), startPoint: .leading, endPoint: .trailing);
    }
    var body: some View {
      ZStack {
        Image(card.imageName);
        VStack {
          Text(card.title)
            .font(Font.largeTitle)
            .fontWeight(.heavy)
            .multilineTextAlignment(.center);
          Text(card.headline)
            .fontWeight(.medium)
            .italic();
        }
        .foregroundColor(.white)
        .offset(y: -218)
        VStack {
          Button(
            action: {
              print("click button")
            },
            label: {
              HStack {
                Text(card.callToAction)
                  .font(Font.largeTitle)
                  .fontWeight(.heavy)
                Image(systemName: "arrow.right.circle")
                  .font(Font.largeTitle)
              }
              // 强调色
              .accentColor(.white)
              .padding(.vertical)
              .padding(.horizontal, 24)
              // .background(bglColor2)
              // .background(LinearGradient(gradient: Gradient(colors: card.gradientColors), startPoint: .leading, endPoint: .trailing))
              // 命名参数,必须要写参数名称 `colors:`
              // .background(getColors(colors: card.gradientColors))
              // 未命名参数,不需要写参数名称 ✅
              .background(getColors2(card.gradientColors))
              .clipShape(Capsule())
              .shadow(color: Color("ShadowColor"), radius: 6, x: 0, y: 3)
            }
          )
          .offset(y: 210)
        }
      }
      .frame(width: 335.0, height: 545.0)
      // .background(Color.pink)
      .background(bglColor)
      .cornerRadius(16)
      .shadow(radius: 8)
    }
}

// just for preview
//struct CardView_Previews: PreviewProvider {
//    static var previews: some View {
//        // CardView()
//        CardView(card: CardData[0])
//        .previewLayout(.sizeThatFits);
//        // 指定预览窗口大小与实际布局一致
//    }
//}


docs

struct LevelTracker {
    static var highestUnlockedLevel = 1
    var currentLevel = 1

    static func unlock(_ level: Int) {
        if level > highestUnlockedLevel { highestUnlockedLevel = level }
    }

    static func isUnlocked(_ level: Int) -> Bool {
        return level <= highestUnlockedLevel
    }

    @discardableResult
    mutating func advance(to level: Int) -> Bool {
        if LevelTracker.isUnlocked(level) {
            currentLevel = level
            return true
        } else {
            return false
        }
    }
}

https://docs.swift.org/swift-book/LanguageGuide/Methods.html

refs

http://online.swiftplayground.run/

https://www.cnblogs.com/xgqfrms/p/16255693.html



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-05-11 02:02  xgqfrms  阅读(103)  评论(2编辑  收藏  举报