FileKit
FileKit is a Swift framework that allows for simple and expressive file management.
Development happens in the develop branch.
Installation
Compatibility
-
OS X 10.9+ / iOS 8.0+ / watchOS 2.0 / tvOS 9.0
-
Xcode 7.1+, Swift 2.1+
Install Using CocoaPods
CocoaPods is a centralized dependency manager for
Objective-C and Swift. Go here
to learn more.
-
Add the project to your Podfile.
-
Run
pod install
and open the.xcworkspace
file to launch Xcode. -
Import the FileKit framework.
Install Using Carthage
Carthage is a decentralized dependency
manager for Objective-C and Swift.
-
Add the project to your Cartfile.
github "nvzqz/FileKit"
-
Run
carthage update
and follow the additional steps
in order to add FileKit to your project. -
Import the FileKit framework.
Usage
Paths
Paths are handled with the Path
structure.
Operations
New Files
A blank file can be written by calling createFile()
on an Path
.
New Directories
A directory can be created by calling createDirectory()
on an Path
.
Intermediate directories are created by default.
New Symlinks
A symbolic link can be created by calling createSymlinkToPath(_:)
on an Path
.
Finding Paths
You can find all paths with the ".txt" extension five folders deep into the
Desktop with:
A negative searchDepth
will make it run until every path in self
is checked
against.
You can even map a function to paths found and get the non-nil results:
Iterating Through Paths
Because Path
conforms to SequenceType
, it can be iterated through with a
for
loop.
Current Working Directory
The current working directory for the process can be changed with Path.Current
.
To quickly change the current working directory to a path and back, there's the
changeDirectory(_:)
method:
Common Ancestor
A common ancestor between two paths can be obtained:
+
Operator
Appends two paths and returns the result
It can also be used to concatenate a string and a path, making the string value
a Path
beforehand.
+=
Operator
Appends the right path to the left path. Also works with a String
.
%
Operator
Returns the standardized version of the path.
*
Operator
Returns the resolved version of the path.
^
Operator
Returns the path's parent path.
->>
Operator
Moves the file at the left path to the right path.
Path
counterpart: moveFile(to:)
File
counterpart: move(to:)
->!
Operator
Forcibly moves the file at the left path to the right path by deleting anything
at the left path before moving the file.
+>>
Operator
Copies the file at the left path to the right path.
Path
counterpart: copyFile(to:)
File
counterpart: copy(to:)
+>!
Operator
Forcibly copies the file at the left path to the right path by deleting anything
at the left path before copying the file.
=>>
Operator
Creates a symlink of the left path at the right path.
Path
counterpart: symlinkFile(to:)
File
counterpart: symlink(to:)
=>!
Operator
Forcibly creates a symlink of the left path at the right path by deleting
anything at the left path before creating the symlink.
Subscripting
Subscripting an Path
will return all of its components up to and including
the index.
standardize()
Standardizes the path.
The same as doing:
resolve()
Resolves the path's symlinks.
The same as doing:
Files
A file can be made using File
with a DataType
for its data type.
Files can be compared by size.
Operators
|>
Operator
Writes the data on the left to the file on the right.
TextFile
The TextFile
class allows for reading and writing strings to a file.
Although it is a subclass of File<String>
, TextFile
offers some functionality
that File<String>
doesn't.
|>>
Operator
Appends the string on the left to the TextFile
on the right.
NSDictionaryFile
A typealias to File<NSDictionary>
.
NSArrayFile
A typealias to File<NSArray>
NSDataFile
A typealias to File<NSData>
DataFile
The DataFile
class allows for reading and writing Data
to a file.
Although it is a subclass of File<Data>
, DataFile
offers some functionality
that File<Data>
doesn't. You could specify Data.ReadingOptions
and Data.WritingOptions
Encodable/Decodable
You can use any Codable
object with File
.
Alternatively you can use utility methods
File Permissions
The FilePermissions
struct allows for seeing the permissions of the current
process for a given file.
Data Types
All types that conform to DataType
can be used to satisfy the generic type for
File
.
Readable Protocol
A Readable
type must implement the static method read(from: Path)
.
All Readable
types can be initialized with init(contentsOfPath:)
.
Writable Protocol
A Writable
type must implement write(to: Path, atomically: Bool)
.
Writing done by write(to: Path)
is done atomically by default.
WritableToFile
Types that have a write(toFile:atomically:)
method that takes in a String
for the file path can conform to Writable
by simply conforming to
WritableToFile
.
WritableConvertible
If a type itself cannot be written to a file but can output a writable type,
then it can conform to WritableConvertible
and become a Writable
that way.
FileKitError
The type for all errors thrown by FileKit operations is FileKitError
.
Errors can be converted to String
directly for any logging. If only the error
message is needed, FileKitError
has a message
property that states why the
error occurred.