Search code examples
iosswift2fmdb

Use of unresolved identifier 'Model' in swift


In my Swift project I have created class named Model:

class Model : NSObject
{
    func createDatabase(nameOfTheDataBase: String  ) ->String
    {
     // do something and return String
    }
}

and when I tried to use this class in another class

  public class User
   {
     var databasePath = String()
     var path : String?
     public func create() -> String
    {
     databasePath = Model().createDatabase("ShoppingPad.sqlite")
    // return something
    }
  }

This gives an error

Use of unresolved identifier 'Model'

I tried to clean and then build my project. But error reappears.


Solution

  • If you're going to do more than just return the database path, you should look into singletons as a means of making the database functions accessible to all classes. There are lots of tutorials and examples out there, but it looks like this

    class Model
    {
        static let sharedInstance: Model = {
            let instance = Model()
            // setup code
            return instance
        }()
    
        func createDatabase(nameOfTheDataBase: String  ) ->String
        {
            // do something and return String
            return "Bob"
        }
    
    }
    
    public class User
    {
         var databasePath = String()
         var path : String?
    
        public func create() -> String
        {
            databasePath = sharedModel.createDatabase("ShoppingPad.sqlite")
            // return something
            return databasePath
        }
    }