'CircularProgressView' 调用我的包时由于 'internal' 保护级别无法访问初始化程序

'CircularProgressView' initializer is inaccessible due to 'internal' protection level when calling my package

我正在制作一个可以找到的 swiftUI 包 here

public struct CircularProgressView: View {
    
    var count: Int
    var total: Int
    var progress: CGFloat
    
    var fontOne: Font = Font.system(size: 75, weight: .bold, design: .rounded)
    var fontTwo: Font = Font.system(size: 25, weight: .bold, design: .rounded)

    var colorOne: Color = Color.primary
    var colorTwo: Color = Color.gray

    var fill = LinearGradient(gradient: Gradient(colors: [Color.green, Color.blue]), startPoint: .top, endPoint: .bottom)
    var lineWidth: CGFloat = 25.0
    
    public var body: some View {
        ZStack{
            Circle()
                .stroke(lineWidth: lineWidth)
                .opacity(0.3)
                .foregroundColor(Color.secondary)
            
            Circle()
                .trim(from: 0.0, to: CGFloat(min(self.progress, 1.0)))
                .stroke(fill ,style: StrokeStyle(lineWidth: lineWidth, lineCap: .round, lineJoin: .round))
                .rotationEffect(Angle(degrees: 270.0))
                .animation(.linear, value: progress)

            VStack {
                Text("\(count)")
                    .font(fontOne)
                    .foregroundColor(colorOne)
                Text("/ \(total)")
                    .font(fontTwo)
                    .foregroundColor(colorTwo)
            }
        }
    }
}

请注意,FontOne 和 fill 等变量是具有默认值的可选参数。

这是我试图在包中共享的视图的代码。我的测试用例成功了,我用这段代码测试了它

import XCTest
@testable import CircularProgress

import SwiftUI

final class CircularProgressTests: XCTestCase {
    func testExample() {
        let item = CircularProgressView(count: 5, total: 10, progress: 0.5)
        XCTAssertEqual(item.progress, 0.5)
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

我将包添加到测试项目中,但是当我尝试执行以下操作时

import SwiftUI
import CircularProgress

struct ContentView: View {
    var body: some View {
        CircularProgressView(count: 5, total: 10, progress: 0.5)
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

我在 CircularProgressView(count: 5, total: 10, progress: 0.5) 处遇到错误。我收到的错误是“'CircularProgressView' 初始值设定项由于 'internal' 保护级别而无法访问”

这是我的第一个包裹,我不确定我做错了什么。

注意:如果要导入包进行测试,请使用主分支,带有 1.0.0 标签的提交是旧版本。

在您的 CircularProgressView 中,您需要定义自己的初始化程序,因为编译器无法合成它。应该这样做:

public struct CircularProgressView: View {
    
    var count: Int
    var total: Int
    var progress: CGFloat

    // add the following to your CircularProgressView
    public init (count: Int, total: Int, progress: CGFloat) {
        self.count = count
        self.total = total
        self.progress = progress
    }

     // rest of code here
}

使用默认参数更新初始化程序

如果您有其他希望成为可选参数的参数,您可以通过将初始化程序设置为具有可选参数的默认值来实现。可以通过以下方式完成:

public struct CircularProgressView: View {

    var count: Int
    var total: Int
    var progress: CGFloat

    var fontOne: Font
    var fontTwo: Font

    var colorOne: Color
    var colorTwo: Color

    var fill: LinearGradient
    var lineWidth: CGFloat

    public init(count: Int,
                total: Int,
                progress: CGFloat,
                fontOne: Font = Font.system(size: 75, weight: .bold, design: .rounded),
                fontTwo: Font = Font.system(size: 25, weight: .bold, design: .rounded),
                colorOne: Color = Color.primary,
                colorTwo: Color = Color.gray,
                fill: LinearGradient = LinearGradient(gradient: Gradient(colors: [Color.green, Color.blue]), startPoint: .top, endPoint: .bottom),
                lineWidth: CGFloat = 25.0) {

        self.count = count
        self.total = total
        self.progress = progress
        self.fontOne = fontOne
        self.fontTwo = fontTwo
        self.colorOne = colorOne
        self.colorTwo = colorTwo
        self.fill = fill
        self.lineWidth = lineWidth
    }

    // remainder of code
}

这基本上设置了所有默认值,因此您实际上不需要在结构中设置它们。

这也意味着您不需要第一个初始化程序,因为这个初始化程序涵盖了这种情况。

然后你可以像这样使用它:

CircularProgressView(count: 5, total: 6, progress: 0.7)
CircularProgressView(count: 5, total: 6, progress: 0.7, colorTwo: Color.red, lineWidth: 6)

只传入你想使用的参数,但显然包括了必需的参数counttotalprogress