Skip to content

Update for Xcode9, Swift 4.0 #24

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode7.1
osx_image: xcode9.1

script:
- bundle exec rake default
9 changes: 8 additions & 1 deletion SwiftFilePath.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0910;
ORGANIZATIONNAME = "Norihiro Sakamoto";
TargetAttributes = {
CC7832EB1A610124005E77C3 = {
Expand Down Expand Up @@ -598,6 +598,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -636,6 +637,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -941,6 +943,7 @@
D63BC3AC1C172BCD0071D0E2 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D63BC3AD1C172BCD0071D0E2 /* Build configuration list for PBXNativeTarget "SwiftFilePath-tvOS Tests" */ = {
isa = XCConfigurationList;
Expand All @@ -949,6 +952,7 @@
D63BC3AF1C172BCD0071D0E2 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D63BC3BA1C172C540071D0E2 /* Build configuration list for PBXNativeTarget "SwiftFilePath-watchOS" */ = {
isa = XCConfigurationList;
Expand All @@ -957,6 +961,7 @@
D63BC3BC1C172C540071D0E2 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D63BC3D31C172C740071D0E2 /* Build configuration list for PBXNativeTarget "SwiftFilePath-Mac" */ = {
isa = XCConfigurationList;
Expand All @@ -965,6 +970,7 @@
D63BC3D51C172C740071D0E2 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D63BC3D61C172C740071D0E2 /* Build configuration list for PBXNativeTarget "SwiftFilePath-Mac Tests" */ = {
isa = XCConfigurationList;
Expand All @@ -973,6 +979,7 @@
D63BC3D81C172C740071D0E2 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -36,6 +37,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
112 changes: 47 additions & 65 deletions SwiftFilePath/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,120 +10,102 @@ public class Path {

// MARK: - Class methods

public class func isDir(path:NSString) -> Bool {
public class func isDir(_ path: String) -> Bool {
var isDirectory: ObjCBool = false
NSFileManager.defaultManager().fileExistsAtPath(path as String, isDirectory:&isDirectory)
return isDirectory ? true : false
FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return isDirectory.boolValue
}

// MARK: - Instance properties and initializer

lazy var fileManager = NSFileManager.defaultManager()
public let path_string:String
var fileManager: FileManager {
return FileManager.default
}

public let url: URL

public init(_ url: URL) {
assert(url.isFileURL, "Must be file URL")
self.url = url
}

public init(_ p: String) {
self.path_string = p
public convenience init(_ path: String) {
self.init(URL(fileURLWithPath: path))
}

// MARK: - Instance val

public var attributes:NSDictionary?{
public var attributes: [FileAttributeKey: Any]?{
get { return self.loadAttributes() }
}

public var asString: String {
return path_string
return url.path
}

public var exists: Bool {
return fileManager.fileExistsAtPath(path_string)
return fileManager.fileExists(atPath: url.path)
}

public var isDir: Bool {
return Path.isDir(path_string);
return Path.isDir(url.path)
}

public var basename:NSString {
return ( path_string as NSString ).lastPathComponent
public var basename: String {
return url.lastPathComponent
}

public var parent: Path{
return Path( (path_string as NSString ).stringByDeletingLastPathComponent )
public var parent: Path {
return Path(url.deletingLastPathComponent())
}

// MARK: - Instance methods

public func toString() -> String {
return path_string
return url.path
}

public func remove() -> Result<Path,NSError> {
@discardableResult
public func remove() -> Result<Path, Error> {
assert(self.exists,"To remove file, file MUST be exists")
var error: NSError?
let result: Bool
do {
try fileManager.removeItemAtPath(path_string)
result = true
} catch let error1 as NSError {
error = error1
result = false
try fileManager.removeItem(at: url)
return Result(success: self)
} catch let error {
return Result(failure: error)
}
return result
? Result(success: self)
: Result(failure: error!);
}

public func copyTo(toPath:Path) -> Result<Path,NSError> {
@discardableResult
public func copyTo(_ toPath: Path) -> Result<Path,Error> {
assert(self.exists,"To copy file, file MUST be exists")
var error: NSError?
let result: Bool
do {
try fileManager.copyItemAtPath(path_string,
toPath: toPath.toString())
result = true
} catch let error1 as NSError {
error = error1
result = false
try fileManager.copyItem(at: url, to: toPath.url)
return Result(success: self)
} catch let error {
return Result(failure: error)
}
return result
? Result(success: self)
: Result(failure: error!)
}

public func moveTo(toPath:Path) -> Result<Path,NSError> {
@discardableResult
public func moveTo(_ toPath: Path) -> Result<Path,Error> {
assert(self.exists,"To move file, file MUST be exists")
var error: NSError?
let result: Bool
do {
try fileManager.moveItemAtPath(path_string,
toPath: toPath.toString())
result = true
} catch let error1 as NSError {
error = error1
result = false
try fileManager.moveItem(at: url, to: toPath.url)
return Result(success: self)
} catch let error {
return Result(failure: error)
}
return result
? Result(success: self)
: Result(failure: error!)
}

private func loadAttributes() -> NSDictionary? {
assert(self.exists,"File must be exists to load file.< \(path_string) >")
var loadError: NSError?
let result: [NSObject: AnyObject]?
private func loadAttributes() -> [FileAttributeKey: Any]? {
assert(self.exists,"File must be exists to load file.< \(url.path) >")
do {
result = try self.fileManager.attributesOfItemAtPath(path_string)
} catch let error as NSError {
loadError = error
result = nil
}

if let error = loadError {
return try fileManager.attributesOfItem(atPath: url.path)
} catch let error {
print("Error< \(error.localizedDescription) >")
}

return result
return nil
}

}
Expand All @@ -132,7 +114,7 @@ public class Path {

extension Path: CustomStringConvertible {
public var description: String {
return "\(NSStringFromClass(self.dynamicType))<path:\(path_string)>"
return "\(String(describing: Path.self))<path:\(url.path)>"
}
}

Expand Down
Loading