swift 简单的应用程序总是在模拟器上崩溃

swift simple app always crashing on simulator

我的应用基本上是一个带有本地存储的网络应用的网络视图。

为了支持URL-Scheme映射,我在viewDidLoad上调用了下面的函数来订阅UIApplicationDidBecomeActiveNotification,然后当我收到这样的消息,我将一个 webapp 加载到 UIWebView:

/**
* Setup the observer to get notifications about app activation
*/

func initObserver() {
    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "initWebApp",
        name: UIApplicationDidBecomeActiveNotification,
        object: nil);
}

/**
* Launches the WebApp, this is called from the observer, 
* and appDelegate.appParameters has possible parameters to the webapp
*/

func initWebApp() {
    var appDelegate=UIApplication.sharedApplication().delegate as AppDelegate;
    println ("- Launch Parameters: "+appDelegate.initParameters);
    let path=appDelegate.initParameters,
        url=NSURL(string: URL_BASE + "?" + (path ?? "")),
        requestObj=NSURLRequest(URL: url!);
    mWebView.loadRequest(requestObj);

}

但是,当我以这种方式(来自观察到的处理程序)初始化 webview 时,只要我单击 window.

上的任意位置,模拟器总是会崩溃并显示下面包含的堆栈跟踪

相反,如果我不订阅事件,只是从viewDidLoad()调用initWebApp(),一切正常(显然URL 方案映射不起作用,但我的意思是 webview 及其应用程序 100% 工作而不会崩溃)

任何人都可以看看这个堆栈跟踪(它总是一样的)并给我任何指示吗?

从事件处理程序中操作 UI 对象(例如 UIWebView)是一种不好的做法吗? (我来自 Android,在那里你经常需要在类似情况下更换线程)

2015-04-06 02:49:34.907 FunqTV[14285:518122] -[NSURL _effectiveStatusBarStyleViewController]: unrecognized selector sent to instance 0x7ae1cac0
2015-04-06 02:49:34.919 FunqTV[14285:518122] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL _effectiveStatusBarStyleViewController]: unrecognized selector sent to instance 0x7ae1cac0'
*** First throw call stack:
*** First throw call stack:
(
    0   CoreFoundation                      0x00648466 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x02037a97 objc_exception_throw + 44
    2   CoreFoundation                      0x006502c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x00598bc7 ___forwarding___ + 1047
    4   CoreFoundation                      0x0059878e _CF_forwarding_prep_0 + 14
    5   UIKit                               0x00fd3fa8 +[UIViewController _currentStatusBarStyleViewController] + 75
    6   UIKit                               0x00e63c81 -[UIApplication _updateCurrentStatusBarViewControllerAppearance] + 59
    7   UIKit                               0x00eb06ea -[UIWindow _updateContextOrderingAndSetLayerHidden:] + 548
    8   UIKit                               0x00eade74 -[UIWindow _initWithFrame:debugName:scene:attached:] + 336
    9   UIKit                               0x00eadd1e -[UIWindow _initWithFrame:debugName:attached:] + 79
    10  UIKit                               0x00eadc7f -[UIWindow _initWithFrame:attached:] + 71
    11  UIKit                               0x011e6f27 -[UIAutoRotatingWindow _initWithFrame:attached:] + 80
    12  UIKit                               0x00eadc33 -[UIWindow initWithFrame:] + 63
    13  UIKit                               0x011e6eba -[UIAutoRotatingWindow initWithFrame:] + 72
    14  UIKit                               0x011e4e2a -[UITextEffectsWindow initWithFrame:] + 72
    15  UIKit                               0x00eae09a -[UIWindow initWithContentRect:] + 164
    16  UIKit                               0x011e4b12 -[UITextEffectsWindow(UIObjectsForPerScreen) _basicInitWithScreen:options:] + 141
    17  UIKit                               0x011e4bcf -[UITextEffectsWindow(UIObjectsForPerScreen) _initWithScreen:options:] + 182
    18  UIKit                               0x016fcf8c +[_UIObjectPerScreen objectOfClass:forScreen:withOptions:createIfNecessary:] + 556
    19  UIKit                               0x011e645c +[UITextEffectsWindow _sharedTextEffectsWindowforScreen:aboveStatusBar:allowHosted:matchesStatusBarOrientationOnAccess:] + 416
    20  UIKit                               0x011e65b4 +[UITextEffectsWindow sharedTextEffectsWindowForScreen:] + 121
    21  UIKit                               0x013d173a -[UIPeripheralHost(UIKitInternal) containerWindow] + 156
    22  UIKit                               0x013d178a -[UIPeripheralHost(UIKitInternal) containerRootController] + 34
    23  UIKit                               0x013c6e15 -[UIPeripheralHost currentState] + 38
    24  UIKit                               0x013c6f10 -[UIPeripheralHost isOnScreen] + 35
    25  UIKit                               0x013d3a6a -[UIPeripheralHost(UIKitInternal) _isCoordinatingWithSystemGestures] + 82
    26  UIKit                               0x00e70da1 -[UIApplication _shouldDelayTouchesForControlCenter] + 56
    27  UIKit                               0x00ebd506 -[UIWindow _shouldDelayTouchForSystemGestures:] + 41
    28  UIKit                               0x0128f8e8 -[_UISystemGestureGateGestureRecognizer touchesBegan:withEvent:] + 498
    29  UIKit                               0x00eb3946 -[UIWindow _sendGesturesForEvent:] + 567
    30  UIKit                               0x00eb4abf -[UIWindow sendEvent:] + 769
    31  UIKit                               0x00e79bb1 -[UIApplication sendEvent:] + 242
    32  UIKit                               0x00e89bf6 _UIApplicationHandleEventFromQueueEvent + 21066
    33  UIKit                               0x00e5dbc7 _UIApplicationHandleEventQueue + 2300
    34  CoreFoundation                      0x0056b98f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    35  CoreFoundation                      0x0056149d __CFRunLoopDoSources0 + 253
    36  CoreFoundation                      0x005609f8 __CFRunLoopRun + 952
    37  CoreFoundation                      0x0056037b CFRunLoopRunSpecific + 443
    38  CoreFoundation                      0x005601ab CFRunLoopRunInMode + 123
    39  GraphicsServices                    0x040152c1 GSEventRunModal + 192
    40  GraphicsServices                    0x040150fe GSEventRun + 104
    41  UIKit                               0x00e619b6 UIApplicationMain + 1526
    42  FunqTV                              0x0007dfbe top_level_code + 78
    43  FunqTV                              0x0007dffb main + 43
    44  libdyld.dylib                       0x027a5ac9 start + 1
    45  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如果我将对 initWebApp() 的调用包装到 dispatch_async() 块中,它会起作用:

   /**
    * Setup the observer to get notifications about app activation
    */

    func initObserver() {
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "requestInitWebApp",
            name: UIApplicationDidBecomeActiveNotification,
            object: nil);
    }

   /**
    * Called from the observer. It now dispatches async a call to the init
    * function rather than calling it directly from the observer thread.
    */

    func requestInitWebApp() {
        println ("Dispatching async call to init webview");
        dispatch_async(dispatch_get_main_queue()) {
            self.initWebApp();
        }
    }