What is a scope ?

I'm learning how to program a button for my iOS App Development and I'm receiving these issue("error") that specifies the next text "Cannot find 'signIn' in scope".
Answered by OOPer in 674209022
You have a file HexagonParameters.swift, but the name of the struct in it is being HexagonParamaters, not HexagonParameters.

Wrong:
Code Block
struct HexagonParamaters{


Right:
Code Block
struct HexagonParameters{


scope is an area meaning some range in code where each name is valid.

Maybe you need some specific description to your issue. Please show enough code which represents what would be your issue.
Take this example

Code Block
class MyClass {
var classScopeVar = 1 // This is visible in the scope of the class
func someFunc() {
var funcScopeVar = 2 // This is only defined (hence visible) in the scope of the func
print("classScopeVar", classScopeVar)
print("funcScopeVar", funcScopeVar)
}
func otherFunc() {
print("classScopeVar", classScopeVar) // This is visible in the scope of the class, hence it works
print("funcScopeVar", funcScopeVar) // This is visible only in the scope of someFunc, hence compiler error here
}
}

So, in your case, check where you declared the button.
If it is inside a func, it will not be visible outside of the func.
You need to declare it at the class level most likely.
If you have created a button in storyboard, you need to connect to an IBOutlet to reference it (control-drag from the button in storyboard to some place in the class ; the following will be automatically created once you give signIn as the name for the button):
Code Block
@IBOutlet weak var signIn: UIButton!


Scope is also important because local declaration is considered in priority.

If you write
Code Block
func yetAnotherFunc() {
var classScopeVar = 3 // You redeclare a new var with the same name
print("classScopeVar", classScopeVar) // You will get 3, not 1
// If you want to access the initial var
print("real classScopeVar", self.classScopeVar) // You will get 1
}


I have just finish reading your examples and I know that they are right.
I want to add a Button in my tutorial with a screen touch to see if the mobile device recognizes the touch for the button and receive a scope 'declare' error from the complier.

Do you have time to give me a example of a button with a declared scope ?

Kind Regards
In the previous example, let's create a signIn button in storyboard, in the viewController.
  • Connect (control drag) this button to the class code, just below the class declaration. Give the name signIn.

  • You need also to create an action : control-drag again from the button in storyboard, to the code, and select Action. Give it a name like pressSignIn

The button has not a 'declared' scope, it has a scope depending how it is declared.
Type the new instructions as shown below
You should get something like:

Code Block
class MyClass {
    @IBOutlet weak var signIn: UIButton! // see the black dot at the left
var classScopeVar = 1 // This is visible in the scope of the class
    @IBAction func pressSignIn(_ sender: UIButton) { // button is in fact signIn
print("Button pressed")
print("It is signIn, which is visible in scope", signIn.titleLabel?.text ?? "") // We check the button is visible here
// You could also have written:
print("Sender", sender.titleLabel?.text ?? "") // The same result
}
func someFunc() {
var funcScopeVar = 2 // This is only defined (hence visible) in the scope of the func
print("classScopeVar", classScopeVar)
print("funcScopeVar", funcScopeVar)
print("I can also call from this func", signIn.titleLabel?.text ?? "") // We check the button is visible here
// But the following would fail:
print("Sender", sender.titleLabel?.text ?? "") // sender is not visible here, only in the IBAction func. So that will cause a compiler error. Comment out this line to be able to compile and run.
}
func otherFunc() {
print("classScopeVar", classScopeVar) // This is visible in the scope of the class, hence it works
print("funcScopeVar", funcScopeVar) // This is visible only in the scope of someFunc, hence compiler error here
}
}


If that's OK, don't forget to close the thread on this answer.
Otherwise, please explain what is still unclear for you.
I will learn to code it myself and if I will obtain a negative result, I will learn with your example.

Kind Regards
Code Block
//
//  BadgeBackground.swift
//  Landmarks
//
//  Created by Besleaga Alexandru Marian on 5/11/21.
//
import SwiftUI
struct BadgeBackground: View {
    var body: some View {
        Path { path in
            var width: CGFloat = 100.0
                        let height = width
                        path.move(
                            to: CGPoint(
                                x: width * 0.95,
                                y: height * 0.20
                            )
                        )
            
            HexagonParameters.segments.forEach { segment in
                            path.addLine(
                                to: CGPoint(
                                    x: width * segment.line.x,
                                    y: height * segment.line.y
                                )
                            )
                        }
            
                }
                .fill(Color.black)
    }
}
struct BadgeBackground_Previews: PreviewProvider {
    static var previews: some View {
        BadgeBackground()
    }
}
And I receive the following error "Cannot find 'HexagonParameters' in scope"
How do I code the the scope for HexagonParameters ?

And I receive the following error "Cannot find 'HexagonParameters' in scope"

How do I code the the scope for HexagonParameters ?

I guess you took the code from a tutorial of SwiftUI and HexagonParameters is a name of a struct.
In this case, scope is somewhere in your project.
You create a new file named HexagonParameters.swift in your project and put the definition of it into the file.
https://developer.apple.com/tutorials/swiftui/drawing-paths-and-shapes

Do you understand the problem ?
You use HexagonParameters, but is is nowhere defined. How could the compiler guess what it is ?

In your code, just add the struct definition:

Code Block
import SwiftUI
struct HexagonParameters {
struct Segment {
let useWidth: (CGFloat, CGFloat, CGFloat)
let xFactors: (CGFloat, CGFloat, CGFloat)
let useHeight: (CGFloat, CGFloat, CGFloat)
let yFactors: (CGFloat, CGFloat, CGFloat)
}
static let adjustment: CGFloat = 0.085
static let points = [
Segment(
useWidth: (1.00, 1.00, 1.00),
xFactors: (0.60, 0.40, 0.50),
useHeight: (1.00, 1.00, 0.00),
yFactors: (0.05, 0.05, 0.00)
),
Segment(
useWidth: (1.00, 1.00, 0.00),
xFactors: (0.05, 0.00, 0.00),
useHeight: (1.00, 1.00, 1.00),
yFactors: (0.20 + adjustment, 0.30 + adjustment, 0.25 + adjustment)
),
Segment(
useWidth: (1.00, 1.00, 0.00),
xFactors: (0.00, 0.05, 0.00),
useHeight: (1.00, 1.00, 1.00),
yFactors: (0.70 - adjustment, 0.80 - adjustment, 0.75 - adjustment)
),
Segment(
useWidth: (1.00, 1.00, 1.00),
xFactors: (0.40, 0.60, 0.50),
useHeight: (1.00, 1.00, 1.00),
yFactors: (0.95, 0.95, 1.00)
),
Segment(
useWidth: (1.00, 1.00, 1.00),
xFactors: (0.95, 1.00, 1.00),
useHeight: (1.00, 1.00, 1.00),
yFactors: (0.80 - adjustment, 0.70 - adjustment, 0.75 - adjustment)
),
Segment(
useWidth: (1.00, 1.00, 1.00),
xFactors: (1.00, 0.95, 1.00),
useHeight: (1.00, 1.00, 1.00),
yFactors: (0.30 + adjustment, 0.20 + adjustment, 0.25 + adjustment)
)
]
}
struct BadgeBackground: View {
var body: some View {
Path { path in
var width: CGFloat = 100.0
let height = width
path.move(
to: CGPoint(
x: width * 0.95,
y: height * 0.20
)
)
HexagonParameters.segments.forEach { segment in
path.addLine(
to: CGPoint(
x: width * segment.line.x,
y: height * segment.line.y
)
)
}
}
.fill(Color.black)
}
}
struct BadgeBackground_Previews: PreviewProvider {
static var previews: some View {
BadgeBackground()
}
}

HexagonParamaters.swift

Code Block
//
//  HexagonParameters.swift
//  Landmarks
//
//  Created by Besleaga Alexandru Marian on 5/11/21.
//
import Foundation
import CoreGraphics
struct HexagonParamaters{
    struct Segment {
            let line: CGPoint
            let curve: CGPoint
            let control: CGPoint
        }
    
