File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -44,11 +44,22 @@ public extension Bundle {
4444/// Extensions on `String` that work with `Path` rather than `String` or `URL`
4545public extension String {
4646 /// Initializes this `String` with the contents of the provided path.
47+ /// - SeeAlso: `init(contentsOf:encoding:)`
48+ @available ( macOS, deprecated: 15 , message: " Use `init(contentsOf:encoding:)` instead " )
49+ @available ( iOS, deprecated: 18 , message: " Use `init(contentsOf:encoding:)` instead " )
50+ @available ( tvOS, deprecated: 18 , message: " Use `init(contentsOf:encoding:)` instead " )
51+ @available ( watchOS, deprecated: 11 , message: " Use `init(contentsOf:encoding:)` instead " )
4752 @inlinable
4853 init < P: Pathish > ( contentsOf path: P ) throws {
4954 try self . init ( contentsOfFile: path. string)
5055 }
5156
57+ /// Initializes this `String` with the contents of the provided path interpreted using a given encoding.
58+ @inlinable
59+ init < P: Pathish > ( contentsOf path: P , encoding: String . Encoding ) throws {
60+ try self . init ( contentsOfFile: path. string, encoding: encoding)
61+ }
62+
5263 /// - Returns: `to` to allow chaining
5364 @inlinable
5465 @discardableResult
Original file line number Diff line number Diff line change @@ -496,6 +496,13 @@ class PathTests: XCTestCase {
496496 }
497497 }
498498
499+ func testStringEncodingExtensions( ) throws {
500+ let string = try String ( contentsOf: Path ( #file) !, encoding: . utf8)
501+ try Path . mktemp { tmpdir in
502+ _ = try string. write ( to: tmpdir. foo, encoding: . utf8)
503+ }
504+ }
505+
499506 func testFileHandleExtensions( ) throws {
500507 _ = try FileHandle ( forReadingAt: Path ( #file) !)
501508 _ = try FileHandle ( forWritingAt: Path ( #file) !)
You can’t perform that action at this time.
0 commit comments