I'm relatively new to coding in general and to swift, but i'm working through an SQLite tutorial (https://www.raywenderlich.com/385-sqlite-with-swift-tutorial-getting-started). I don't understand the rationale for using an extension here. I've created a custom class to wrap the SQL connection as described, but my understanding of an extension is to extend an existing class to add functionality to the class. Given that i've written the custom class why would i not just put any code that would require an extension into the class itself?
This is the extension code;
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
}
}
My implementation is basically the same but dumped into the class itself....
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
Anyone got an insight, i'm obviously missing something but i've no idea what it is?