A simple-to-use wrapper for CLLocation for encoding and decoding CLLocation and CLLocationCoordinate2D.
Motivation
A lot of iOS applications work with the Core Location framework. The main object of Core Location is CLLocation, which represents a location. For some odd reason, Core Location doesn’t conform to Codable protocol. Therefore, we can’t serialize it by default, save it to a database, or send it through a network API.
This project solves that problem.
Solution
In this code, I created a wrapper for CLLocation which you can use to serialize and deserialize CLLocation.
Enjoy!
P.S. Check the tests to see how it works and how to use CLLocationWrapper.
class MyClass:Encodable {
var location:CLLocation
}
and voilà! it’s serialized!
Now you can send it to a server, save it in User Defaults, or write it to a database:
let jsonEncoder = JSONEncoder()
do {
let encodedLocation = try jsonEncoder.encode(location)
// Do whatever you want with the location
} catch {
print("Error! Location encode failed: '\(error)'")
}
And how to decode it?
Very simple, but don’t forget to unwrap:
let jsonDecoder = JSONDecoder()
do {
let decodedLocationWrapper = try jsonDecoder.decode(CLLocationWrapper.self, from: encodedLocation)
let location = decodedLocationWrapper.location
// Do whatever you want with the wrapped location
} catch {
print("Error! Location wrapper decode failed: '\(error)'")
}
Installation
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding CLLocationWrapper as a dependency is as easy as adding it to the dependencies value of your Package.swift.
If you prefer not to use any of the mentioned dependency managers, you can integrate CLLocationWrapper into your project manually.
Contribution
You can contribute by adding new features, clarify the current code or fixing bugs. To do so, open a PR. When submitting a pull request, please add tests and ensure your changes don’t break any existing tests.
CLLocation+Codable
Vision
A simple-to-use wrapper for CLLocation for encoding and decoding CLLocation and CLLocationCoordinate2D.
Motivation
A lot of iOS applications work with the Core Location framework. The main object of Core Location is CLLocation, which represents a location. For some odd reason, Core Location doesn’t conform to Codable protocol. Therefore, we can’t serialize it by default, save it to a database, or send it through a network API. This project solves that problem.
Solution
In this code, I created a wrapper for CLLocation which you can use to serialize and deserialize CLLocation. Enjoy!
P.S. Check the tests to see how it works and how to use CLLocationWrapper.
Demo
Let’s say you have a location:
you may now serialize that directly, ie
and voilà! it’s serialized!
Now you can send it to a server, save it in User Defaults, or write it to a database:
And how to decode it?
Very simple, but don’t forget to unwrap:
Installation
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the
swift
compiler.Once you have your Swift package set up, adding CLLocationWrapper as a dependency is as easy as adding it to the
dependencies
value of yourPackage.swift
.Manually
If you prefer not to use any of the mentioned dependency managers, you can integrate CLLocationWrapper into your project manually.
Contribution
You can contribute by adding new features, clarify the current code or fixing bugs. To do so, open a PR. When submitting a pull request, please add tests and ensure your changes don’t break any existing tests.
Credits
This repository was Inspired by this package