小编loc*_*wei的帖子

为什么“ViewModifier”协议有“关联类型”和“类型别名”?

据我所知,ViewModifier协议的定义如下:

protocol ViewModifier {

    // content view type passed to body()
    typealias Content

    // type of view returned by body()
    associatedtype Body : View

    // only requirement
   func body(content: Self.Content) -> Self.Body

}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

为什么Self.Contenta typealiaswhileSelf.Body是an associatedtype?有什么不同?

type-alias associated-types swift-protocols swiftui

7
推荐指数
2
解决办法
1586
查看次数

@ViewBuilder 闭包的默认值?

我们可以给@ViewBuilder闭包参数一个默认值吗?

\n

我在做实验的时候出现了这个问题:

\n
import SwiftUI\nimport PlaygroundSupport\n\n// MyView\nstruct MyView<S:View, T:View>: View {\n    \n    let groove: S\n    let bar   : T\n    let p     : CGFloat = 10  // padding\n    \n    // \xe2\xad\x90\xef\xb8\x8f no default values\n    init(@ViewBuilder groove: () -> S, @ViewBuilder bar: () -> T) {\n        self.groove = groove()\n        self.bar    = bar()\n    }\n    \n    var body: some View {\n        ZStack {\n            groove\n            bar.padding(p)\n        }.frame(height: 80)\n    }\n}\n\n// content view\nstruct ContentView: View {\n    var body: some View {\n        // using MyView\n …
Run Code Online (Sandbox Code Playgroud)

swiftui

5
推荐指数
1
解决办法
1871
查看次数

Object.assign() 导致 TypeError: this.map 不是函数

我试图扩展Array.prototype一些新的方法/属性,以下是我在第一次尝试中所做的,最终得到了TypeError

\n
// Array.prototype + .max(), .maxRowLength (by Object.assign)\nObject.assign(Array.prototype, {\n\n    // max something in the array\n    max(mapf = (x) => x) {\n        // -----------------------------------------\n        return Math.max(...this.map(x => mapf(x)));\n        //                 ^^^^^^^^\n        // \xe2\x9b\x94 TypeError: this.map is not a function\n        // -----------------------------------------\n    },\n\n    // max row length of 2D array\n    // assuming an element of the 2D array is called a "row"\n    get maxRowLength() {\n        return this.max(row => row.length);\n    },\n});\n\n// matrix (2D array)\nlet m = [[1, 2, 3, …
Run Code Online (Sandbox Code Playgroud)

javascript

5
推荐指数
1
解决办法
655
查看次数