第20章:错误处理(119)

本文介绍了一个基于Swift语言的错误处理示例,通过定义错误类型、实现数据操作方法并结合具体测试案例来展示如何进行错误抛出及捕获。示例中使用了枚举来定义可能发生的错误,并在数据访问层实现了创建、读取、更新和删除等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源代码:

//程序目的:学习《从零开始学swift》
//第20章:错误处理
//Create By ChenZhen

enum DAOError: Error {
    case Nodata
    case PrimaryKeyNull
}

class Note {
    var date: NSDate?
    var content: String?
    
    init(date: NSDate?, content: String?) {
        self.date = date
        self.content = content
    }
}

class NoteDAO {
    //保存数据列表
    private var listData = [Note]()
    
    //插入Note方法
    func create(model: Note) {
        listData.append(model)
    }
    
    //删除Note方法
    func remove(model: Note) throws {
        guard let date = model.date else {
            //抛出“主键为空”错误
            throw DAOError.PrimaryKeyNull
        }
        //比较日期主键是否相等
        for (index, note) in listData.enumerated() where note.date == date {
            listData.remove(at: index)
        }
    }
    
    //修改Note方法
    func modify(model: Note) throws {
        guard let date = model.date else {
            //抛出主键为空的错误
            throw DAOError.PrimaryKeyNull
        }
        //比较日期主键是否相等
        for note in listData where note.date == date {
            note.content = model.content
        }
    }
    
    //查询所有数据的方法
    func findAll() throws -> [Note] {
        guard listData.count  > 0 else {
            //抛出“没有数据”错误
            throw DAOError.Nodata
        }
        defer {
            print("关闭数据库")
        }
        defer {
            print("释放语句对象")
        }
        return listData
    }
    
    //修改Note方法
    func findBy(model: Note) throws -> Note? {
        guard let date = model.date else {
            //抛出主键为空的错误
            throw DAOError.PrimaryKeyNull
        }
        //比较日期主键是否相等
        for note in listData where note.date == date {
            return note
        }
        return nil
    }
}


//测试示例
//日期格式对象
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let dao = NoteDAO()

//查询所有数据
do {
    try dao.findAll()
} catch DAOError.Nodata {
    print("没有数据")
    
    //准备要插入的数据
    var date1: NSDate = dateFormatter.date(from: "2016-01-01 16:01:03")! as NSDate
    var note1: Note = Note(date: date1, content: "welcome to mynote.")
    var date2: NSDate = dateFormatter.date(from: "2016-01-02 8:01:03")! as NSDate
    var note2: Note = Note(date: date2, content: "欢迎使用mynote。")
    //插入数据
    dao.create(model: note1)
    dao.create(model: note2)
}

do {
    var note: Note = Note(date: nil, content: "welcome to mynote")
    try dao.remove(model: note)
} catch DAOError.PrimaryKeyNull {
    print("主键为空。")
}

func printNotes() throws {
    let datas = try dao.findAll()
    for note in datas {
        print("date: \(note.date!) - content: \(note.content!)")
    }
}

try printNotes()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值