1 Reply
      Latest reply: Nov 30, 2016 2:16 AM by eskimo RSS
      JayCo Level 1 Level 1 (5 points)

        I'm trying to implement a coreData class to handle all the database queries - The problem is that I'm getting an error saying that "value of type 'dataModel' has no member 'context'  which is on line 15

         

        My current code is:

        import UIKit
        import CoreData
        class dataModel: NSObject {
            override init() {
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                let context = appDelegate.persistentContainer.viewContext
              
                let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Item")
                request.returnsObjectsAsFaults = false
            }
          
            func findItems() {
              
                do {
                    let results = try self.context.fetch(self.request)
                  
                    if results.count > 0 {
                      
                        for result in results {
                          
                            if let itemID = result.objectWithID(id) as? Item {
                                print(itemID)
                            }
                          
                            if let itemName = result.value(forKey: "title") as? String {
                                print(itemName)
                            }
                          
                            if let itemDesc = result.value(forKey: "content") as? String {
                                print(itemDesc)
                            }
                          
                        }
                      
                    }
                  
                } catch {
                    print("APP: Error getting data \(error.localizedDescription)")
                }
                     
              
            }
          
          
        
          
        }
        
        

         

        I think I'm in the right direction, just can't seem to get this area to work.

        Thanks

        • Re: Implement coreData class issue
          eskimo Apple Staff Apple Staff (6,005 points)

          I’m not sure how you’re expecting this to work but I suspect that the problem here is line 6.  This declares a local constant context, whose scope does not extend because the initialiser in which it’s contained.  Thus, line 15 can’t access self.context.

          If you want context to be a constant property of dataModel, you’ll need to declare things like this:

          class dataModel … {
          
              override init() {
                  …
                  self.context = …
                  …
              }
          
              let context: NSManagedObjectContext
          
              …
          }
          

          ps In Swift, type names begin with a capital, so dataModel should be DataModel.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"