Bad Code

Hello: I have the code below to try to get a random selection from CoreData store. However i'm getting a crash on line 33 with the error:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x232). After studying the code for hours, I still can't figure out why the crash. The code actually worked one time!!!


let fetchRequest = NSFetchRequest(entityName: "TempSCQ")
        fetchRequest.includesPropertyValues = false
      
        var selector = Int()
        nameGrade =  gradeValue.stringValue
      
  print ("Grade entered is \(nameGrade)")
      
        func getRandomRecord () {
          
            if nameGrade == "2" {
                selector = Int.random(in: 0 ..< 355)
                        } else
                      
            if nameGrade == "4" {
                selector = Int.random(in: 0 ..< 418)
                        } else
                          
            if nameGrade == "6" {
                selector = Int.random(in: 0 ..< 375)
                        } else
                              
            if nameGrade == "8" {
                selector = Int.random(in: 0 ..< 813)
                        } else
                                  
            if nameGrade == "12" {
                selector = Int.random(in: 0 ..< 355)
                }
        }
        getRandomRecord()

        fetchRequest.predicate = NSPredicate(format: "qid == %@", selector) //Thread 1: EXC_BAD_ACCESS (code=1, address=0x232)
Answered by DTS Engineer in 399101022

Try changing this:

NSPredicate(format: "qid == %@", selector)

to this:

NSPredicate(format: "qid == %@", selector as NSNumber)

That predicate initialiser takes variable arguments, and support for variable arguments in Swift is kinda patchy. Specifically, Swift is unable to check the expected argument type, as determined by the format string, against the actual argument type. So it doesn’t know that the predicate is expecting an object, and instead passes an

Int
. Needless to say, that doesn’t end well.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

ps DTS is closed 21 Dec through 1 Jan.

Accepted Answer

Try changing this:

NSPredicate(format: "qid == %@", selector)

to this:

NSPredicate(format: "qid == %@", selector as NSNumber)

That predicate initialiser takes variable arguments, and support for variable arguments in Swift is kinda patchy. Specifically, Swift is unable to check the expected argument type, as determined by the format string, against the actual argument type. So it doesn’t know that the predicate is expecting an object, and instead passes an

Int
. Needless to say, that doesn’t end well.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

ps DTS is closed 21 Dec through 1 Jan.

Thanks eskimo:


No wonder you are a top scorer on the Forum!


Your suggestion was the solution.

Bad Code
 
 
Q