mapbox / MapboxStatic.swift

Static map snapshots with overlays in Swift or Objective-C on iOS, macOS, tvOS, and watchOS
https://www.mapbox.com/api-documentation/?language=Swift#static
Other
189 stars 31 forks source link

Add support for bridging to Objective-C #28

Closed 1ec5 closed 8 years ago

1ec5 commented 8 years ago

All public structs are now classes that inherit from NSObject and have prefixed Objective-C class names so they can bridge to Objective-C. Changed let properties to var properties because mutability of classes is determined on a property-by-property basis. Enums have numeric types so they can bridge too; instead of backing enum values with strings, use switch statements.

Added multiple convenience initializers to Snapshot because optional parameters in the designated initializer aren’t getting elided when bridging to Objective-C. Added multiple convenience initializers to Marker that take non-variant types, so that Marker.Label can be taken private.

Removed colors from overlay initializers because inner type aliases don’t bridge and duplicating initializers would be unmaintainable. Also removed other parameters from Path’s initializer since they make less sense without color parameters. Added members to Path that traffic in raw pointers instead of arrays to enable bridging to Objective-C with C arrays.

GeoJSON’s initializer is now failable instead of throwable, because an error type mismatch between the standard thrown error type and the error parameter of NSJSONSerialization.dataWithJSONObject(_:options:).

Fixed a crash initializing a color with three-digit hexadecimal number. When the hexadecimal string is an NSString under the hood, indexing into an array literal can wind up getting garbage if the NSString is initialized with UTF-16 or other storage under the hood.

Added Objective-C examples to the readme. Added a new example application in Objective-C that has the same minimalist feature set as the Swift example application, plus a little code to exercise round-tripping of coordinates for Path.

Fixes #17.

/cc @friedbunny @tmcw

1ec5 commented 8 years ago

This PR ends up reversing some of the gains in idiomaticity in #23 for the sake of Objective-C support. It’s a worthwhile balance for now, but we’ll have to revisit many of these changes in light of Swift 3 and any corresponding changes to Objective-C.