iOS prepareforsegue throwing xc_bad - ios

I'm using a prepareForSegue method,
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "roundSegue" {
var selectedRound:NSManagedObject = roundList[self.tableView.indexPathForSelectedRow().row] as NSManagedObject
let SVC: SetupVC = segue.destinationViewController as SetupVC
SVC.course = selectedRound.valueForKey("course") as String
SVC.existingRound = selectedRound
}
}
However, it is throwing an error "Thread 1:EXC_BAD_INSTRUCTION", on the 'if segue.identifier == "roundSegue"' line...any thoughts on why it would be throwing this error?

It is possible that your segue identifier is nil. If this is the case, you are attempting to unwrap a implicitly unwrapped optional variable that is nil, which would cause a crash. try this instead:
if let identifier = segue.identifier {
if identifier == "roundSegue" {
// your code here.
}
}

STUPID ME! I had an iboutlet that needed to be deleted. that was it. Thanks for all the help though.

In the Storyboard interface. Click the segue and open the attributes inspector
Set the seque identifier and the bad access should be fixed

Related

Can't assign Bool member of ViewController

For some reason this prepare code for a segue crashes because newGridViewController is nil. Any ideas?
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if segue.identifier == "Grid" {
if let newGridViewController = segue.destination as? GridViewController
if savePhotoWithSlicer.isOn {
newGridViewController?.savePhotoWithSlicer = true
if (newGridViewController?.savePhotoWithSlicer)! { print("TRUE") }
}
}
}
I guess the problem is let newGridController = segue.destination as? GridViewController
My thought is your GridViewController is nil. Since you force unwrap newGridViewController in this line if (newGridViewController?.savePhotoWithSlicer)! { print("TRUE") } the code crashes.
Show us some more code to give you detailed explanations.
You don't say what the exception is, but I imagine that it is "unexpectedly found nil..." caused by the force unwrap on the second last line,
I suspect that the root cause is the conditional downcast to GridViewController failed, so newGridViewController is actually nil. Since you use a conditional unwrap everywhere except the second last line, you don't get a crash until that point.
A better structure is to use an if let... conditional downcast:
if let newGridViewController = segue.destination as? GridViewController {
newGridViewController.savePhotoWithSlicer = savePhotoWithSlicer.isOn
if newGridViewController.savePhotoWithSlicer {
print("TRUE")
}
}
This will prevent the crash, but it probably still won't print "TRUE" as I strongly suspect that segue.destination is not a GridViewController - You need to check your storyboard and make sure that you have the right custom class for your scene.
UPDATE
Since you have now clarified that the segue leads to a navigation controller that embeds the GridViewController you can use this to get the desired view controller:
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if segue.identifier == "Grid" {
if let navController = segue.destination as? UINavigationController {
if let newGridViewController = navController.viewControllers.first as? GridViewController {
newGridViewController.savePhotoWithSlicer = savePhotoWithSlicer.isOn
if newGridViewController.savePhotoWithSlicer {
print("TRUE")
}
}
}
}
}
Objective c and Swift will happily let you "assign" a value to nil, and that's probably what you are doing. Just b/c you assign "true" to nil, doesn't mean you actually did anything ("true" just gets ignored). So when you force unwrap the nil, you still crash.

Value of type 'UIViewController' has no member 'jsonfile' named JSON

I currently have a button set to go to a TableViewController but decided I wanted to embed that TableViewController in a TabBarController. I am running into an error while trying to pass it to the UITabBarController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showListSegue") {
let tabBarController = segue.destinationViewController as! UITabBarController
tabBarController.selectedIndex = 0 // choose which tab is selected
let des = tabBarController.selectedViewController!
des.jsonfile = self.jsonfile
}
}
In the last line of code, des.jsonfile = self.jsonfile, I am getting the error...
Value of type 'UIViewController' has no member 'jsonfile'
I am trying to pass the jsonfile to the TableViewController which is now embedded in the UITabBarController. How can this be done? I have this variable in the TableViewController is was getting passed to but now that I threw this TabBarController in the mix I am getting all confused.
I also tried to create a Cocoa file for the TabBarcontroller and set the variable var jsonfile : JSON! but that did not work either. (That is the variable in my TableViewController that I want to pass it to) Please help. Thank you.
You need to let the compiler know that selectedViewController is a type with the member jsonFile. Also, you should check that it actually is existing and of the correct class at runtime. Here's the kind of pattern you should be using:
class JSONDisplayController: UIViewController {
var jsonfile: String
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showListSegue") {
guard let tabBarController = segue.destinationViewController as? UITabBarController else {
preconditionFailure("Unexpected destination.")
}
tabBarController.selectedIndex = 0 // choose which tab is selected
guard let des = tabBarController.selectedViewController as? JSONDisplayController else {
preconditionFailure("Unexpected selection.")
}
des.jsonfile = jsonfile
}
}

