-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Removing deprecated string.characters calls and xcode 9.1 recommend settings. #305
Conversation
Removing deprecated string.characters calls.
Generated by 🚫 Danger |
@@ -92,7 +92,7 @@ public extension Color { | |||
/// SwifterSwift: Short hexadecimal value string (read-only, if applicable). | |||
public var shortHexString: String? { | |||
let string = hexString.replacingOccurrences(of: "#", with: "") | |||
let chrs = Array(string.characters) | |||
let chrs = Array(string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this fail now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think not, it returns an array of characters the same way as before :)
@@ -39,7 +39,7 @@ public extension String { | |||
/// SwifterSwift: Array of characters of a string. | |||
/// | |||
public var charactersArray: [Character] { | |||
return Array(characters) | |||
return Array(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we get an array of characters here or an array of a Single string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess I have to read about the new Strings 🤔 Implicit cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns an array of characters :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do this? Forgive me I haven't had to deal with this yet 😃 But we may want to re-evaluate all the string extensions in general and see if some are obsolete
var chars: [Characters] = "Swift"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucianoPAlmeida Appreciate you testing that! It's very hard for me to run a playground on my slow iMac 😭
Codecov Report
@@ Coverage Diff @@
## master #305 +/- ##
==========================================
- Coverage 94.66% 94.43% -0.23%
==========================================
Files 103 104 +1
Lines 5994 5988 -6
==========================================
- Hits 5674 5655 -19
- Misses 320 333 +13
Continue to review full report at Codecov.
|
@@ -229,7 +229,7 @@ public extension String { | |||
/// "Hello world!".length -> 12 | |||
/// | |||
public var length: Int { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably deprecate this one. It's not even a good name because it can easily be confused with NSString.length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯, also String.count
can be used in Swift 4
@@ -401,7 +401,8 @@ public extension String { | |||
/// | |||
/// - Returns: The reversed string. | |||
public func reversed() -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this one too. We can just use String(str.reversed())
if we even need String
typing
@@ -575,7 +576,7 @@ public extension String { | |||
/// - Parameter string: substring to search for. | |||
/// - Returns: first index of substring in string (if applicable). | |||
public func firstIndex(of string: String) -> Int? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably obsolete now that String
is a Collection
?
We can use func index(of element: Self.Element) -> Self.Index?
I think?
@@ -620,10 +621,10 @@ public extension String { | |||
/// - length: amount of characters to be sliced after given index. | |||
/// - Returns: sliced substring of length number of characters (if applicable) (example: "Hello World".slicing(from: 6, length: 5) -> "World") | |||
public func slicing(from i: Int, length: Int) -> String? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we can subscript because it's a collection so this can probably go too 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know about this one. I think handles some safe convenience cases that maybe aren't done with the subscript. @SD10 @omaralbeik What you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should turn it into a safe subscript that accepts a Range<String.Index>
and returns a Substring?
then. The name of this method doesn't make sense, I'd expect to get an ArraySlice
out of it. Since it has ing
it also makes me think it's mutating and it's not 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it can be turned in a subscript with range because the second parameter is a length, not an end index 😅 I think worth just rename and returning a substring instead. @SD10 @omaralbeik What you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point @LucianoPAlmeida 🤔 But I'm having trouble thinking of a place where someone would be using a length and not an endIndex
considering they have a startIndex
. You can always get the endIndex
by incrementing the startIndex
by n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. So you think will be better deprecating this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can always replace it with the subscript
version in another PR. Sorry to make so many changes. I should've thought about this before we even released SwifterSwift 4.0. It completely slipped my mind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the mutating version? Should be maintained?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucianoPAlmeida lets not make this change right now... I just opened the file and there are a ton of these slice
methods. We also already have subscripts
from ranges but they return String?
and not Substring?
It's too much for me to look at right now...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SD10 No problem we can open a issue/PR later with only this methods to discuss :)
@@ -684,10 +685,10 @@ public extension String { | |||
/// - Parameter i: string index the slicing should start from. | |||
/// - Returns: sliced substring starting from start index (if applicable) (example: "Hello world".slicing(at: 6) -> "world") | |||
public func slicing(at i: Int) -> String? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
||
@@ -710,7 +711,7 @@ public extension String { | |||
/// - Parameter separator: separator to split string by. | |||
/// - Returns: array of strings separated by given string. | |||
public func splitted(by separator: Character) -> [String] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this one but it looks like split(separator: Element)
is the way to go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucianoPAlmeida Let me know what you think of the methods I've commented. I think a lot of them have become obsolete because they were simply added as convenience to not have to reference the underlying characters Array
. We get a lot of this out of the box form Collection
now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @LucianoPAlmeida
I think we need to check all String extensions as @SD10 mentioned
@@ -86,7 +86,7 @@ public extension String { | |||
/// "".firstCharacterAsString -> nil | |||
/// | |||
public var firstCharacterAsString: String? { | |||
guard let first = characters.first else { | |||
guard let first = self.first else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to rename first
to firstCharacter
to avoid variable shadowing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can't rename first because is a native String property now that its conformance with Collection was restored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucianoPAlmeida I meant guard let firstCharacter = self.first else { ...
@@ -210,7 +210,7 @@ public extension String { | |||
/// "".lastCharacterAsString -> nil | |||
/// | |||
public var lastCharacterAsString: String? { | |||
guard let last = characters.last else { | |||
guard let last = self.last else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to rename last
to lastCharacter
to avoid variable shadowing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing like first above :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably just remove these as well 😅 "Some string".last
and casting when we need it should be prefered
@@ -386,7 +386,7 @@ public extension String { | |||
/// | |||
/// - Returns: The most common character. | |||
public func mostCommonCharacter() -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how come mostCommonCharacter
is returning String instead of Character here 🤔
@@ -442,7 +443,7 @@ public extension String { | |||
/// | |||
/// - Parameter i: index. | |||
public subscript(safe i: Int) -> String? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this return Character?
as well?
@omaralbeik @SD10 Working on those that should be deprecated :) How deprecation should be done? Just annotate the method? Annotate and move to another file? In case of renaming, should I remove the previous method? |
@LucianoPAlmeida Here is an example from basically: |
…nd clean up the tests. Adding changeling entries for the deprecations.
@SD10 @LucianoPAlmeida Please let me know if we can merge this soon, as other PRs are depending on it. We'll probably look again on String extensions later |
Hey @omaralbeik That's it for this one, I was just waiting for both of you guys review it :) |
CHANGELOG.md
Outdated
- `slicing(from: to:)` is deprecated, use `string[safe: start..<end]` | ||
- `firstIndex(of:)` is deprecated, use the natives `index(of: Character)` or `range(of: StringProtocol)` instead. | ||
- `splitted(by:)` is deprecated, use the native `split(separator: )` instead. | ||
- `reversed() -> String` is deprecated, use the Swift 4 new `reversed() -> ReversedCollection<String>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LucianoPAlmeida Please add the PR number as a reference under this part of the CHANGELOG
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with this 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you guys!
Xcode 9.1 project recommended settings
Removing deprecated string.characters calls.
Checklist