我一般来说都不是新手,但很快,但是我正在研究SQLite教程(https://www.raywenderlich.com/385-sqlite-with-swift-tutorial-getting-started)。我不了解在此处使用扩展程序的理由。我已经创建了一个自定义类来包装SQL连接,如上所述,但是我对扩展的理解是扩展现有类以向该类添加功能。既然我已经编写了自定义类,为什么我不将任何需要扩展的代码放到类本身中呢?
这是扩展代码;
https://www.raywenderlich.com/385-sqlite-with-swift-tutorial-getting-started
extension SQLiteDatabase {
func prepareStatement(sql: String) throws -> OpaquePointer? {
var statement: OpaquePointer? = nil
guard sqlite3_prepare_v2(dbPointer, sql, -1, &statement, nil) == SQLITE_OK else {
throw SQLiteError.Prepare(message: errorMessage)
}
return statement
}
}
我的实现基本相同,但是转储到类本身中。...
class SQLiteDatabase {
<stuff>
func prepareStatement(sql: String) throws -> OpaquePointer? {
var statement: OpaquePointer? = nil
guard sqlite3_prepare_v2(dbPointer, sql, -1, &statement, nil) == SQLITE_OK else {
throw SQLiteError.Prepare(message: errorMessage)
}
return statement
}
} // End Class
任何人都有见识,我显然错过了一些东西,但我不知道它是什么?