ios - Weird fatal error: unexpectedly found nil while unwrapping an Optional value -
i'm using sqlite ios swift app. below error snippet when select row database.
the problem is: has value generate fatal error. (unexpectedly found nil while unwrapping optional value)
the library 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. more clear:
print(studentraw![studentdbinfo.code]) -> crash print(studentraw![studentdbinfo.name]) -> "pham tuan anh"
you force unwrapping studentraw
, , that's crashing because it's nil. need add following before try use it.
guard let studentraw = studentraw else { // handle error return }
as why code
has value, looks reusing variable, possibly has been set previously.
Comments
Post a Comment