Using JSONEncoder to encode a variable with Codable as type

Use a generic type constrained to Encodable

func saveObject<T : Encodable>(_ object: T, at location: String) {
    //Some code

    let data = try JSONEncoder().encode(object)

    //Some more code
}

Leave a Comment