if let filepath = NSBundle.mainBundle().pathForResource(“abc”, ofType: "txt")
{
var test_string=“abcd”
let fileContent:NSData= test_string.dataUsingEncoding(NSUTF8StringEncoding)!
fileContent.writeToFile(filepath, atomically: true)
let contents = try NSString(contentsOfFile: filepath, usedEncoding: nil) as String
print("file content : "+contents)
}
catch
{
self.view.makeToast("write to file error")
}
Asked
Active
Viewed 1,106 times
1

Rohit Pradhan
- 3,867
- 1
- 21
- 29

sharsad k k
- 171
- 1
- 10
-
You can't write to the bundle folder – Leo Dabus Feb 25 '16 at 06:05
-
If you need a place to write your string you can use the documents, preferences or application support folder – Leo Dabus Feb 25 '16 at 06:06
-
@Leo Dabus . how can i use documents, preferences or application support folder – sharsad k k Feb 25 '16 at 06:17
-
http://stackoverflow.com/a/26557965/2303865 – Leo Dabus Feb 25 '16 at 07:05
-
1Thanks a lot... its working fine...:) – sharsad k k Feb 25 '16 at 07:39
1 Answers
0
Check Read and write data from text file.
I think your problem is the path, because a simulator folder can be any where but for device no, it is better to save the file inside the folder of your apps itself.
And if you are writing text, you can just write try test_string.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
-
-
have you check the path you writing to your device? It's something like this **/var/mobile/Containers/Data/Application/1AS7BED8-FF5F-48B1-AWE9-1EEF2EE3E500/Documents/abc.txt**. I think you're saving it inside your project bundle – Lee Feb 25 '16 at 06:25
-
my file path : "/var/mobile/Containers/Bundle/Application/7B0B3136-B2F5-4A4A-B989-04FE5FDA5979/fileSwift.app/abc.txt" . How can i change to "/var/mobile/Containers/Data/Application/..." – sharsad k k Feb 25 '16 at 06:29
-
Im using the method in the link above for my apps and it does work for both simulator and device. However, when i tried your code the filepath is a nil. I think the code i learnt is easy to use and understand – Lee Feb 25 '16 at 06:45
-
i created abc.txt file inside my app folder and do the code i wrote above. can u give me detail code for create , read and write of text file – sharsad k k Feb 25 '16 at 06:53
-
I had attached actually in the post [this is the link](http://stackoverflow.com/questions/24097826/read-and-write-data-from-text-file?answertab=votes#tab-top) – Lee Feb 25 '16 at 07:12
-
Thanks a lot.....its working fine..:) let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) // create the destination url for the text file to be saved let fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("abc.txt") try text.writeToURL(fileDestinationUrl, atomically: true, encoding: NSUTF8StringEncoding) – sharsad k k Feb 25 '16 at 07:37
-
its okay haha, I had actually added the link in the post. Just maybe the link color is not that obvious. mark as answer please if dont mind :) – Lee Feb 25 '16 at 07:44