Cannot Find Variable in Scope

Swift compiler error: cannot find variable in scope

Sometimes there are ghost errors that seem to not go away, even when Xcode is closed and reopened, but are fixed by either changing the code then changing it back, or clicking build anyway and the build succeeds.

Otherwise, the color highlighting of T1 in the return doesn't match in the variable declaration. That suggests name clashing. Are there any types or global variables called T1, even if there's nothing else in your code maybe it's a built-in type or similar? Try naming the variable differently.

switch and cannot find variable in scope

A pair of braces is called a scope.

In Swift (unlike some other languages) there is a simple but iron rule:

  • A variable declared inside a scope – in your particular case inside the switch statement – is visible in its own scope and on a lower level – like in your second example

  • It's not visible on a higher level outside the scope – like in your first example.

You can even declare size as constant because it's guaranteed to be initialized.

func sizeCheckVar(value: Int) -> String {
let size: String
switch value {
case 0...2: size = "small"
case 3...5: size = "medium"
case 6...10: size = "large"
default: size = "huge"
}
return size
}

However actually you don't need the local variable at all. Just return the values immediately

func sizeCheckVar(value: Int) -> String {
switch value {
case 0...2: return "small"
case 3...5: return "medium"
case 6...10: return "large"
default: return "huge"
}
}

Side note: The colon in a switch statement is also a kind of scope separator otherwise you would get errors about redeclaration of variables in the first example.

Swift Cannot find variable in scope

midiManager is not present inside your MainMenueBar. So midiManager can not be found. You must need to add midiManager inside your MainMenueBar structure. Like this -

struct MainMenuBar: MenuBar
{
public let midiManager = MIDI.IO.Manager(clientName: "MIDIEventLogger", model: "ELMC Midi Monitor", manufacturer: "ELMC")
public var body: StandardMenuBar
{

StandardMenu(title: "Midi Devices")
{
midiManager.endpoints.outputs.forEach { device in
TextMenuItem(title: device.name) { _ in
print(device.uniqueID)
}
}
}
}
}

or
By creating a common class as-

class MidiManager {
static let midiManager = MIDI.IO.Manager(clientName: "MIDIEventLogger", model: "ELMC Midi Monitor", manufacturer: "ELMC")
}

And then use it in both MainMenuBar and ReactNativeMidiModule as-

struct MainMenuBar: MenuBar {
public let midiManager = MidiManager.midiManager
public var body: StandardMenuBar {
StandardMenu(title: "Midi Devices") {
midiManager.endpoints.outputs.forEach { device in
TextMenuItem(title: device.name) { _ in
print(device.uniqueID)
}
}
}
}
}

@objc(ReactNativeMidiModule)
class ReactNativeMidiModule: RCTEventEmitter, NSApplicationDelegate {
// Create midi manager
public let midiManager = MidiManager.midiManager

// other code
}

Cannot Find In Scope Despite Variable Declared In Class

You have both a class and a struct named Bitmap. I'm guessing the class is erroneous, given your description of how you intend to use it.

Remove the class wrapped around the struct and remove convenience, which is not used for 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
}
}

Why do I keep getting a Cannot find '...' in scope error?

You don't have anything defined called scoreTracker in your code, but you're trying to use it in your preview. Instead, you can pass a constant in place of your binding (just for preview purposes):

struct scoreboardView: View {
@Binding var score: Int
var body: some View {
List {
ForEach(1..<9) {
Text("Game \($0): \(score) ")
}
}
}
}

struct scoreboardView_Previews: PreviewProvider {
static var previews: some View {
scoreboardView(score: .constant(50))
}
}


Related Topics



Leave a reply



Submit