Strings in Swift 4
Encountering a function that only accepts a
Substring
when you want to pass aString
is less common — most functions should either take aString
or anyStringProtocol
-conforming type. But if you do need to pass aString
, the quickest way is to subscript the string with the range operator...
without specifying any bounds[…][…]
You may be tempted to take full advantage of the existence of
StringProtocol
and convert all your APIs to takeStringProtocol
instances rather than plainString
s. But the advice of the Swift team is not to do that[…][…]
Keep in mind, though, that as of Swift 4,
StringProtocol
is not yet intended as a conformance target for your own custom string types. The documentation explicitly warns against it[…]