Skip to content
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

Fixed substring API warnings for Swift 3.2 and up #2240

Merged
merged 2 commits into from
Aug 22, 2017

Conversation

htinlinn
Copy link
Contributor

As of the latest version of Xcode 9 beta, substring functions from String have been deprecated and show up as warnings when compiling using Swift 4 compiler.

This pull request fixes those warnings by replacing calls to those functions with the new subscript slicing syntax.

@ghost
Copy link

ghost commented Aug 15, 2017

thanks

Copy link
Contributor

@jshier jshier left a comment

Choose a reason for hiding this comment

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

One logic change and some whitespace changes, otherwise this looks good. Thanks!

components.append("-b \"\(string.substring(to: string.characters.index(before: string.endIndex)))\"")

#if swift(>=3.2)
components.append("-b \"\(string[..<string.index(before: string.endIndex)])\"")
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe you need the before: call anymore, as ..< does essentially the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we will still want the before: call. substring(to:) gets you every character except for the one specified so in this case it seems to be stripping out the last character in the string.

Copy link
Contributor

Choose a reason for hiding this comment

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

index(before: endIndex gives you endIndex - 1. ..<endIndex also gives you endIndex - 1. ..<index(before: endIndex) would give you endIndex - 2. So to be equivalent to what it was before it would be ..<endIndex, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The original version with substring(to:) also gives you endIndex - 2: -1 from index(before:) and another -1 from substring(to:).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, thanks.

@@ -308,7 +308,12 @@ extension Request: CustomDebugStringConvertible {
let cookies = cookieStorage.cookies(for: url), !cookies.isEmpty
{
let string = cookies.reduce("") { $0 + "\($1.name)=\($1.value);" }
components.append("-b \"\(string.substring(to: string.characters.index(before: string.endIndex)))\"")

Copy link
Contributor

Choose a reason for hiding this comment

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

No need for extra whitespace around the #if checks. We prefer to keep the whitespace the same as if there was no check in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

components.append("-b \"\(string.substring(to: string.characters.index(before: string.endIndex)))\"")

#if swift(>=3.2)
components.append("-b \"\(string[..<string.index(before: string.endIndex)])\"")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please unindent the code inside the #if check to match out style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@@ -308,7 +308,12 @@ extension Request: CustomDebugStringConvertible {
let cookies = cookieStorage.cookies(for: url), !cookies.isEmpty
{
let string = cookies.reduce("") { $0 + "\($1.name)=\($1.value);" }

#if swift(>=3.2)
Copy link
Contributor

Choose a reason for hiding this comment

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

#if statements and the code should be at the same level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@htinlinn htinlinn force-pushed the xcode-9-string-warnings branch from a92d920 to ceed9c4 Compare August 21, 2017 17:41
@@ -308,7 +308,12 @@ extension Request: CustomDebugStringConvertible {
let cookies = cookieStorage.cookies(for: url), !cookies.isEmpty
{
let string = cookies.reduce("") { $0 + "\($1.name)=\($1.value);" }

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, like I said before, please remove the newlines before and after all version checks. Basically, where there are version checks, the whitespace should match what was there before. Same indentation level, same newlines.

@cnoon
Copy link
Member

cnoon commented Aug 22, 2017

Hi @htinlinn,

The compiler warnings you mention here do not appear for me in Xcode 9 beta 5. I'm currently installing beta 6 and will give it a shot there, but I don't anticipate we'll see the warnings there either. Were you seeing these issues in beta 4?

I'm going to close issue out for now since I'm not seeing the warnings. We'll certainly take a pass at updating all the APIs and implementations to the latest Swift 4 syntax in the AF 5 branch.

If I'm completely mistaken here and the warnings are still present in beta 5 and for some reason I'm not seeing them, then please comment back on this ticket and we'll happily re-open.

Thanks anyways! 🍻

@cnoon cnoon closed this Aug 22, 2017
@cnoon cnoon added the swift label Aug 22, 2017
@jshier
Copy link
Contributor

jshier commented Aug 22, 2017

@cnoon They only appear in Swift 4 mode.

@htinlinn One last change and this should be good to go.

@jshier jshier reopened this Aug 22, 2017
@htinlinn htinlinn force-pushed the xcode-9-string-warnings branch from ceed9c4 to d59687e Compare August 22, 2017 14:42
@htinlinn
Copy link
Contributor Author

@jshier Done. Sorry about all the back and forth with this. I wasn't thinking about newlines when you said whitespace.

@cnoon
Copy link
Member

cnoon commented Aug 22, 2017

Ah thanks @jshier...late night. I figured I was missing something. 😕

@jshier
Copy link
Contributor

jshier commented Aug 22, 2017

Merging without full Travis, because it would likely be hours before Travis got to us. All tests pass locally for me.

@jshier jshier merged commit e396a0e into Alamofire:master Aug 22, 2017
@cnoon cnoon added this to the 4.5.1 milestone Sep 6, 2017
@PGRBryant
Copy link

Looks like we've still got three substring deprecated and one 'characters' deprecated warnings hanging around. Unless the version I'm running from cocoapads isn't up to date?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants