Skip to content

change FileManager.default() class function to static let #484

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

Merged
merged 3 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ extension NSData {
if fd == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: dirPath)
}
let pathResult = FileManager.default().string(withFileSystemRepresentation:buf, length: Int(strlen(buf)))
let pathResult = FileManager.default.string(withFileSystemRepresentation:buf, length: Int(strlen(buf)))
return (fd, pathResult)
}

Expand Down Expand Up @@ -472,7 +472,7 @@ extension NSData {
} catch let err {
if let auxFilePath = auxFilePath {
do {
try FileManager.default().removeItem(atPath: auxFilePath)
try FileManager.default.removeItem(atPath: auxFilePath)
} catch _ {}
}
throw err
Expand All @@ -482,7 +482,7 @@ extension NSData {
if let auxFilePath = auxFilePath {
if rename(auxFilePath, path) != 0 {
do {
try FileManager.default().removeItem(atPath: auxFilePath)
try FileManager.default.removeItem(atPath: auxFilePath)
} catch _ {}
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
Expand Down
15 changes: 6 additions & 9 deletions Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ open class FileManager: NSObject {

/* Returns the default singleton instance.
*/
internal static let defaultInstance = FileManager()
open class func `default`() -> FileManager {
return defaultInstance
}
public static let `default` = FileManager()

/* Returns an NSArray of NSURLs locating the mounted volumes available on the computer. The property keys that can be requested are available in NSURL.
*/
Expand Down Expand Up @@ -461,7 +458,7 @@ open class FileManager: NSObject {
return
} else if errno == ENOTEMPTY {

let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
let fsRep = FileManager.default.fileSystemRepresentation(withPath: path)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
ps.advanced(by: 1).initialize(to: nil)
Expand Down Expand Up @@ -733,7 +730,7 @@ open class FileManager: NSObject {
return nil
}

guard let destination = try? FileManager.default().destinationOfSymbolicLink(atPath: path) else {
guard let destination = try? FileManager.default.destinationOfSymbolicLink(atPath: path) else {
return nil
}

Expand Down Expand Up @@ -874,7 +871,7 @@ extension FileManager {
init?(path: String) {
let url = URL(fileURLWithPath: path)
self.baseURL = url
guard let ie = FileManager.default().enumerator(at: url, includingPropertiesForKeys: nil, options: [], errorHandler: nil) else {
guard let ie = FileManager.default.enumerator(at: url, includingPropertiesForKeys: nil, options: [], errorHandler: nil) else {
return nil
}
self.innerEnumerator = ie
Expand Down Expand Up @@ -906,8 +903,8 @@ extension FileManager {
_errorHandler = errorHandler

if let path = _url.path {
if FileManager.default().fileExists(atPath: path) {
let fsRep = FileManager.default().fileSystemRepresentation(withPath: path)
if FileManager.default.fileExists(atPath: path) {
let fsRep = FileManager.default.fileSystemRepresentation(withPath: path)
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
ps.advanced(by: 1).initialize(to: nil)
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSKeyedArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ open class NSKeyedArchiver : NSCoder {
if finishedEncoding {
try _NSCleanupTemporaryFile(auxFilePath, path)
} else {
try FileManager.default().removeItem(atPath: auxFilePath)
try FileManager.default.removeItem(atPath: auxFilePath)
}
} catch _ {
}
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ internal extension String {
}

let temp = _stringByRemovingPrefix(prefix)
if FileManager.default().fileExists(atPath: temp) {
if FileManager.default.fileExists(atPath: temp) {
return temp
}

Expand Down Expand Up @@ -347,7 +347,7 @@ public extension NSString {

default:
resolvedPath = resolvedPath.bridge().stringByAppendingPathComponent(component)
if let destination = FileManager.default()._tryToResolveTrailingSymlinkInPath(resolvedPath) {
if let destination = FileManager.default._tryToResolveTrailingSymlinkInPath(resolvedPath) {
resolvedPath = destination
}
}
Expand Down Expand Up @@ -427,7 +427,7 @@ public extension NSString {
}

var isDirectory = false
let exists = FileManager.default().fileExists(atPath: path, isDirectory: &isDirectory)
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory
}

Expand All @@ -436,7 +436,7 @@ public extension NSString {
internal func _getNamesAtURL(_ filePathURL: URL, prependWith: String, namePredicate: _FileNamePredicate, typePredicate: _FileNamePredicate) -> [String] {
var result: [String] = []

if let enumerator = FileManager.default().enumerator(at: filePathURL, includingPropertiesForKeys: nil, options: .skipsSubdirectoryDescendants, errorHandler: nil) {
if let enumerator = FileManager.default.enumerator(at: filePathURL, includingPropertiesForKeys: nil, options: .skipsSubdirectoryDescendants, errorHandler: nil) {
for item in enumerator.lazy.map({ $0 as! URL }) {
let itemName = item.lastPathComponent

Expand Down Expand Up @@ -618,14 +618,14 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
if fd == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
}
let pathResult = FileManager.default().string(withFileSystemRepresentation: buf, length: Int(strlen(buf)))
let pathResult = FileManager.default.string(withFileSystemRepresentation: buf, length: Int(strlen(buf)))
return (fd, pathResult)
}

internal func _NSCleanupTemporaryFile(_ auxFilePath: String, _ filePath: String) throws {
if rename(auxFilePath, filePath) != 0 {
do {
try FileManager.default().removeItem(atPath: auxFilePath)
try FileManager.default.removeItem(atPath: auxFilePath)
} catch _ {
}
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ open class Task: NSObject {
open var arguments: [String]?
open var environment: [String : String]? // if not set, use current

open var currentDirectoryPath: String = FileManager.defaultInstance.currentDirectoryPath
open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath

// standard I/O channels; could be either an NSFileHandle or an NSPipe
open var standardInput: AnyObject? {
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ open class NSURL: NSObject, NSSecureCoding, NSCopying {
} else {
absolutePath = path
}
let _ = FileManager.default().fileExists(atPath: absolutePath, isDirectory: &isDir)
let _ = FileManager.default.fileExists(atPath: absolutePath, isDirectory: &isDir)
} catch {
// ignored
}
Expand All @@ -287,7 +287,7 @@ open class NSURL: NSObject, NSSecureCoding, NSCopying {
if thePath.hasSuffix("/") {
isDir = true
} else {
let _ = FileManager.default().fileExists(atPath: path, isDirectory: &isDir)
let _ = FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
}

self.init(fileURLWithPath: thePath, isDirectory: isDir, relativeTo: nil)
Expand Down Expand Up @@ -697,7 +697,7 @@ extension NSURL {
if let urlWithoutDirectory = result,
let path = urlWithoutDirectory.path {
var isDir : Bool = false
if FileManager.default().fileExists(atPath: path, isDirectory: &isDir) && isDir {
if FileManager.default.fileExists(atPath: path, isDirectory: &isDir) && isDir {
result = self.appendingPathComponent(pathComponent, isDirectory: true)
}
}
Expand Down Expand Up @@ -747,7 +747,7 @@ extension NSURL {
if selfPath.hasPrefix("/") {
absolutePath = selfPath
} else {
let workingDir = FileManager.default().currentDirectoryPath
let workingDir = FileManager.default.currentDirectoryPath
absolutePath = workingDir.bridge().stringByAppendingPathComponent(selfPath)
}

Expand All @@ -769,15 +769,15 @@ extension NSURL {

default:
resolvedPath = resolvedPath.bridge().stringByAppendingPathComponent(component)
if let destination = FileManager.default()._tryToResolveTrailingSymlinkInPath(resolvedPath) {
if let destination = FileManager.default._tryToResolveTrailingSymlinkInPath(resolvedPath) {
resolvedPath = destination
}
}
}

// It might be a responsibility of NSURL(fileURLWithPath:). Check it.
var isExistingDirectory = false
let _ = FileManager.default().fileExists(atPath: resolvedPath, isDirectory: &isExistingDirectory)
let _ = FileManager.default.fileExists(atPath: resolvedPath, isDirectory: &isExistingDirectory)

if excludeSystemDirs {
resolvedPath = resolvedPath._tryToRemovePathPrefix("/private") ?? resolvedPath
Expand Down
14 changes: 7 additions & 7 deletions TestFoundation/TestNSBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TestNSBundle : XCTestCase {
let testPlist = bundle.urlForResource("Test", withExtension: "plist")
XCTAssertNotNil(testPlist)
XCTAssertEqual("Test.plist", testPlist!.lastPathComponent)
XCTAssert(FileManager.default().fileExists(atPath: testPlist!.path!))
XCTAssert(FileManager.default.fileExists(atPath: testPlist!.path!))

// aliases, paths
XCTAssertEqual(testPlist!.path, bundle.urlForResource("Test", withExtension: "plist", subdirectory: nil)!.path)
Expand Down Expand Up @@ -111,20 +111,20 @@ class TestNSBundle : XCTestCase {
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"

do {
try FileManager.default().createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)

// Make a flat bundle in the playground
let bundlePath = tempDir + _bundleName
try FileManager.default().createDirectory(atPath: bundlePath, withIntermediateDirectories: false, attributes: nil)
try FileManager.default.createDirectory(atPath: bundlePath, withIntermediateDirectories: false, attributes: nil)

// Put some resources in the bundle
for n in _bundleResourceNames {
let _ = FileManager.default().createFile(atPath: bundlePath + "/" + n, contents: nil, attributes: nil)
let _ = FileManager.default.createFile(atPath: bundlePath + "/" + n, contents: nil, attributes: nil)
}
// Add a resource into a subdirectory
let subDirPath = bundlePath + "/" + _subDirectory
try FileManager.default().createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
let _ = FileManager.default().createFile(atPath: subDirPath + "/" + _main + "." + _type, contents: nil, attributes: nil)
try FileManager.default.createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
let _ = FileManager.default.createFile(atPath: subDirPath + "/" + _main + "." + _type, contents: nil, attributes: nil)
} catch _ {
return nil
}
Expand All @@ -135,7 +135,7 @@ class TestNSBundle : XCTestCase {

private func _cleanupPlayground(_ location: String) {
do {
try FileManager.default().removeItem(atPath: location)
try FileManager.default.removeItem(atPath: location)
} catch _ {
// Oh well
}
Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TestNSData: XCTestCase {
let savePath = URL(fileURLWithPath: "/var/tmp/Test.plist")
do {
try saveData.write(to: savePath, options: .dataWritingAtomic)
let fileManager = FileManager.default()
let fileManager = FileManager.default
XCTAssertTrue(fileManager.fileExists(atPath: savePath.path!))
try! fileManager.removeItem(atPath: savePath.path!)
} catch _ {
Expand Down
Loading