Skip to content

Upgrade to swift 4 #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 22, 2017
Merged

Upgrade to swift 4 #663

merged 6 commits into from
Nov 22, 2017

Conversation

bsudekum
Copy link
Contributor

This begins the process of upgrading to swift 4.

/cc @1ec5 @ericrwolfe @frederoni

Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to maintain Swift 3 support by changing SWIFT_VERSION back to 3.1 and enclosing any incompatible code in #if swift checks?

@bsudekum
Copy link
Contributor Author

enclosing any incompatible code in #if swift checks?

Let's see how many there are. This could become very tiresome if it's a lot of code. The other option is to maintain a branch for x more months... and with a set EOL.

@@ -190,7 +190,7 @@ class GeometryTests: XCTestCase {

// https://github.com/Turfjs/turf/blob/142e137ce0c758e2825a260ab32b24db0aa19439/packages/turf-distance/test.js
let a = distance(along: line)
XCTAssertEqualWithAccuracy(a, 97_159.57803131901, accuracy: 1)
XCTAssertEqual(a, 97_159.57803131901, accuracy: 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frederoni
Copy link
Contributor

Should we add an additional Bitrise app with the new Xcode 9 stack?

@bsudekum
Copy link
Contributor Author

Yes, we should test both.

@bsudekum bsudekum force-pushed the swift4 branch 2 times, most recently from b4f150f to e35203a Compare September 27, 2017 22:21
@bsudekum
Copy link
Contributor Author

Added bitrise test for xcode 9. It will only run on this branch.

@bsudekum
Copy link
Contributor Author

bsudekum commented Sep 28, 2017

So I added quite a few @objc declarations but there are still a handful of public methods that are not bridging.

@bsudekum bsudekum changed the title Upgrade to swift 4 Upgrade to swift 4, allow swift 3 Sep 28, 2017
@bsudekum
Copy link
Contributor Author

A line line https://github.com/mapbox/mapbox-navigation-ios/pull/663/files#diff-851f438872328e98cbe395ac0733c16bR130 will not accept the prefix @objc because it doesn't bridge. However, I guess it never bridged to begin with?

@1ec5
Copy link
Contributor

1ec5 commented Sep 28, 2017

Not sure which line you’re referring to, but there are probably a number of methods that never bridged to Objective-C. Let’s track those issues separately. Making these situations explicit is one of the benefits of Swift 4’s @objc behavior.

@@ -17,13 +17,13 @@ open class NavigationLocationManager: CLLocationManager {
/**
Indicates whether the device is plugged in or not.
*/
public private(set) var isPluggedIn: Bool = false
@objc public private(set) var isPluggedIn: Bool = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private(set)

I did not know you could do that.

@bsudekum
Copy link
Contributor Author

We'll need to update MapboxDirections.swift to swift 4 before merging this.

@bsudekum
Copy link
Contributor Author

This is blocked by CocoaPods/CocoaPods#7134

@1ec5
Copy link
Contributor

1ec5 commented Oct 24, 2017

Is it really blocked? This PR was designed to work when compiled as Swift 3, so we could have Carthage compiling it as Swift 4 and CocoaPods compiling it as Swift 3 for the time being.

@bsudekum
Copy link
Contributor Author

@1ec5 I guess that would work.

Cartfile Outdated
@@ -2,7 +2,7 @@ binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" ~> 3.6
github "mapbox/MapboxDirections.swift" ~> 0.12
github "mapbox/turf-swift" ~> 0.0.3
github "rs/SDWebImage" ~> 4.1
github "Project-OSRM/osrm-text-instructions.swift" ~> 0.5
github "Project-OSRM/osrm-text-instructions.swift" "f60d5bc175794aee6c7199d60548820e0fb23097"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change when Project-OSRM/osrm-text-instructions.swift#50 is released.

@bsudekum
Copy link
Contributor Author

@mapbox/navigation-ios this is ready for review.

Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is functionally sound, but there are a number of places where running the migrator resulted in unnecessarily verbose code.

@@ -126,7 +126,6 @@ -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

MGLPointAnnotation *destination = [[MGLPointAnnotation alloc] init];
destination.coordinate = self.destination;
controller.destination = destination;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this line removed? Doesn’t this cause the preceding two lines to be useless?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destination was deprecated eons ago.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So shouldn’t the two lines above be removed too?

@@ -21,7 +21,7 @@ class MapboxCoreNavigationTests: XCTestCase {
navigation.resume()
let depart = CLLocation(coordinate: CLLocationCoordinate2D(latitude: 37.795042, longitude: -122.413165), altitude: 1, horizontalAccuracy: 1, verticalAccuracy: 1, course: 0, speed: 10, timestamp: Date())

self.expectation(forNotification: RouteControllerDidPassSpokenInstructionPoint.rawValue, object: navigation) { (notification) -> Bool in
self.expectation(forNotification: NSNotification.Name(rawValue: RouteControllerDidPassSpokenInstructionPoint.rawValue), object: navigation) { (notification) -> Bool in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RouteControllerDidPassSpokenInstructionPoint directly instead of converting to a raw value and back to an NSNotification.Name (×4).

@@ -6,13 +6,13 @@ extension BottomBannerView {

let timeRemainingLabel = TimeRemainingLabel()
timeRemainingLabel.translatesAutoresizingMaskIntoConstraints = false
timeRemainingLabel.font = .systemFont(ofSize: 28, weight: UIFontWeightMedium)
timeRemainingLabel.font = .systemFont(ofSize: 28, weight: UIFont.Weight.medium)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop UIFont.Weight (×2).

@@ -58,22 +58,22 @@ open class DayStyle: Style {
tintColor = .defaultTint
}

ArrivalTimeLabel.appearance().font = UIFont.systemFont(ofSize: 18, weight: UIFontWeightMedium).adjustedFont
ArrivalTimeLabel.appearance().font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium).adjustedFont
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop UIFont.Weight (×10).

var attributes: [String: Any] {
return [NSFontAttributeName: font, NSForegroundColorAttributeName: textColor]
var attributes: [NSAttributedStringKey: Any] {
return [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: textColor]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop NSAttributedStringKey.

let valueAttributes: [String: Any] = [NSForegroundColorAttributeName: valueTextColor, NSFontAttributeName: valueFont]
let unitAttributes: [String: Any] = [NSForegroundColorAttributeName: unitTextColor, NSFontAttributeName: unitFont]
let valueAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.foregroundColor: valueTextColor, NSAttributedStringKey.font: valueFont]
let unitAttributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.foregroundColor: unitTextColor, NSAttributedStringKey.font: unitFont]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop NSAttributedStringKey from both these lines.

@@ -37,14 +37,14 @@ extension UIFont {

func with(fontFamily: String?) -> UIFont {
guard let fontFamily = fontFamily else { return self }
let weight = (fontDescriptor.object(forKey: UIFontDescriptorTraitsAttribute) as! [String:Any])[UIFontWeightTrait]
let attributes = [UIFontDescriptorTraitsAttribute: [UIFontWeightTrait: weight]]
let weight = (fontDescriptor.object(forKey: UIFontDescriptor.AttributeName.traits) as! [String:Any])[UIFontDescriptor.TraitKey.weight.rawValue]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop UIFontDescriptor.AttributeName.

let weight = (fontDescriptor.object(forKey: UIFontDescriptorTraitsAttribute) as! [String:Any])[UIFontWeightTrait]
let attributes = [UIFontDescriptorTraitsAttribute: [UIFontWeightTrait: weight]]
let weight = (fontDescriptor.object(forKey: UIFontDescriptor.AttributeName.traits) as! [String:Any])[UIFontDescriptor.TraitKey.weight.rawValue]
let attributes = [UIFontDescriptor.AttributeName.traits: [UIFontDescriptor.TraitKey.weight: weight]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline this variable so that you can drop UIFontDescriptor.AttributeName and possibly UIFontDescriptor.TraitKey.

let descriptor = UIFontDescriptor(name: fontName, size: pointSize).withFamily(fontFamily).addingAttributes(attributes)
return UIFont(descriptor: descriptor, size: pointSize)
}

func with(weight: CGFloat) -> UIFont {
let font = UIFont(descriptor: fontDescriptor.addingAttributes([UIFontWeightTrait: weight]), size: pointSize)
let font = UIFont(descriptor: fontDescriptor.addingAttributes([UIFontDescriptor.AttributeName.traits: weight]), size: pointSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop UIFontDescriptor.AttributeName.

view.primaryLabel.font = UIFont.systemFont(ofSize: 30, weight: UIFontWeightMedium)
view.secondaryLabel.font = UIFont.systemFont(ofSize: 26, weight: UIFontWeightMedium)
view.primaryLabel.font = UIFont.systemFont(ofSize: 30, weight: UIFont.Weight.medium)
view.secondaryLabel.font = UIFont.systemFont(ofSize: 26, weight: UIFont.Weight.medium)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: drop UIFont.Weight from both these lines.

@1ec5 1ec5 changed the title Upgrade to swift 4, allow swift 3 Upgrade to swift 4 Nov 21, 2017
@bsudekum
Copy link
Contributor Author

@1ec5 fixed.

@@ -43,7 +43,7 @@ class MapboxCoreNavigationTests: XCTestCase {
let locationManager = ReplayLocationManager(locations: [location, location])
let navigation = RouteController(along: route, directions: directions, locationManager: locationManager)

self.expectation(forNotification: NSNotification.Name(rawValue: RouteControllerDidPassSpokenInstructionPoint.rawValue), object: navigation) { (notification) -> Bool in
self.expectation(forNotification: RouteControllerDidPassSpokenInstructionPoint), object: navigation) { (notification) -> Bool in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unbalanced parentheses are causing a build failure (×3).

@1ec5
Copy link
Contributor

1ec5 commented Nov 22, 2017

Fixed a serving or two of copypasta in fcb0e12c0434a574d77a655c25723004c7b3f27b and ac5f85e2e8da6c710a0f902cedecacd4f83a6eb6.

@1ec5 1ec5 added build Issues related to builds and dependency management. and removed ✓ ready for review labels Nov 22, 2017
@1ec5 1ec5 force-pushed the swift4 branch 2 times, most recently from b176bbf to e7e45da Compare November 22, 2017 21:45
@1ec5 1ec5 merged commit f8081f8 into master Nov 22, 2017
@1ec5 1ec5 deleted the swift4 branch November 22, 2017 23:33
@1ec5 1ec5 added the backwards incompatible changes that break backwards compatibility of public API label Dec 18, 2018
@1ec5 1ec5 added this to the v0.11.0 milestone Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards incompatible changes that break backwards compatibility of public API build Issues related to builds and dependency management.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants