ios Swift 项目因线程 1 崩溃:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25353790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift project crashing with Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x0)
提问by SamG
It looks like a bad memory access, like trying to access an object that does not exist. I tried using NSZombie to see if something came up, as far as I could tell nothing did. It is crashing at the declaration for the app delegate.
它看起来像一个错误的内存访问,就像试图访问一个不存在的对象。我尝试使用 NSZombie 来查看是否出现了问题,据我所知没有任何反应。它在应用程序委托的声明处崩溃。
AppDelegate.swift
AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool
{
// Override point for customization after app launches
Parse.setApplicationId("removed on purpose", clientKey: "removed on purpose")
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
PFFacebookUtils.initializeFacebook()
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool
{
return FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication, withSession: PFFacebookUtils.session())
}
func applicationDidBecomeActive(application: UIApplication)
{
FBAppCall.handleDidBecomeActiveWithSession(PFFacebookUtils.session())
}
func applicationWillResignActive(application: UIApplication)
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication)
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication)
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationWillTerminate(application: UIApplication)
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
DashboardViewController.swift
DashboardViewController.swift
import UIKit
class DashboardViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view
}
}
Using breakpoints I have determined that it is not even getting past the class declaration for the app delegate. I tried checking all of the classes in my Main.storyboard file as well to make sure everything was linked properly, again as far as I can tell it is.
使用断点我已经确定它甚至没有通过应用程序委托的类声明。我也尝试检查 Main.storyboard 文件中的所有类,以确保所有内容都正确链接,再次就我所知。
采纳答案by Klaas
I ran into the same issue today. As of Xcode 6 beta 6 the auto complete suggests:
我今天遇到了同样的问题。从 Xcode 6 beta 6 开始,自动完成提示:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool {}
This crashes at startup with an EXC_BAD_ACCESS and a blank screen.
这会在启动时崩溃,并显示 EXC_BAD_ACCESS 和空白屏幕。
As soon as an !
is added to the last argument, everything works fine:
将 an!
添加到最后一个参数后,一切正常:
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]!) -> Bool {}
In current documentationthe !
is missing as well:
在当前文档中!
也缺少:
optional func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]) -> Bool
回答by King-Wizard
Works with Xcode 6.1:
适用于 Xcode 6.1:
Try
尝试
PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
instead of
代替
PFAnalytics.trackAppOpenedWithLaunchOptions()
回答by C?ur
Solution from OP.
来自 OP 的解决方案。
Problem solved by fixing the code as below.
通过修复如下代码解决了问题。
In all method signatures, replace:
在所有方法签名中,替换:
application: UIApplication
with:
和:
application: UIApplication!
And in application:didFinishLaunchingWithOptions:
, replace:
在 中application:didFinishLaunchingWithOptions:
,替换:
launchOptions: [NSObject : AnyObject]
with:
和:
launchOptions: NSDictionary!