1

My iOS app is returning this error.

EXC_BAD_ACCESS(code=EXC_i386_GPFLT )

This is occuring on return Singleton.instance Here is the code regarding the singleton I am using.

class var sharedData : SharedData {
    struct Singleton {
        static let instance = SharedData()
    }

    return Singleton.instance
}

Can someone help me understand this error and help me resolve it? Any suggestions or tips are appreciated it.

William Alex
  • 385
  • 1
  • 3
  • 14
  • Is this question a duplicate of another one you posted later? http://stackoverflow.com/questions/29083179/ios-swift-calling-a-singleton-method-3-times-causes-app-to-crash – endavid May 24 '16 at 22:20

4 Answers4

2

With Swift 1.2 there is an easier option to create singletons now:

class DataManager {
    static let sharedInstance = DataManager()

    /// To deny direct access, make your init function private if you want
    private init() {
    }
}
HorseT
  • 6,592
  • 2
  • 18
  • 16
2

I was using a singleton as others have mentioned above,

static let sharedData = SharedData()

and it was crashing on a real device but not in the simulator. It turns out that I just needed to clean the project and rebuild. Don't fall for false positives ;)

endavid
  • 1,781
  • 17
  • 42
1

You can replace all your code with the following:

static let sharedData = SharedData()
bzz
  • 5,556
  • 24
  • 26
-1

I had a badly named function in my Swift singleton class, that must have been tripping up ARC when it was called. This class initializes another class from a file, and so I ended up with this signature:

func initOtherClass(otherClass: NSObject, URL fileURL: NSURL) -> Bool

Whoops. Changing the name from init to initialize solved the EXC_BAD_ACCESS errors. I hope this helps to save someone else some time.

Dov
  • 15,530
  • 13
  • 76
  • 177