exc bad access code 1 in prepare for segue - ios

I ma getting the following error when trying to assign a value to a variable in the destination for a segue
Thread 1: EXC_BAD_ACCESS(Code1, adderss = some-address)
the code that is causing it is as follow's
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if(testNo < 12){
let letterTestController = segue.destinationViewController as! LetterTestHolderViewController
// CRASHES ON THE NEXT LINE
letterTestController.answer = correctAnswers[testNo]
letterTestController.testAnswers = options[testNo]
letterTestController.testNo = testNo
print("SegueIng")
}
}
It was working fine a while age and i have no idea what is causing this error.
Edit
Debugger variable values

Related

Value of type 'HistoryViewController' has no member 'match'

I cannot figure out what the error "Value of type 'HistoryViewController' has no member 'match'" is telling me. Directly above, similar code seems to work:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "throwDownScissors" {
let controller = segue.destination as! ResultViewController
controller.match = self.match
}
else if segue.identifier == "showHistory" {
let controller = segue.destination as! HistoryViewController
controller.match = self.match
}
}
Why does the second bit of code trigger the error?
It seems that your ResultViewController has a match property declared inside it but HistoryViewController doesn't have this property so when the if statment go through the else condition it cannot call the match property which is not existing since you force casted the destination controller to HistoryViewController

why can't recordAudioURL be assigned to the segue.destination object?

I am following an online course and the instructor code has the following code, but I get an error. It might be an issue with updates on Swift or Xcode. How would you approach to fix this error?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "stopRecording" {
let playsoundsVC = segue.destination as! PlaySoundsViewController
let recordedAudioURL = sender as! URL
playsoundsVC.recordedAudioURL = recordedAudioURL
}
}
the last line of the code gives the error:
Cannot assign value of type 'URL' to type 'URL?.Type'
edit1: start of the PlaySoundsViewController file
class PlaySoundsViewController: UIViewController {
var recordedAudioURL = URL?.self
}
Make sure var inside PlaySoundsViewController is declared like this
class PlaySoundsViewController:UIViewController {
var recordedAudioURL:URL?
}
or var recordedAudioURL:URL! if it's 100% it will be assigned

Sending data to another view controller

This is a learning app that I am making for fun, I have been stuck here for 2 days.
I have two views setup that I use to send data that the user will pick to the other one (they are named AddCoinVC and MainVC).
In AddCoinVC, the sending is performed when the user clicks on the button
let vc = MainViewController()
vc.coinArray.append(CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0"))
performSegue(withIdentifier: "backToMain", sender: self)
I have setup a breakpoint at this point and printing vc.coinArray prints me the correct value =
($R0 = 1 value { (coinName = "Test", coinSymbol = "Test", coinAmount = "0")
}
But when I go to my other breakpoint at MainVC, it displays 0 value.
var coinArray = [CoinWallet]()
This is the var that I use, the default in MainVC is CoinWallet which is empty when first loading the app. This is the custom Class.
class CoinWallet {
var coinName:String = ""
var coinSymbol:String = ""
var coinAmount:String = ""
init(coinName: String, coinSymbol:String, coinAmount: String) {
self.coinName = coinName
self.coinSymbol = coinSymbol
self.coinAmount = coinAmount
}
}
When the segue and the sending is performed from AddCoinVC to MainVC 'coinArray' should have this value sent to it.
Why would be the value empty if vc.coinArray has 1 value?
The error occurs because MainViewController() does not return the view controller you expect. It's a new blank instance which is not the instance in the storyboard.
Since you are performing a segue anyway, pass the CoinWallet instance as sender parameter in performSegue
let coin = CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0")
performSegue(withIdentifier: "backToMain", sender: coin)
Then implement prepare(for segue and use the destination property as reference to the main view controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToMain" {
let coin = sender as! CoinWallet
let mainViewController = segue.destination as! MainViewController
mainViewController.coinArray.append(coin)
}
}
You should add this method in your AddCoinVC and send data as below,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let mainVC = segue.destination as? MainViewController {
mainVC.coinArray.append(CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0"))
}
}
In the below lines of code, you are just creating a new instance that is not the viewController being segued.
let vc = MainViewController()
vc.coinArray.append(CoinWallet(coinName: "Test", coinSymbol: "Test", coinAmount: "0"))
You should only perform the segue on button click as below and set any data inside the above method.
performSegue(withIdentifier: "backToMain", sender: self)

transfer weather data to container view

Im unable to update labels in container view. Here's how i've done it.
​
I wrote my updateWeather function in main VC and retrieved the weather data successfully. when i printed weatherJSON it shows all the received data in console.
now when i started writing updateUI function i could only update the labels on main VC.
so i used prepare segue to send data to container view and sent a string to container VC and updated "humidity" label successfully. all labels accept strings without any issues.
but i have no idea how to send weather data to container view.
i tried passing values using object weatherDataModel but nothing happens. i even declared a new object referring to container view class and used it in updateUI function to set label values but it won't work too.
I have no idea what to pass in place of string to get weather data through to next VC.
override func prepare(for segue: UIStoryboardSegue, sender for: Any?) {
if segue.identifier == "displayFullWeatherInfo"{
let destinationVC = segue.destination as! FullWeatherViewController
destinationVC.delegate = "\(weatherDataModel.pressure)"
....
....
Heres my WeatherDataModel Class:
import Foundation
class WeatherDataModel{
var city = ""
var temp = 0
var country = ""
var humidity = 0
}
in my main VC i have created weatherDataModel object and here's my updateWeatherInfo code:
func updateWeatherInfo(json : JSON){
if let tempDefault = json["data"][0]["temp"].double{
weatherDataModel.temp = Int(tempDefault)
weatherDataModel.city = json["data"][0["city_name"].stringValue
weatherDataModel.country = json["data"][0]["country_code"].stringValue
weatherDataModel.humidity = json["data"][0]["rh"].intValue
updateWeatherUI()
}
else{
currentLocation.text = "Not Available"
}
}
​
Create a property for your weather model object in your second VC FullWeatherViewController:
var weatherDataModel: WeatherDataModel! //your object
And in your first VC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "displayFullWeatherInfo" {
let destinationVC = segue.destination as! FullWeatherViewController
destinationVC.weatherDataModel = weatherDataModel
}

Passing object between view controllers in Swift

I am having a strange issue. I am trying to pass an object from ViewControllerA to CreateInvitationViewController.
In ViewControllerA I have following code:
func btnPassedRequestTouched(sender:UIButton!)
{
print("button passed request touched")
// pass request
self.performSegueWithIdentifier("createInvitationSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "createInvitationSegue" {
let createInvitationView = segue.destinationViewController as! CreateInvitationViewController
let invitation = invitations[self.carousel.currentItemIndex]
createInvitationView.invitation = invitation
}
}
I put a breakpoint on this line: createInvitationView.invitation = invitation, and I can see that this object exists.
In ViewControllerB I have the following code:
class CreateInvitationViewController: UIViewController {
var invitation = Invitation()
override func viewDidLoad() {
super.viewDidLoad()
// if invitation is set, then use it to populate fields
if let _ = self.invitation.id {
invitationText.hidden = false
invitationText.text = self.invitation.note
}
}
I am using Show action on the segue that I made and it looks like this:
This is the error that I am getting:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyProject.CreateInvitationViewController 0x156365810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postRequest.'
Do you have some suggestion why this happens?
It's telling you it cannot find postRequest. That means that you either have some code that is trying to set postRequest or, more likely, you have some control in your storyboard that is trying to connect to an outlet called postRequest, but no such outlet exists (e.g. maybe you had some outlet with that name in the past, removed the outlet from the code, but neglected to update the storyboard accordingly).

Resources