    static let adjustment: CGFloat = 0.085
    
    static let segments = [
        Segment(
                    line:    CGPoint(x: 0.60, y: 0.05),
                    curve:   CGPoint(x: 0.40, y: 0.05),
                    control: CGPoint(x: 0.50, y: 0.00)
                ),
                Segment(
                    line:    CGPoint(x: 0.05, y: 0.20),
                    curve:   CGPoint(x: 0.00, y: 0.30),
                    control: CGPoint(x: 0.00, y: 0.25)
                ),
                Segment(
                    line:    CGPoint(x: 0.00, y: 0.70),
                    curve:   CGPoint(x: 0.05, y: 0.80),
                    control: CGPoint(x: 0.00, y: 0.75)
                ),
                Segment(
                    line:    CGPoint(x: 0.40, y: 0.95),
                    curve:   CGPoint(x: 0.60, y: 0.95),
                    control: CGPoint(x: 0.50, y: 1.00)
                ),
                Segment(
                    line:    CGPoint(x: 0.95, y: 0.80),
                    curve:   CGPoint(x: 1.00, y: 0.70),
                    control: CGPoint(x: 1.00, y: 0.75)
                ),
                Segment(
                    line:    CGPoint(x: 1.00, y: 0.30),
                    curve:   CGPoint(x: 0.95, y: 0.20),
                    control: CGPoint(x: 1.00, y: 0.25)
                )
        ]
}


BadgeBackground.swift
Code Block
//
//  BadgeBackground.swift
//  Landmarks
//
//  Created by Besleaga Alexandru Marian on 5/11/21.
//
import SwiftUI
struct BadgeBackground: View {
    var body: some View {
        Path { path in
            var width: CGFloat = 100.0
                        let height = width
                        path.move(
                            to: CGPoint(
                                x: width * 0.95,
                                y: height * (0.20 + HexagonParameters.adjustment)
                            )
                        )
            
            HexagonParameters.segments.forEach { segment in
                            path.addLine(
                                to: CGPoint(
                                    x: width * segment.line.x,
                                    y: height * segment.line.y
                                )
                            )
                
                path.addQuadCurve(
                                    to: CGPoint(
                                        x: width * segment.curve.x,
                                        y: height * segment.curve.y
                                    ),
                                    control: CGPoint(
                                        x: width * segment.control.x,
                                        y: height * segment.control.y
                                    )
                                )
                        }
            
                }
                .fill(Color.black)
    }
}
struct BadgeBackground_Previews: PreviewProvider {
    static var previews: some View {
        BadgeBackground()
    }
}


Code Block
Cannot find 'HexagonParameters' in scope


What should I do ?


Accepted Answer
You have a file HexagonParameters.swift, but the name of the struct in it is being HexagonParamaters, not HexagonParameters.

Wrong:
Code Block
struct HexagonParamaters{


Right:
Code Block
struct HexagonParameters{


What is a scope ?
 
 
Q