CLLocationWrapper

1.0

A simple-to-use wrapper for CLLocation for encoding and decoding CLLocation.
visskiss/CLLocation-Codable

What's New

v1.0

2023-01-28T03:27:37Z

What's Changed

New Contributors

Full Changelog: https://github.com/visskiss/CLLocation-Codable/commits/v1.0

Swift 5.7 [Platforms](https://www.apple.com/ios/ios-15/) Swift Package Index Swift Package Manager License

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:

import CoreLocation

let location = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 16.6, longitude: 12.5),
                          altitude: 2.6,
                          horizontalAccuracy: 1.8,
                          verticalAccuracy: 9,
                          course: 46.891,
                          speed: 1.5,
                          timestamp: Date())

you may now serialize that directly, ie

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.

dependencies: [
    .package(url: "https://github.com/visskiss/CLLocation-Codable")
]

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

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Oct 22 2024 09:31:08 GMT-0900 (Hawaii-Aleutian Daylight Time)