My program should fitch the data from an array, and put it in a tableView
var rideTime: String!
var rideLocation: String!
var RideDriver: String!
rideTime = RidestList[indexPath.row].TimeX!
rideLocation = RidestList[indexPath.row].LocationX!
RideDriver = RidestList[indexPath.row].DriverNameX!
cell.DriverX.text = rideTime;
cell.TimeX.text = rideLocation;
cell.LocationX.text = RideDriver;
return cell
In the cell.DriverX.text=rideTime it gives me EXC_BAD_Instraction
And a "atal error: unexpectedly found nil while unwrapping an Optional value
(lldb) " error appears, can anyone help?
if let ride_time = RidestList[indexPath.row].TimeX
{
cell.DriverX.text = ride_time
}
if let ride_location =RidestList[indexPath.row].LocationX
{
cell.TimeX.text = ride_location
}
if let river_driver =RidestList[indexPath.row].DriverNameX
{
cell.LocationX.text = river_driver
}
this could help you with your problem
The crash is basically due to no value coming to your variable rideTime. Best practice is use of optional binding var rideTime: String? to the variables which can expect null values. This will restrict the crash that you faced.
Related
I have following Swift struct:
struct MainStruct : Decodable{
var array : [InternalArray]?
}
struct InternalArray : Decodable{
var firstName : String?
var lastName : String?
var Number : Int?
}
And this is how I'm using it:
var testing: MainStruct? = MainStruct()
testing?.array = []
testing?.array!.append(InternalArray())
testing?.array![0].firstName = "TEST"
testing?.array![1].firstName = "TEST 1" - error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION
It seems to work fine when I'm setting array element [0] but when I try to set element [1] I get an error. Maybe somebody know what is wrong with my implementation ?
The problem is you only have 1 item (index 0).
Let's unpack this, first we unwrap your option MainStruct in the testing variable, and unwrap the optional array:
if let unwrappedTestingMainStruct = testing,
let unwrappedArray = unwrappedTestingMainStruct.array {
unwrappedArray.count // = 1
}
You then try to access testing?.array![1] which would be the second item in the array… which doesn't exist.
If you just had the line:
testing?.array![1]
you would see the same error
I get this Error:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
When I call this function:
ViewController().textToLabel(answer: answerLabel)
I call this from a different class:
func textToLabel(answer: String){
answerLabel.text = answer //I get the error here
}
Second View Controller
import Foundation
import UIKit
class germanClass{
func forGerman(){
var answerLabel = ""
var voiceText = ""
var colorPicker = "white"
var randomNumber = arc4random_uniform(UInt32(numberOfAnswers + 1))
switch (randomNumber){
case 0...20:
answerLabel = "Ja😋"
voiceText = "Ja"
colorPicker = "green"
}
//ViewController().changeColor(color: colorPicker)
ViewController().textToLabel(answer: answerLabel)
if (voiceTextOn){
randomNumber = arc4random_uniform(UInt32(7))
if (randomNumber == 0){
if (userName != "Ich möchte wissen wie du heißt! 😉" || userName != "du"){
voiceText += userName
}
}
}
textToSpeech().siriVoice(language: "de-de", text: voiceText)
}
}
Based on what you've shown, I'd say you are sending a label where you should be sending label.text
Either send the text or send the label and define your method accordingly.
Calling ViewController().textToLabel(answer: answerLabel) will instantiate a new ViewController and call textToLabel(answer:) on it, which is probably not what you want.
If you're trying to pass data between two view controllers you will have to find some other way to do it, depending on the relationship between them.
Also, your second view controller should inherit from UIViewController (as any view controllers should).
After I converted my project to Swift 3.0, I find error in log from variable:
var arrayOfHours = stringArray.map {String(describing: $0!.characters.prefix(2))}
Error is:
code of error: "Optional(Swift.String.CharacterView(_core: Swift._StringCore(_baseAddress: Optional(0x000060800024ee70), _countAndFlags: 2, _owner: Optional(Swift._HeapBufferStorage<Swift._StringBufferIVars,
Swift.UInt16>))))"
where is my error occuring?
Update:
var stringArray in console: [Optional("1226"), Optional("1249"), Optional("1312"), Optional("1336"), Optional("1359"), Optional("1422"), Optional("1446"), Optional("1509"), Optional("1532"), Optional("1549"), Optional("1607"), Optional("1624"), Optional("1642"), Optional("1659"), Optional("1717"), Optional("1734"), Optional("1752"), Optional("1809"), Optional("1827"), Optional("1844"), Optional("1902"), Optional("1919"), Optional("1954"), Optional("2032"), Optional("2107"), Optional("2142"), Optional("2217"), Optional("2252"), Optional("2327"), Optional("2402"), Optional("2437"), Optional("2512")]
var stringArray: [String?] = ["1226", "1249"]
print(stringArray) // [Optional("1226"), Optional("1249")]
var arrayOfHours = stringArray.map { String($0!.characters.prefix(2)) }
print(arrayOfHours) // ["12", "12"]
Well its giving you an Optional of the String casting because String() will return nil if it found something else rather than a String, So what you'll have to do is unwrap that as in :
stringArray.map {String(describing: $0!.characters.prefix(2))!}
Just added an force unwrap ! to the String() result
so I'm having this problem where I can't unwrap an optional for outputting it into the label, I've even tried just printing it in the console and it still gives me an optional
The code and array are in different files.
Code It's in a VC:
for var i = 0; i < stateName.count; i++ {
if tax.state == stateName[i][0] {
stateName[i][1] = Double(taxNumb.text!)!
print(stateName[i][1])
output.text = String(stateName[i][1])
}
}
Array Code I made this in an empty swift file:
var tax : Taxes? = nil
var stateName = [
["AK - Alaska", tax?.alaska!],
["AL - Alabama", tax?.alabama!],
["AR - Arkansas", tax?.arkansas!],
["AZ - Arizona", tax?.arizona!],
["CA - California", tax?.california!]
]
As I wrote in my comment to your previous question use the "Nil Coalescing" ?? operator:
output.text = String(stateName[i][1] ?? "not set")
Or using the alternate swift String magic
output.text = "\(stateName[i][1] ?? "not set")"
The operator returns the first value if it not nil, otherwise it returns the second value.
I have a property in my model of Type : [String:Array<NSDictionary>].
I want to add elements to this, in a loop. So this is what I do :
for var k=0;k<body.count;k++ {
var dict=body[k] as NSDictionary
if(k==0) {
self.model.data[i]!=[dict]
}
else {
self.model.data[i]!.append(dict)
}
}
When I do this, I get the following error :
fatal error: unexpectedly found nil while unwrapping an Optional value
The constructor initializes model.data to data=[String:Array<NSDictionary>]().
Please Help. Thanks in advance.
The problem is probably that you are unwrapping self.model.data[i] when you assign [dict] to it. I assume you are doing that in the case where data[i] has no value yet. So the forced unwrap will result in a nil pointer, which causes a crash.
Try this:
for var k=0;k<body.count;k++ {
var dict=body[k] as NSDictionary
if(k==0) {
self.model.data[i]=[dict]
}
else {
self.model.data[i]!.append(dict)
}
}
Although this assumes that self.model.data[i] exists for the case where k is not equal to 0. I am not sure if that is correct, you don't provide enough details.
What I think you are trying to do is this:
for var k = 0; k < body.count; k++ {
var dict = body[k] as NSDictionary
if self.model.data[i] == nil {
self.model.data[i] = []
}
self.model.data[i]!.append(dict)
}