Pass data via 'prepareForSegue' in Swift 2

Trying to pass a string from one view controller to another view controller (MoreDetailViewController)
Coming up with this error :
unknown class MoreDetailViewController in Interface Builder file.
Could not cast value of type 'UIViewController' (0x10f4ae308) to 'ParseStarterProject_Swift.MoreDetailViewController' (0x10d512060).
(lldb)
Sorry if this is obvious, but I have only recently started coding. Thank you! :)
Code :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showDetail") {
let detailController : MoreDetailViewController = segue.destinationViewController as! MoreDetailViewController
detailController.receivedId = selectedId
}
}
Is your second view controller correctly set as a MoreDetailViewController in Interface Builder ?
You can check it easily : Open your .xib or .storyboard file, select your second view controller, and on the Identity Inspector (right panel, third tab) enter your class name.
The error is simply saying that the segue destination viewcontroller is not a MoreDetailViewController. You should also not force unwrap it, in which case it will crash if the object is not correct.
if segue.identifier == "showDetail" {
if let vc = segue.destinationViewController as? MoreDetailViewController {
vc.receivedId = selectedId
}
}
Edit
Did not see previous answer which was the exact same code. Sorry about that.
Try this :
if segue.identifier == "showDetail"
{
if let detailController = segue.destinationViewController as? MoreDetailViewController
{
detailController.receivedId = selectedId
}
}
Note: Don't forget to set viewcontroller class as MoreDetailViewController

Populating prepareForSegue's destinationViewController via returned type

Good afternoon!
I'm relatively new to Swift, though I think I've managed to wrap my head around most of it, however I'm having difficulties setting a segue's destinationViewController indirectly.
I understand that destinationViewController accepts AnyObject? but how do I go about returning the class as a function's return value directly to destinationViewController? Something like:
override func prepareForSegue( segue: UIStoryboardSegue, sender: AnyObject? ) {
if( segue.identifier == "nextView" ) {
let nextScene = segue.destinationViewController as? getNextView()
// ...blah blah blah...
}
}
Where getNextView() is overridden by a subclass whose sole purpose is to return a reference to the destinationViewController:
override func getNextView -> AnyObject! {
return SomeClassBasedOnUIViewController
}
XCode isn't happy I'm employing "consecutive statements" on one line in my prepareForSegue() and I'm at a loss how to resolve it so any help would be greatly appreciated!
Thanks!
EDIT:
Sorry about that, but getNextView() doesn't actually return an Optional. Just picked up on that and amended the question. The issue persists regardless, though.
What you're trying to do is illogical. This is a compile time indication of the class or protocol to which the reference must conform. Trying to get this reference at runtime won't help you. Instead you should be checking for conformance to a static protocol and then dispatching calls based on that protocol to an unknown implementation class.
override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject!) {
if segue.identifier == "nextView" {
let nextScene : BaseClassBasedOnUIViewController = segue.destinationViewController as! getNextView()
}
}
destinationViewController shouldn't be optional. Can you try this one.
override func getNextView -> AnyObject! {
return SomeClassBasedOnUIViewController
}

swift segue undeclared type breaks the code

I have been searching every where for a solution but i can't find it and when i look at my code and others it looks as it should be looking.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "helpUpdateProfile" ){
var detailVC = segue.destinationViewController as DetailVC;
detailVC.toPass = "help"
}
}
It seems to be just on the line that says
var detailVC = segue.destinationViewController as DetailVC;
DetailVC is getting the error, i have no idea what type to declare this, also i have not find any one doing that in this code. I would really like to know what type to declare it.
i am using ios7.1 and 8+ for this app with xcode 6.x latest stable version
First you should post the error message, second be sure that DetailVC is selected in class of your viewController
You will either need to force the cast with:
var detailVC = segue.destinationViewController as! DetailVC
Notice the ! after the as.
Or you can do:
if let detailVC = segue.destinationViewController as? DetailVC {
detailVC.toPass = "help"
}
Notice the ? after the as.
segue.destinationViewController returns a UIViewController. What you are doing with the "as" is downcasting the UIViewController to your subclass of UIViewController. The Swift compiler is not able to determine if a downcast between a class and one of its subclasses will succeed. If you know with absolute certainty the type destinationViewController, you can force the downcast with as!. However, if the downcast fails at runtime, your application will crash. The safer option is to use optional binding and if let with as?. For more information you can read about as on the Apple Swift Blog

Resources