Something strange happens when assigning a name to UIViewController

I have some viewControllers each with associated segues. RootViewController is connected to NavigationController. Now, in one UIViewController, I am doing a segue to navigate to another UIViewController like this and it works:

self.performSegueWithIdentifier("toLoginTypeActivity", sender: self)

      

The UIViewController that I am attached to after the segues is executed is:

import Foundation
import UIKit

class LoginTypeActivityViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        println("I am here.")
        //self.title = "Namaskar"         // Here seems to be something strange.
        println("I am here also.")
    }
}

      

But, the segue doesn't work if I comment self.title

in the code (marked as strange). All println works, though. Even: "I'm here too." printed. But segues are not executed. This gives a runtime error.

What should be the problem and how to solve it? I'm sure I named the identifier correctly.

Here is a runtime error:

2014-10-13 17:21:27.878 mobilepay[29387:1382271] -[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7b6b33d0
2014-10-13 17:21:27.881 mobilepay[29387:1382271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7b6b33d0'
*** First throw call stack:
(
0   CoreFoundation                      0x00bb5df6 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x0083fa97 objc_exception_throw + 44
2   CoreFoundation                      0x00bbda75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x00b069c7 ___forwarding___ + 1047
4   CoreFoundation                      0x00b0658e _CF_forwarding_prep_0 + 14
5   UIFoundation                        0x067eeb91 __NSStringDrawingEngine + 29221
6   UIFoundation                        0x067e784d -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 171
7   UIKit                               0x013be151 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 6626
8   UIKit                               0x013bbe30 -[UILabel drawTextInRect:] + 581
9   UIKit                               0x013be256 -[UILabel drawRect:] + 98
10  UIKit                               0x0123354b -[UIView(CALayerDelegate) drawLayer:inContext:] + 519
11  QuartzCore                          0x01077d51 -[CALayer drawInContext:] + 118
12  QuartzCore                          0x01077c87 _ZL16backing_callbackP9CGContextPv + 96
13  QuartzCore                          0x00f5c7ae CABackingStoreUpdate_ + 2788
14  QuartzCore                          0x01077c1f ___ZN2CA5Layer8display_Ev_block_invoke + 93
15  QuartzCore                          0x010ad406 x_blame_allocations + 15
16  QuartzCore                          0x01077a85 _ZN2CA5Layer8display_Ev + 1591
17  QuartzCore                          0x01077cd6 -[CALayer _display] + 33
18  QuartzCore                          0x01077446 _ZN2CA5Layer7displayEv + 142
19  QuartzCore                          0x01077cb0 -[CALayer display] + 33
20  QuartzCore                          0x0106bee6 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 322
21  QuartzCore                          0x0106bf6c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
22  QuartzCore                          0x00fca676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
23  QuartzCore                          0x00fcba3c _ZN2CA11Transaction6commitEv + 392
24  QuartzCore                          0x00fcc108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
25  CoreFoundation                      0x00ad8fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26  CoreFoundation                      0x00ad8f00 __CFRunLoopDoObservers + 400
27  CoreFoundation                      0x00ace93a __CFRunLoopRun + 1226
28  CoreFoundation                      0x00ace1ab CFRunLoopRunSpecific + 443
29  CoreFoundation                      0x00acdfdb CFRunLoopRunInMode + 123
30  GraphicsServices                    0x0317124f GSEventRunModal + 192
31  GraphicsServices                    0x0317108c GSEventRun + 104
32  UIKit                               0x011a8e16 UIApplicationMain + 1526
33  mobilepay                           0x001410de top_level_code + 78
34  mobilepay                           0x0014111b main + 43
35  libdyld.dylib                       0x03ad7ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

      

Note that the assignment self.title

is done in other ViewControllers.

Edit:

Here's the updated code.

class LoginTypeActivityViewController: UIViewController{

    var delegate = UIApplication.sharedApplication().delegate as AppDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
        println("viewDidLoad self.title = \(self.title)")
        println("viewDidLoad self.navigationItem.title = \(self.navigationItem.title)")
    }

    override func viewWillAppear(animated: Bool) {
        println("viewWillAppear self.title = \(self.title)")
        println("viewWillAppear self.navigationItem.title = \(self.navigationItem.title)")
    }

    override func viewDidAppear(animated: Bool) {
        println("viewDidAppear self.title = \(self.title)")
        println("viewDidAppear self.navigationItem.title = \(self.navigationItem.title)")
        self.navigationItem.title = "Koshish"
        println("printing in viewDidAppear after setting self.navigatioItem.title = \(self.navigationItem.title)")
}

      

}

And here's the output:

viewDidLoad self.title = nil
viewDidLoad self.navigationItem.title = nil
viewWillAppear self.title = nil
viewWillAppear self.navigationItem.title = nil
viewDidAppear self.title = nil
viewDidAppear self.navigationItem.title = nil
printing in viewDidAppear after setting self.navigatioItem.title = Optional("Koshish")
2014-10-14 12:46:48.931 mobilepay[34547:1593230] -[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7a162090
2014-10-14 12:46:48.933 mobilepay[34547:1593230] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7a162090'
*** First throw call stack:
(
0   CoreFoundation                      0x00bd3df6 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x0085da97 objc_exception_throw + 44
2   CoreFoundation                      0x00bdba75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x00b249c7 ___forwarding___ + 1047
4   CoreFoundation                      0x00b2458e _CF_forwarding_prep_0 + 14
5   UIFoundation                        0x0680cb91 __NSStringDrawingEngine + 29221
6   UIFoundation                        0x0680584d -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 171
7   UIKit                               0x013dc151 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 6626
8   UIKit                               0x013d9e30 -[UILabel drawTextInRect:] + 581
9   UIKit                               0x013dc256 -[UILabel drawRect:] + 98
10  UIKit                               0x0125154b -[UIView(CALayerDelegate) drawLayer:inContext:] + 519
11  QuartzCore                          0x01095d51 -[CALayer drawInContext:] + 118
12  QuartzCore                          0x01095c87 _ZL16backing_callbackP9CGContextPv + 96
13  QuartzCore                          0x00f7a7ae CABackingStoreUpdate_ + 2788
14  QuartzCore                          0x01095c1f ___ZN2CA5Layer8display_Ev_block_invoke + 93
15  QuartzCore                          0x010cb406 x_blame_allocations + 15
16  QuartzCore                          0x01095a85 _ZN2CA5Layer8display_Ev + 1591
17  QuartzCore                          0x01095cd6 -[CALayer _display] + 33
18  QuartzCore                          0x01095446 _ZN2CA5Layer7displayEv + 142
19  QuartzCore                          0x01095cb0 -[CALayer display] + 33
20  QuartzCore                          0x01089ee6 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 322
21  QuartzCore                          0x01089f6c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
22  QuartzCore                          0x00fe8676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
23  QuartzCore                          0x00fe9a3c _ZN2CA11Transaction6commitEv + 392
24  QuartzCore                          0x00fea108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
25  CoreFoundation                      0x00af6fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26  CoreFoundation                      0x00af6f00 __CFRunLoopDoObservers + 400
27  CoreFoundation                      0x00aec93a __CFRunLoopRun + 1226
28  CoreFoundation                      0x00aec1ab CFRunLoopRunSpecific + 443
29  CoreFoundation                      0x00aebfdb CFRunLoopRunInMode + 123
30  GraphicsServices                    0x0318f24f GSEventRunModal + 192
31  GraphicsServices                    0x0318f08c GSEventRun + 104
32  UIKit                               0x011c6e16 UIApplicationMain + 1526
33  mobilepay                           0x00160f5e top_level_code + 78
34  mobilepay                           0x00160f9b main + 43
35  libdyld.dylib                       0x03af5ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

      

solved

I finally found the problem in my code:

In my didFinishLaunchingWithOptions I did this:

var navigationBarAppearance = UINavigationBar.appearance()
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
navigationBarAppearance.tintColor = UIColor(rgba: "#000000", alpha: 1.0)
navigationBarAppearance.barTintColor = UIColor(rgba: "#000000", alpha: 1.0)
navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial-BoldMT", size: 20.0), NSForegroundColorAttributeName: "#000000", alpha: 1.0)]

      

As you can see, in the NSForegroundColorAttributeName, I just set the string and didn't call the UIColor function with it.

I feel like Swift should have given me some hint that I am not calling NSForegroundColorAttribute with the correct value.

+3


source to share


1 answer


I cannot give you an answer, but there are suggestions on how you can solve it. Somehow the title property of your UINavigationItem was set for some object and not the NSString.

So, if you have the string "//self.title", write down the title value, the title value of your "UINavigationItem" class, and if it is not zero, then write down the class. I'm guessing it's somehow assigned a number, not a string, but it might just be garbage or something thrown out. Or your storyboard file might be damaged.



EDIT: So something is really weird. Other ideas:

  • if this session is started before the ViewDidLoad in the View, strange things can happen.

  • implement 'override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject?)' in the calling class and then check that you are on the main thread

  • use a debugger, add an exception breakpoint and when you crash, check that the crash is enabled in Thread 0. Also, poke around - there is an object that doesn't respond to a selector, you have its address, so do "po 0x ....... "in the debugger and see what it is.

  • Otherwise, you need to take some time and try to create a demo project, modeled after your real one, which has the same problem. If you can do that, you can upload it to a public site like Dropbox, update your question and people like me will be looking at it (I will at least do that). Its traditional, if you get to this point to offer some reward, I'm just saying that you know, but I will look at it without it (I don't need more points).

0


source







All Articles