0

I am currently writing a iOS App using AWS Mobile Hub and the AWS services. I'm in the middle of implementing API Gateway into my app after connecting it with an AWS Lambda function. After doing all that, AWS generated an SDK for me to implement into my project but the problem is, is that the code is in Swift 2.0. I am trying to convert it into Swift 3.0 but I have no idea what this code is supposed to do/trying to do, so I don't know how to convert it.

My question is, how do I convert this line into Swift 3.0?

var URLString: String = "https://XXXXX.execute-api.XXXX.amazonaws.com/prod" if URLString.hasSuffix("/") { URLString = URLString.substringToIndex(URLString.startIndex.advancedBy(URLString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) - 1)) } 

The problem is the line of code in the "if" statement. Thank you.

3
  • That looks like a (very fragile) attempt to remove the last character from a string, so this might be what you are looking for: stackoverflow.com/questions/39609791/…. Commented Mar 14, 2017 at 18:05
  • Can you read code? This isn't a case of versions - it's a case of what the code is trying to do. And YES, it's trying to remove a trailing "/" character. My real question is why are you asking? Are you experiencing some other issue? Commented Mar 14, 2017 at 18:09
  • I can read code. I am new to Swift though, and no I am not experiencing another issue. I am getting an error "Value of type "String" has no member "substringToIndex"" Commented Mar 14, 2017 at 18:13

1 Answer 1

0

Swift 3 version:

var URLString: String = "https://XXXXX.execute-api.XXXX.amazonaws.com/prod" if URLString.hasSuffix("/") { let index = URLString.index(URLString.startIndex, offsetBy: (URLString.characters.count - 1)) URLString = URLString.substring(to: index) } 

Please use camelcase starting from small letter with naming your variables within Swift. More convenient way:

var urlString: String = "https://XXXXX.execute-api.XXXX.amazonaws.com/prod" if urlString.hasSuffix("/") { urlString = String(urlString.characters.dropLast()) } 
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't write that code. Like I said, it was generated through AWS. But thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.