-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIO for Trevor
46 lines (35 loc) · 1.47 KB
/
FileIO for Trevor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// ViewController.swift
// testFileIO
//
// Created by Robert Leon Collins Jr on 10/21/15.
// Copyright © 2015 Robert Leon Collins Jr. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let file = "file.txt" //this is the file. we will write to and read from it
let textArray: [String: String] = ["firstPerson": "Robert", "secondPerson": "Johnny", "thirdPerson": "Chi", "fourthPerson": "Trevor"] //trying to write an array
let text = NSDictionary(dictionary: textArray)
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = dir.stringByAppendingPathComponent(file);
//writing
do {
try text.writeToFile(path, atomically: false)
print("I SUCCEEDED")
}
catch let error {"THERE IS A MISTKE!!! \(error)"}
//reading
do {
let text2 = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(text2)
}
catch let errorTwo {"I can't read... \(errorTwo)"}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}