Skip to content

Commit

Permalink
Updated all example codes in chapter-fileManagement to Swift 2.0 (Xco…
Browse files Browse the repository at this point in the history
…de 7)
  • Loading branch information
vandadnp committed Jun 22, 2015
1 parent 780e7d4 commit e018c3e
Show file tree
Hide file tree
Showing 38 changed files with 290 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
9D1CE3381958040A0068FE3B /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0600;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Pixolity Ltd.";
TargetAttributes = {
9D1CE33F1958040A0068FE3B = {
Expand Down Expand Up @@ -243,6 +244,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -309,6 +311,7 @@
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
INFOPLIST_FILE = "Creating Folders on Disk/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -320,6 +323,7 @@
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
INFOPLIST_FILE = "Creating Folders on Disk/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -339,6 +343,7 @@
INFOPLIST_FILE = "Creating Folders on DiskTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
METAL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
};
Expand All @@ -355,6 +360,7 @@
INFOPLIST_FILE = "Creating Folders on DiskTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
METAL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
};
Expand All @@ -379,6 +385,7 @@
9D1CE3591958040A0068FE3B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
9D1CE35A1958040A0068FE3B /* Build configuration list for PBXNativeTarget "Creating Folders on DiskTests" */ = {
isa = XCConfigurationList;
Expand All @@ -387,6 +394,7 @@
9D1CE35C1958040A0068FE3B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0600"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -48,6 +48,8 @@
ReferencedContainer = "container:Creating Folders on Disk.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
Expand All @@ -57,8 +59,10 @@
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D1CE33F1958040A0068FE3B"
Expand All @@ -76,7 +80,8 @@
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D1CE33F1958040A0068FE3B"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// Vandad Nahavandipoor for his work. Feel free to visit my blog
// at http://vandadnp.wordpress.com for daily tips and tricks in Swift
// and Objective-C and various other programming languages.
//
//
// You can purchase "iOS 8 Swift Programming Cookbook" from
// the following URL:
// http://shop.oreilly.com/product/0636920034254.do
Expand All @@ -30,19 +30,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func example1(){

let tempPath = NSTemporaryDirectory()
let imagesPath = tempPath.stringByAppendingPathComponent("images")
var error:NSError?
let fileManager = NSFileManager()

if fileManager.createDirectoryAtPath(imagesPath,
withIntermediateDirectories: true,
attributes: nil,
error: nil){
println("Created the directory")
} else {
println("Could not create the directory")
}
let tempPath = NSTemporaryDirectory()
let imagesPath = tempPath.stringByAppendingPathComponent("images")
let fileManager = NSFileManager()

do {
try fileManager.createDirectoryAtPath(imagesPath,
withIntermediateDirectories: true,
attributes: nil)
print("Created the directory")
} catch {
print("Could not create the directory")
}
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.pixolity.ios.cookbook.app</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.pixolity.ios.cookbook.app</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
9DAD55EB19581626009F9E4F /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0600;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Pixolity Ltd.";
TargetAttributes = {
9DAD55F219581626009F9E4F = {
Expand Down Expand Up @@ -243,6 +244,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -309,6 +311,7 @@
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
INFOPLIST_FILE = "Deleting Files and Folders/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -320,6 +323,7 @@
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
INFOPLIST_FILE = "Deleting Files and Folders/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -339,6 +343,7 @@
INFOPLIST_FILE = "Deleting Files and FoldersTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
METAL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
};
Expand All @@ -355,6 +360,7 @@
INFOPLIST_FILE = "Deleting Files and FoldersTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
METAL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.pixolity.ios.cookbook.app;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
};
Expand All @@ -379,6 +385,7 @@
9DAD560C19581626009F9E4F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
9DAD560D19581626009F9E4F /* Build configuration list for PBXNativeTarget "Deleting Files and FoldersTests" */ = {
isa = XCConfigurationList;
Expand All @@ -387,6 +394,7 @@
9DAD560F19581626009F9E4F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0600"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -48,6 +48,8 @@
ReferencedContainer = "container:Deleting Files and Folders.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
Expand All @@ -57,8 +59,10 @@
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9DAD55F219581626009F9E4F"
Expand All @@ -76,7 +80,8 @@
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9DAD55F219581626009F9E4F"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func createFolderAtPath(path: String){

var error:NSError?

if fileManager.createDirectoryAtPath(path,
withIntermediateDirectories: true,
attributes: nil,
error: &error) == false && error != nil{
println("Failed to create folder at \(path), error = \(error!)")
do{
try fileManager.createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError{
print("Failed to create folder at \(path), error = \(error)")
}

}
Expand All @@ -52,70 +50,61 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let fileName = NSString(format: "%lu.txt", counter)
let path = folder.stringByAppendingPathComponent(String(fileName))
let fileContents = "Some text"
var error:NSError?
if fileContents.writeToFile(path,
atomically: true,
encoding: NSUTF8StringEncoding,
error: &error) == false{
if let theError = error{
println("Failed to save the file at path \(path)" +
" with error = \(theError)")
}
do{
try fileContents.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError{
print("Failed to save the file at path \(path)" +
" with error = \(error)")
}

}

}

/* Enumerates all files/folders at a given path */
func enumerateFilesInFolder(folder: String){

var error:NSError?
let contents = fileManager.contentsOfDirectoryAtPath(
folder,
error: &error)!

if let theError = error{
println("An error occurred \(theError)")
} else if contents.count == 0{
println("No content was found")
} else {
println("Contents of path \(folder) = \(contents)")
do{
let contents = try fileManager.contentsOfDirectoryAtPath(folder)
if contents.count == 0{
print("No content was found")
} else {
print("Contents of path \(folder) = \(contents)")
}
} catch let error as NSError{
print("An error occurred \(error)")
}


}

/* Deletes all files/folders in a given path */
func deleteFilesInFolder(folder: String){

var error:NSError?
let contents = fileManager.contentsOfDirectoryAtPath(folder,
error: &error) as! [String]

if let theError = error{
println("An error occurred = \(theError)")
} else {
do{
let contents = try fileManager.contentsOfDirectoryAtPath(folder)
for fileName in contents{
let filePath = folder.stringByAppendingPathComponent(fileName)
if fileManager.removeItemAtPath(filePath, error: nil){
println("Successfully removed item at path \(filePath)")
} else {
println("Failed to remove item at path \(filePath)")
do {
try fileManager.removeItemAtPath(filePath)
print("Successfully removed item at path \(filePath)")
} catch _ {
print("Failed to remove item at path \(filePath)")
}
}
} catch let error as NSError{
print("An error occurred = \(error)")
}

}

/* Deletes a folder with a given path */
func deleteFolderAtPath(path: String){

var error:NSError?
if fileManager.removeItemAtPath(path, error: &error){
println("Successfully deleted the path \(path)")
} else {
if let theError = error{
println("Failed to remove path \(path) with error \(theError)")
}
do {
try fileManager.removeItemAtPath(path)
print("Successfully deleted the path \(path)")
} catch let error as NSError {
print("Failed to remove path \(path) with error \(error)")
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.pixolity.ios.cookbook.app</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Loading

0 comments on commit e018c3e

Please sign in to comment.