Fatal error: unexpectedly found nil while unwrapping an Optional value

386 views
Skip to first unread message

Chris Cannon

unread,
Nov 5, 2014, 11:24:34 AM11/5/14
to byu-coc...@googlegroups.com
Hello everyone,

I wanted to first explain what I trying to do. 


I would like to subviews(3 different UIViewControllers) for each of the pages. 

I understand that I need to have the code look something like this:

self.view1 = self.storyboard?.instantiateInitialViewController("View1") as UIViewController
self.addChildViewController(view1)
view1.willMoveToParentViewController(self)
view1.frame = CGRectMake(0, 0, wBounds, hBounds)
           self.scrollView.addSubview(view1)
           self.scrollView.bringSubviewToFront(view1)


This code is not showing anything. Please help. 

Joshua Dutton

unread,
Nov 5, 2014, 12:07:49 PM11/5/14
to byu-coc...@googlegroups.com
It looks like your storyboard is nil (that's the optional value you are unwrapping). Any idea why that's the case?

Your code would be safer if you checked the optional first with optional chaining like this:

if let myViewController = self.storyboard?.instantiateInitialViewController("View1") {
self.view1 = myViewController
// do stuff with it
} else {
// value was nil
}


--
For our job posting guidelines see http://cocoaheads.byu.edu/newsite/jobPostingGuidelines.html
Be sure to visit our website at http://cocoaheads.byu.edu
---
You received this message because you are subscribed to the Google Groups "BYU CocoaHeads" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byu-cocoahead...@googlegroups.com.
To post to this group, send email to byu-coc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Cannon

unread,
Nov 5, 2014, 12:25:42 PM11/5/14
to byu-coc...@googlegroups.com
Well that would make sense to do. I am sorry I am new to iOS Development.

This is what I change it to.

if let view1 = self.storyboard?.instantiateInitialViewController("View1"){
        
        self.addChildViewController(view1)
        view1.willMoveToParentViewController(self)
        view1.frame = CGRectMake(0, 0, wBounds, hBounds)
        self.scrollView.addSubview(view1)
        self.scrollView.bringSubviewToFront(view1)
        }
I am getting an error of " Type '()' does not conform to protocol 'StringLiteralConvertible' "

Why would I get that?

Joshua Dutton

unread,
Nov 5, 2014, 6:26:50 PM11/5/14
to byu-coc...@googlegroups.com
instantiateInitialViewController() doesn't take any arguments. You are looking for instantiateViewControllerWithIdentifier(identifier: String). Next time look at the docs:


As a new iOS developer, I recommend you get really comfortable with reading Apple's documentation. It's really extensive and is one of the best sources for learning Apple's frameworks. It's built right into XCode and also really easy to google. This book by Big Nerd Ranch is good at teaching people to use the docs while learning iOS development:


That edition teaches iOS development in Objective-C. You'll have to wait until they release a Swift version, but I would recommend getting the Objective-C version anyway to help you understand the majority of the iOS resources currently out there.
Reply all
Reply to author
Forward
0 new messages