Error Cannot find « » in scope XCODE

Hello I have a problem with xCode, I have this error : Cannot find « string » in scope. Thank you so much for your help

Photos:

My code:

//

//  AuthviewModel.swift

//  iSpeak

//

//  Created by Sayan on 21.11.22.

//

import Foundation

import Firebase

class AuthViewModel: ObservableObject {

    var manager = FirebaseManager.shared

    

    @Published var isFinishedConnecting: Bool = false

    @Published var isAuth: Bool = false

    @Published var showError: Bool = false

    

    var errorString: String = ""

    var datas: [String: Any] = [:]

    

    var auth: Auth {

        return manager.auth

    }

    

    init() {

        observeAuthentication()

    }

    

    func observeAuthentication() {

        auth.addStateDidChangeListener(handleChangeListener)

    }

    

    func handleChangeListener(auth: Auth, user: User?) {

        self.isFinishedConnecting = true

        self.isAuth = user != nil

    }

    

    func signIn(email: String, password: String) {

        guard checkValue(_string: email, value: "adresse email") else { return }

        guard checkValue(_string: password, value: "Mot de passe") else { return }

        

    }

    

    func createUser() {

        

    }

    

    func checkValue(_string: String, value: String) -> Bool {

        let isNotEmpty = string != ""

        self.errorString = !isNotEmpty ? "" : "Merci d'entrer (value) pour continuer"

        self.showError = !isNotEmpty

        return isNotEmpty

    }

}

Accepted Reply

func checkValue(_string: String, value: String) -> Bool {
  let isNotEmotv = string != ""

There is an inconsistency between parameter declaration in func and the var used in the test.

You use the wrong parameter name in test. Or you have a missing space between _ and string. Which causes the compiler to look for a parameer named _string and not string.

First option, change the test on string to a test on _string:

func checkValue(_string: String, value: String) -> Bool {
  let isNotEmotv = _string != ""  // <<-- add _

or second option, change the parameter definition in the func declaration

func checkValue(_ string: String, value: String) -> Bool {  // <<-- add space
  let isNotEmotv = string != ""

Second option is preferrable, but then you need to change the calls as:

    checkValue(email, value: "adresse email"
    checkValue(password, value: "Mot de passe")

However, signIn is bizarre.

You call checkValue which is a Bool, not an optional. So guard does not make sense here.

 func signIn(email: String, password: String) {
    guard checkValue(_string: email, value: "adresse email") else { return }
    guard checkValue(_string: password, value: "Mot de passe") else { return }
  }

What do you want to do if check are not OK ?

Your code should probably be (if you select second option):

 func signIn(email: String, password: String) -> Bool {
  
    if checkValue(email, value: "adresse email") && checkValue(password, value: "Mot de passe") {
    // Do something when OK
    return true
   } else {
      // Not OK, alert user that if failed
      return false
   }
 }
  • Just a precision: guard is unneeded, as you have no value to guard ; you could simply write if !checkValue(email, value: "adresse email") { return }

Add a Comment

Replies

func checkValue(_string: String, value: String) -> Bool {
  let isNotEmotv = string != ""

There is an inconsistency between parameter declaration in func and the var used in the test.

You use the wrong parameter name in test. Or you have a missing space between _ and string. Which causes the compiler to look for a parameer named _string and not string.

First option, change the test on string to a test on _string:

func checkValue(_string: String, value: String) -> Bool {
  let isNotEmotv = _string != ""  // <<-- add _

or second option, change the parameter definition in the func declaration

func checkValue(_ string: String, value: String) -> Bool {  // <<-- add space
  let isNotEmotv = string != ""

Second option is preferrable, but then you need to change the calls as:

    checkValue(email, value: "adresse email"
    checkValue(password, value: "Mot de passe")

However, signIn is bizarre.

You call checkValue which is a Bool, not an optional. So guard does not make sense here.

 func signIn(email: String, password: String) {
    guard checkValue(_string: email, value: "adresse email") else { return }
    guard checkValue(_string: password, value: "Mot de passe") else { return }
  }

What do you want to do if check are not OK ?

Your code should probably be (if you select second option):

 func signIn(email: String, password: String) -> Bool {
  
    if checkValue(email, value: "adresse email") && checkValue(password, value: "Mot de passe") {
    // Do something when OK
    return true
   } else {
      // Not OK, alert user that if failed
      return false
   }
 }
  • Just a precision: guard is unneeded, as you have no value to guard ; you could simply write if !checkValue(email, value: "adresse email") { return }

Add a Comment

I agree with @Claude31's answer, but I also want to say, don't name variables like reserved keywords. Don't use string to represent a String. Give it a name that suits the data it holds, like you've done with errorString.

Thank you so much!