How to solve problem using a variable outside a function

Hello:


I am having some difficulty unstanding why my variable doesn't work outside a function:

Here is my code:


let fetchRequest = NSFetchRequest(entityName: "SCQ")
        fetchRequest.returnsObjectsAsFaults = false
       
        let nameGrade =  gradeValue.stringValue
        print ("Name Grade From Declaration is \(nameGrade)")
        let subjectSelected = subjectValue.stringValue
       
        func gradeToFetch() {
            if nameGrade == "2" || nameGrade == "3" {
              let nameGrade = "2"
             
              print ("Name Grade  First Try is \(nameGrade)")
        }
      
       
       gradeToFetch()
       
        let predicate1 = NSPredicate(format: "grade = %@",nameGrade)
        let predicate2 = NSPredicate(format: "subject = %@",subjectSelected)
       
        fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate1, predicate2])
       
        do {
           
            items =  try managedContext.fetch(fetchRequest) as! [NSManagedObject]
            print ("selected grade is \(nameGrade)")
            print ("selected subject is \(subjectSelected)")
            print ("Group Name is \(nameGroup)")
            print ("Records Selected = \(items.count)")
           
        } catch let error as NSError {
           
            print("Could not fetch. \(error), \(error.userInfo)")
        }


I want the value entered (line 4) to be the value converted by the function (lines 8 - 13) in the predicate (line 21) but i am getting the value entered in the predicate. (the output below shows 2 as the converted value but the predicate uses 3)

This is the log:


Name Grade From Declaration is 3 // line 5

Name Grade First Try is 2 // line 10

Answered by Claude31 in 407397022

A bit hard to follow your code. Did you edit the code, introducing inconsistencies, or is it the full reall code ?


But, I noticed, with proper indentation:


func gradeToFetch() {
    if nameGrade == "2" || nameGrade == "3" {
        let nameGrade = "2"
       
        print ("Name Grade  First Try is \(nameGrade)")
    }
   
   
    gradeToFetch()



There seems to miss a closing curly bracket line 7.

Hence you call gradeToFetch() inside gradeToFetch(). Probably not your intent.


Once this is corrected, nameGrade is redeclared line 3.

Hence, its ne value is lost once exiting the func.


Did you want to write :


var nameGrade =  gradeValue.stringValue          // var to be able to change
print ("Name Grade From Declaration is \(nameGrade)")
let subjectSelected = subjectValue.stringValue
 
func gradeToFetch() {
    if nameGrade == "2" || nameGrade == "3" {
        nameGrade = "2"     // Do not redeclare here
       
        print ("Name Grade  First Try is \(nameGrade)")
    }
}

gradeToFetch()
Accepted Answer

A bit hard to follow your code. Did you edit the code, introducing inconsistencies, or is it the full reall code ?


But, I noticed, with proper indentation:


func gradeToFetch() {
    if nameGrade == "2" || nameGrade == "3" {
        let nameGrade = "2"
       
        print ("Name Grade  First Try is \(nameGrade)")
    }
   
   
    gradeToFetch()



There seems to miss a closing curly bracket line 7.

Hence you call gradeToFetch() inside gradeToFetch(). Probably not your intent.


Once this is corrected, nameGrade is redeclared line 3.

Hence, its ne value is lost once exiting the func.


Did you want to write :


var nameGrade =  gradeValue.stringValue          // var to be able to change
print ("Name Grade From Declaration is \(nameGrade)")
let subjectSelected = subjectValue.stringValue
 
func gradeToFetch() {
    if nameGrade == "2" || nameGrade == "3" {
        nameGrade = "2"     // Do not redeclare here
       
        print ("Name Grade  First Try is \(nameGrade)")
    }
}

gradeToFetch()

Thanks Claude:


You make it seem so easy!

How to solve problem using a variable outside a function
 
 
Q