2

I'm pretty new to iOs development and I am trying to follow this tutorial

Everything seemed to be working alright but when i created the ModelManager class the code fails to build with the following errors

class ModelManager{
    let sharedInstance = ModelManager()
    class func getInstance() -> ModelManager
    {
        if(sharedInstance.database == nil) //Instance member 'sharedInstance' cannot be used on type 'ModelManager'
        {
            sharedInstance.database = FMDatabase(path: Utility.getPath("FirstAscent.sqlite")) ///Use of unresolved identifier 'FMDatabase'
        }
        return sharedInstance //Instance member 'sharedInstance' cannot be used on type 'ModelManager'
    }

}

Earlier in the tutorial I also could not find the libsqlite3.0.dylib file to link it as a binary with my libraries but I have linked the .tbd file instead.

I also copied the fmdb into the folder in my project as directed.

Any suggestions would be appreciated

Xitcod13
  • 5,949
  • 9
  • 40
  • 81

1 Answers1

2
  1. The problem is that sharedInstance should be defined as a static within the class, e.g.:

    static let sharedInstance = ModelManager()
    

    And then you could reference ModelManager.sharedInstance (not just sharedInstance) whenever you need reference to this singleton.

    Looking at this code sample, I think the author intended you to implement this sharedInstance as a global, but I think that's poor decision. It's better to make it a class property of the ModelManager, to avoid polluting your namespace.

    While we're talking about design choices, this tutorial's use of getInstance method is a poor design choice. First, it's not thread-safe. Second, it also breaks the singleton pattern (i.e. you have to reference this method, not the sharedInstance property ... his code samples use both, which is really bad). I'd suggest excising getInstance from the code entirely, and incorporate the initialization of the FMDatabase instance in the init method for ModelManager. Then, you can reference ModelManager.sharedInstance everywhere you need access to this singleton, and it eliminates the thread safety issues and the dependence on getInstance.

  2. Regarding the .dylib vs .tbd files, that is fine. Xcode used to provide dylib files, but many have been replaced with these tbd files. See https://stackoverflow.com/a/32115656/1271826.

  3. If you still see the "Use of unresolved identifier 'FMDatabase'" error, make sure that

    • you included the FMDB *.m files into your target (which you can confirm by going to "Compile Sources" section of the "Build Phases" tab under the target settings); and

    • make sure to include #import "FMDB.h" in your bridging header file.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • thank you for the answer. I am now having problems with a file inside the FMDB that is trying to import XCTest/XCTest.h I get an error that this file cannot be found. I tried looking online for a solution but I cannot seem to find how to fix it. – Xitcod13 Jul 26 '16 at 22:14
  • You generally only use `XCTest` in unit tests. Are you doing unit tests? If the code in question is not being used for a unit test, you shouldn't be importing `XCTest`. I wonder if whether, when you added FMDB to your project, whether you accidentally may have copied too many unrelated files (e.g. the unit tests that FMDB itself uses in its own project). Make sure to only include the contents of the `src/fmdb` folder in your app. – Rob Jul 26 '16 at 22:16
  • The code is part of the FMDB project that i copied over to my project. I'm not running any tests. – Xitcod13 Jul 26 '16 at 22:24
  • Yep, so you probably copied some stuff over that you didn't mean to. The only files that should have been added to your project are the `.m` and `.h` files in the [src/fmdb](https://github.com/ccgus/fmdb/tree/master/src/fmdb) folder (but not the `info.plist` ... your project already has an `info.plist`). Just remove all of those tests that were accidentally added to your project (as well as anything else you accidentally included) and you should be good. – Rob Jul 26 '16 at 22:37
  • Yes I did I copied the project instead of the folder by mistake everything works fine now. I will still most likely scratch everything to make it thread safe but ive learned quite a bit thank you :) – Xitcod13 Jul 26 '16 at 22:45