首页 > 解决方案 > 尽管在类中声明了变量,但无法在范围内找到

问题描述

我正在使用 Swift 在 Xcode 上编写一个基本的 fps 应用程序。错误出现在我的 Bitmap.swift 文件中,该文件旨在将像素存储在 2D 数组中,以便有一种方法可以在应用程序中表示整个图像。

代码如下:

import UIKit

class Bitmap: UIViewController {

    public struct Bitmap
    {
        public private(set) var pixels: [Color]
        public let width: Int
        
        public init(width: Int, pixels: [Color])
        {
            self.width = width
            self.pixels = pixels
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}


extension Bitmap
{
    var height: Int
    {
        return pixels.count / width
    }
    subscript(x: Int, y: Int) -> Color
    {
        get { return pixels[y * width + x] }
        set { pixels[y * width + x] = newValue}
    }
    convenience init(width: Int, height: Int, color: Color) {
        self.pixels = Array(repeating: color, count: width * height)
        self.width = width
    }
}

尽管在我的类中声明了“像素”和“宽度”,但我的扩展函数不断收到这些错误:

“在范围内找不到‘像素’”“在范围内找不到‘宽度’”“‘位图’类型的值没有成员‘像素’”“‘位图’类型的值没有成员‘宽度’”

编辑:这是我的另一个文件“Color.swift”的代码

import UIKit

class Color: UIViewController {
    
    public struct Color {
        public var r, g, b, a: UInt8
        
        public init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 255)
        {
            self.r = r
            self.g = g
            self.b = b
            self.a = a
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}


extension Color {
    static let clear = Color(r: 0, g: 0, b: 0, a: 0)
    static let black = Color(r: 0, g: 0, b: 0)
    static let white = Color(r: 255, g: 255, b: 255)
    static let gray = Color(r: 192, g: 192, b: 192)
    static let red = Color(r: 255, g: 0, b: 0)
    static let green = Color(r: 0, g: 255, b: 0)
    static let blue = Color(r: 0, g: 0, b: 255)
}

任何帮助将不胜感激。

标签: iosswiftxcodepixel

解决方案


您同时拥有aclass structnamed Bitmapclass鉴于您对打算如何使用它的描述,我猜这是错误的。

去掉sclass周围的包裹struct,去掉convenience不用于structs 的:

public struct Bitmap
{
    public private(set) var pixels: [Color]
    public let width: Int
    
    public init(width: Int, pixels: [Color])
    {
        self.width = width
        self.pixels = pixels
    }
}


extension Bitmap
{
    var height: Int
    {
        return pixels.count / width
    }
    subscript(x: Int, y: Int) -> Color
    {
        get { return pixels[y * width + x] }
        set { pixels[y * width + x] = newValue}
    }
    init(width: Int, height: Int, color: Color) {
        self.pixels = Array(repeating: color, count: width * height)
        self.width = width
    }
}

推荐阅读