I'm using sqlite for my iOS Swift app. Below is an error snippet when I select a row from database.
The problem is: It has a value but it generate a fatal error. (unexpectedly found nil while unwrapping an Optional value)
The library I used is: stephencelis/SQLite.swift
import Foundation
import SQLite
struct studentDB {
let table = Table("students")
let code = Expression<String>("code")
let name = Expression<String>("name")
let gender = Expression<Int>("gender")
let birth = Expression<String>("birth")
let born = Expression<String>("born")
let className = Expression<String>("class")
let speciality = Expression<String>("speciality")
let department = Expression<String>("department")
let faculty = Expression<String>("faculty")
let training = Expression<String>("training")
let course = Expression<String>("course")
let advisor = Expression<String>("advisor")
}
struct student {
let code: String!
let name: String?
let gender: Int?
let birth: String?
let born: String?
let className: String?
let speciality: String?
let department: String?
let faculty: String?
let training: String?
let course: String?
let advisor: String?
}
class Student {
class func getStudent(code: String) -> student? {
let studentDBInfo = studentDB()
let studentRaw = try! userDatabase.pluck(studentDBInfo.table.filter(studentDBInfo.code == code))
if (studentRaw != nil) {
let studentInfo = student(
code: studentRaw![studentDBInfo.code],
name: studentRaw![studentDBInfo.name],
gender: studentRaw![studentDBInfo.gender],
birth: studentRaw![studentDBInfo.birth],
born: studentRaw![studentDBInfo.born],
className: studentRaw![studentDBInfo.className],
speciality: studentRaw![studentDBInfo.speciality],
department: studentRaw![studentDBInfo.department],
faculty: studentRaw![studentDBInfo.faculty],
training: studentRaw![studentDBInfo.training],
course: studentRaw![studentDBInfo.course],
advisor: studentRaw![studentDBInfo.advisor]
)
return studentInfo
} else {
return nil
}
}
}
All other column work fine. To be more clear:
print(studentRaw![studentDBInfo.code]) -> crash
print(studentRaw![studentDBInfo.name]) -> "Pham Tuan Anh"