Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Created August 29, 2016 20:37
Show Gist options
  • Save aybekckaya/90ac8d24963674bdbe117c8e3df8a1bb to your computer and use it in GitHub Desktop.
Save aybekckaya/90ac8d24963674bdbe117c8e3df8a1bb to your computer and use it in GitHub Desktop.
//
// StringExtensions.swift
// PlistSwift
//
// Created by aybek can kaya on 16/01/16.
// Copyright © 2016 aybek can kaya. All rights reserved.
//
import Foundation
extension String
{
// Document Path Extensions
/**
Finds app's installed directory on real device or simulator
- returns: path string of installed directory
*/
static func documentsPath()->String
{
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
guard paths.count > 0 else
{
return ""
}
let documentsDirectory = paths[0]
return documentsDirectory
}
/**
Gets bundle path Item
- parameter theName: name of file Item
- parameter theType: type of file Item
- returns: String value of bundlePath
*/
static func bundlePath(name theName:String! ,type theType:String!)->String
{
let bundleResource = NSBundle.mainBundle().pathForResource(theName, ofType: theType)
return bundleResource!
}
/**
Creates file at given path. Usage : "pathName".createFile() . If file exists the file will not be re-created
*/
func createFile()
{
guard self.fileExistsAtPath() == false else{
return
}
let tempString = "";
do {
try tempString.writeToFile(self, atomically: true, encoding: NSUTF8StringEncoding)
}
catch {
////print("errorr")
}
}
/**
Creates directory at given path. Usage : "pathName".createFile() . If directory exists the directory will not be re-created
*/
func createDirectory()
{
guard self.fileExistsAtPath() == false else{
return
}
do {
try NSFileManager.defaultManager().createDirectoryAtPath(self, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
////print(error.localizedDescription);
}
}
/**
Copies file from instanced location to given path
- parameter thePath: copying location path
*/
func copyToPath (path thePath:String)
{
guard thePath.fileExistsAtPath() == false else
{
return;
}
let fileManager = NSFileManager.defaultManager()
do{
try fileManager.copyItemAtPath(self, toPath: thePath)
}catch let error as NSError{
////print(error.localizedDescription)
}
}
func deleteFile()
{
let fileManager = NSFileManager.defaultManager();
do{
try fileManager.removeItemAtPath(self)
}catch let error as NSError{
////print(error.localizedDescription)
}
}
func deleteDirectory()
{
}
func allFiles()->[String]
{
if !self.isDirectory()
{
return []
}
let manager = NSFileManager.defaultManager()
do
{
let files = try manager.contentsOfDirectoryAtPath(self)
return files
}
catch
{
}
return []
}
func isDirectory()->Bool
{
var isDir:ObjCBool = false
let manager = NSFileManager()
if manager.fileExistsAtPath(self, isDirectory: &isDir)
{
if Bool(isDir) == true
{
return true
}
}
return false
}
/**
Checks whether file exist at given path
- returns: true if file exist at path
*/
func fileExistsAtPath()->Bool
{
let fileManager = NSFileManager.defaultManager()
if(fileManager.fileExistsAtPath(self))
{
return true
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment