[英]iOS Swift : EXC_BAD_ACCESS(code=EXC_i386_GPFLT ) regarding a singleton
My iOS app is returning this error. 我的iOS应用返回此错误。
EXC_BAD_ACCESS(code=EXC_i386_GPFLT ) EXC_BAD_ACCESS(代码= EXC_i386_GPFLT)
This is occuring on return Singleton.instance
Here is the code regarding the singleton I am using. 这是在return Singleton.instance
时发生的。这是有关我正在使用的return Singleton.instance
的代码。
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. 任何建议或提示,不胜感激。
您可以将所有代码替换为以下代码:
static let sharedData = SharedData()
With Swift 1.2 there is an easier option to create singletons now: 使用Swift 1.2,现在有一个更简单的选项来创建单例:
class DataManager {
static let sharedInstance = DataManager()
/// To deny direct access, make your init function private if you want
private init() {
}
}
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 ;) 不要因为误报而堕落;)
I had a badly named function in my Swift singleton class, that must have been tripping up ARC when it was called. 我的Swift单例类中有一个命名错误的函数,该函数在调用ARC时一定会跳闸。 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. 将名称从init
更改为initialize
可解决EXC_BAD_ACCESS
错误。 I hope this helps to save someone else some time. 我希望这有助于节省其他人的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.