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
}
More Related Contents:
- How to use Any in Codable Type
- Xcode 10.2 with Swift 5.0 compiler – protocol inheritance issue
- How do I add different types conforming to a protocol with an associated type to a collection?
- Swift Protocol get only settable?
- Swift String escaping when serializing to JSON using Codable
- How to get boolean value from switch state?
- How to hide UINavigationBar 1px bottom line
- Can’t use Swift classes inside Objective-C
- Move view with keyboard using Swift
- How to have stored properties in Swift, the same way I had on Objective-C?
- How to insert new cell into UITableView in Swift
- How to get a user’s time zone?
- How to tell SwiftUI views to bind to nested ObservableObjects
- When to use @objc in Swift?
- How to play a sound using Swift?
- Writing handler for UIAlertAction
- ios 8 Swift – TableView with embedded CollectionView
- Open UITableView edit action buttons programmatically
- SwiftUI TextField max length
- iOS 14 How to trigger Local Network dialog and check user answer?
- How to push and present to UIViewController programmatically without segue in iOS Swift 3
- Firebase asynchronous function, what’s in the background queue and what’s not
- How to split filename from file extension in Swift?
- Loading a ViewController inside a Container View
- AppDelegate and SceneDelegate when supporting iOS 12 and 13
- How to remove border of the navigationBar in swift?
- Swift 3 JSON NSFastEnumerationIterator has no subscript members
- How to use NSUnderlineStyle.PatternDot
- arc4random_uniform not available in Xcode 7.0 beta (7a176x) on OSX 10.10.4
- Delaying function in swift [duplicate]