尽管在 Class 中声明了变量,但无法在范围内找到
Cannot Find In Scope Despite Variable Declared In Class
我正在使用 Swift 在 Xcode 上编写一个基本的 fps 应用程序。错误出现在我的 Bitmap.swift 文件中,该文件旨在将像素存储在二维数组中,以便有一种方法可以在应用程序中表示整个图像。
代码如下:
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
}
}
尽管在我的 class 中声明了“像素”和“宽度”,但我的扩展函数仍然出现这些错误:
“在范围内找不到 'pixels'”
“在范围内找不到 'width'”
“'Bitmap' 类型的值没有成员 'pixels'”
“'Bitmap' 类型的值没有成员 '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)
}
任何帮助将不胜感激。
您有一个 class
和 一个 struct
名为 Bitmap
。考虑到你打算如何使用它,我猜 class
是错误的。
删除 class
包裹在 struct
和删除 convenience
,它不用于 struct
s:
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
}
}
我正在使用 Swift 在 Xcode 上编写一个基本的 fps 应用程序。错误出现在我的 Bitmap.swift 文件中,该文件旨在将像素存储在二维数组中,以便有一种方法可以在应用程序中表示整个图像。
代码如下:
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
}
}
尽管在我的 class 中声明了“像素”和“宽度”,但我的扩展函数仍然出现这些错误:
“在范围内找不到 'pixels'” “在范围内找不到 'width'” “'Bitmap' 类型的值没有成员 'pixels'” “'Bitmap' 类型的值没有成员 '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)
}
任何帮助将不胜感激。
您有一个 class
和 一个 struct
名为 Bitmap
。考虑到你打算如何使用它,我猜 class
是错误的。
删除 class
包裹在 struct
和删除 convenience
,它不用于 struct
s:
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
}
}