1

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?

Mark
  • 35
  • 4
  • 1
    As long as you have access to the source code of the original class and you can modify it (i.e. the extended type isn't declared in a 3rd party framework/Foundation/UIKit,etc), there's no functional difference in whether you declare protocol conformance/add new properties/methods directly in the type declaration/in an extension. However, using an extension can help make your code more readable by grouping relevant methods in the same extension. – Dávid Pásztor Dec 11 '18 at 12:13
  • @DávidPásztor isn't that the whole point of the class setup though? So in this tutorial the writer is using both extensions and protocol definitions when actually the class itself is part of the tutorial which is i guess where the root of my confusion comes from. Why would you do that?? – Mark Dec 11 '18 at 12:17
  • See also: https://www.natashatherobot.com/using-swift-extensions/. – Martin R Dec 11 '18 at 12:19
  • @MartinR so what i take from those articles is that it's purely a preference in terms of how to code. Could you not just do the same thing in a single class and put some kind of comment line between public and private for example? Are there any speed / compilation considerations here? Further could it not be more complex if you have say 5 different extension blocks for a class? – Mark Dec 11 '18 at 12:32
  • @Mark: As explained in those articles, it is a way to organize your code. I am not aware of compilation or speed problems with that approach. – Martin R Dec 11 '18 at 12:56

0 Answers0