Read file in swift, iOS playground











up vote
12
down vote

favorite
3












Having searched through the many (many!) swift playground questions to even craft this code, I'm still struggling.



I've placed a text file in the Resources folder of package contents, and it appears as an alias (link) in the running temp files generated by the playground (/var/folders/ ...).



import UIKit

let bundle = NSBundle.mainBundle()

let myFilePath = bundle.pathForResource("dict1", ofType: "txt")

println(myFilePath) // <-- this is correct, there is a shortcut to the Resource file at this location

var error:NSError?

var content = String(contentsOfFile:myFilePath!, encoding:NSUTF8StringEncoding, error: &error)

println(content!) // <-- this is *NOT* the file contents [EDIT: see later note]

// Demonstrate there's no error
if let theError = error {
print("(theError.localizedDescription)")
} else {
print("No error")
}


The problem being, that content is shown in the playground output as being Some "applengamenhownswiftntoken", rather than the file contents as expected.



It's finding the file, because if I change the filename, it errors. Any advice on getting the file contents?



Xcode 6.1



EDIT:
So, the actual problem was that I wasn't expecting the playground output (including, println) to be escaped. That, combined with fatigue and other stupidities led me to believe there was a problem, when none existed.



Interestingly, not everything seems to be escaped in playground:



println("foonbar")    // Outputs "foonbar", escaped
println("\n") // Outputs "n", unescaped









share|improve this question




















  • 1




    That code works ok for me if I put a dict1.txt file in the appropriate place. What's in your dict1.txt file and how does it differ from what you see in your content variable?
    – Mike S
    Oct 23 '14 at 22:37






  • 1




    @MikeS, it's amazing how your simple question led me to the (obvious) answer. Yes, of course content matches the file. I should never code tired. What threw me off was the escaped 'n', which made the content look like a path.
    – alttag
    Oct 24 '14 at 13:03












  • As I'm learning, playground output of println != console output of println. More info on the console at this question.
    – alttag
    Oct 24 '14 at 16:07















up vote
12
down vote

favorite
3












Having searched through the many (many!) swift playground questions to even craft this code, I'm still struggling.



I've placed a text file in the Resources folder of package contents, and it appears as an alias (link) in the running temp files generated by the playground (/var/folders/ ...).



import UIKit

let bundle = NSBundle.mainBundle()

let myFilePath = bundle.pathForResource("dict1", ofType: "txt")

println(myFilePath) // <-- this is correct, there is a shortcut to the Resource file at this location

var error:NSError?

var content = String(contentsOfFile:myFilePath!, encoding:NSUTF8StringEncoding, error: &error)

println(content!) // <-- this is *NOT* the file contents [EDIT: see later note]

// Demonstrate there's no error
if let theError = error {
print("(theError.localizedDescription)")
} else {
print("No error")
}


The problem being, that content is shown in the playground output as being Some "applengamenhownswiftntoken", rather than the file contents as expected.



It's finding the file, because if I change the filename, it errors. Any advice on getting the file contents?



Xcode 6.1



EDIT:
So, the actual problem was that I wasn't expecting the playground output (including, println) to be escaped. That, combined with fatigue and other stupidities led me to believe there was a problem, when none existed.



Interestingly, not everything seems to be escaped in playground:



println("foonbar")    // Outputs "foonbar", escaped
println("\n") // Outputs "n", unescaped









share|improve this question




















  • 1




    That code works ok for me if I put a dict1.txt file in the appropriate place. What's in your dict1.txt file and how does it differ from what you see in your content variable?
    – Mike S
    Oct 23 '14 at 22:37






  • 1




    @MikeS, it's amazing how your simple question led me to the (obvious) answer. Yes, of course content matches the file. I should never code tired. What threw me off was the escaped 'n', which made the content look like a path.
    – alttag
    Oct 24 '14 at 13:03












  • As I'm learning, playground output of println != console output of println. More info on the console at this question.
    – alttag
    Oct 24 '14 at 16:07













up vote
12
down vote

favorite
3









up vote
12
down vote

favorite
3






3





Having searched through the many (many!) swift playground questions to even craft this code, I'm still struggling.



I've placed a text file in the Resources folder of package contents, and it appears as an alias (link) in the running temp files generated by the playground (/var/folders/ ...).



import UIKit

let bundle = NSBundle.mainBundle()

let myFilePath = bundle.pathForResource("dict1", ofType: "txt")

println(myFilePath) // <-- this is correct, there is a shortcut to the Resource file at this location

var error:NSError?

var content = String(contentsOfFile:myFilePath!, encoding:NSUTF8StringEncoding, error: &error)

println(content!) // <-- this is *NOT* the file contents [EDIT: see later note]

// Demonstrate there's no error
if let theError = error {
print("(theError.localizedDescription)")
} else {
print("No error")
}


The problem being, that content is shown in the playground output as being Some "applengamenhownswiftntoken", rather than the file contents as expected.



It's finding the file, because if I change the filename, it errors. Any advice on getting the file contents?



Xcode 6.1



EDIT:
So, the actual problem was that I wasn't expecting the playground output (including, println) to be escaped. That, combined with fatigue and other stupidities led me to believe there was a problem, when none existed.



Interestingly, not everything seems to be escaped in playground:



println("foonbar")    // Outputs "foonbar", escaped
println("\n") // Outputs "n", unescaped









share|improve this question















Having searched through the many (many!) swift playground questions to even craft this code, I'm still struggling.



I've placed a text file in the Resources folder of package contents, and it appears as an alias (link) in the running temp files generated by the playground (/var/folders/ ...).



import UIKit

let bundle = NSBundle.mainBundle()

let myFilePath = bundle.pathForResource("dict1", ofType: "txt")

println(myFilePath) // <-- this is correct, there is a shortcut to the Resource file at this location

var error:NSError?

var content = String(contentsOfFile:myFilePath!, encoding:NSUTF8StringEncoding, error: &error)

println(content!) // <-- this is *NOT* the file contents [EDIT: see later note]

// Demonstrate there's no error
if let theError = error {
print("(theError.localizedDescription)")
} else {
print("No error")
}


The problem being, that content is shown in the playground output as being Some "applengamenhownswiftntoken", rather than the file contents as expected.



It's finding the file, because if I change the filename, it errors. Any advice on getting the file contents?



Xcode 6.1



EDIT:
So, the actual problem was that I wasn't expecting the playground output (including, println) to be escaped. That, combined with fatigue and other stupidities led me to believe there was a problem, when none existed.



Interestingly, not everything seems to be escaped in playground:



println("foonbar")    // Outputs "foonbar", escaped
println("\n") // Outputs "n", unescaped






ios xcode swift swift-playground






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 24 '14 at 16:04

























asked Oct 23 '14 at 22:12









alttag

7391520




7391520








  • 1




    That code works ok for me if I put a dict1.txt file in the appropriate place. What's in your dict1.txt file and how does it differ from what you see in your content variable?
    – Mike S
    Oct 23 '14 at 22:37






  • 1




    @MikeS, it's amazing how your simple question led me to the (obvious) answer. Yes, of course content matches the file. I should never code tired. What threw me off was the escaped 'n', which made the content look like a path.
    – alttag
    Oct 24 '14 at 13:03












  • As I'm learning, playground output of println != console output of println. More info on the console at this question.
    – alttag
    Oct 24 '14 at 16:07














  • 1




    That code works ok for me if I put a dict1.txt file in the appropriate place. What's in your dict1.txt file and how does it differ from what you see in your content variable?
    – Mike S
    Oct 23 '14 at 22:37






  • 1




    @MikeS, it's amazing how your simple question led me to the (obvious) answer. Yes, of course content matches the file. I should never code tired. What threw me off was the escaped 'n', which made the content look like a path.
    – alttag
    Oct 24 '14 at 13:03












  • As I'm learning, playground output of println != console output of println. More info on the console at this question.
    – alttag
    Oct 24 '14 at 16:07








1




1




That code works ok for me if I put a dict1.txt file in the appropriate place. What's in your dict1.txt file and how does it differ from what you see in your content variable?
– Mike S
Oct 23 '14 at 22:37




That code works ok for me if I put a dict1.txt file in the appropriate place. What's in your dict1.txt file and how does it differ from what you see in your content variable?
– Mike S
Oct 23 '14 at 22:37




1




1




@MikeS, it's amazing how your simple question led me to the (obvious) answer. Yes, of course content matches the file. I should never code tired. What threw me off was the escaped 'n', which made the content look like a path.
– alttag
Oct 24 '14 at 13:03






@MikeS, it's amazing how your simple question led me to the (obvious) answer. Yes, of course content matches the file. I should never code tired. What threw me off was the escaped 'n', which made the content look like a path.
– alttag
Oct 24 '14 at 13:03














As I'm learning, playground output of println != console output of println. More info on the console at this question.
– alttag
Oct 24 '14 at 16:07




As I'm learning, playground output of println != console output of println. More info on the console at this question.
– alttag
Oct 24 '14 at 16:07












2 Answers
2






active

oldest

votes

















up vote
11
down vote













You can try creating a class for opening and saving your files:



update: Xcode 10 • Swift 4.2



class File {
class func open(_ path: String, encoding: String.Encoding = .utf8) -> String? {
if FileManager.default.fileExists(atPath: path) {
do {
return try String(contentsOfFile: path, encoding: encoding)
} catch {
print(error)
return nil
}
}
return nil
}

class func save(_ path: String, _ content: String, encoding: String.Encoding = .utf8) -> Bool {
do {
try content.write(toFile: path, atomically: true, encoding: encoding)
return true
} catch {
print(error)
return false
}
}
}


usage: File.save



let stringToSave: String = "Your text"

let didSave = File.save("(NSHomeDirectory())/Desktop/file.txt", stringToSave)

if didSave { print("file saved") } else { print("error saving file") }


usage: File.open



if let loadedData = File.open("(NSHomeDirectory())/Desktop/file.txt") {
print(loadedData)
} else {
println("error reading file")
}


If you prefer working with URLs (as I do and recommended by Apple):
Note that in Swift 4, the class URL already exists.



class Url {
class func open(url: URL) -> String? {
do {
return try String(contentsOf: url, encoding: String.Encoding.utf8)
} catch {
print(error)
return nil
}
}

class func save(url: URL, fileContent: String) -> Bool {
do {
try fileContent.write(to: url, atomically: true, encoding: .utf8)
return true
} catch {
print(error)
return false
}
}
}





share|improve this answer






























    up vote
    6
    down vote













    I have seen this problem with .txt files created from .rtf files using TextEdit.



    I loaded a text.txt file in the resources folder of my playground using similar code to you. The file contents was "hello there" and was made by converting an .rtf file to .txt by changing the extension.



    let path = NSBundle.mainBundle().pathForResource("text", ofType: "txt")//or rtf for an rtf file
    var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
    println(text)


    The output was:



    {rtf1ansiansicpg1252cocoartf1343cocoasubrtf140
    {fonttblf0fswissfcharset0 Helvetica;}
    {colortbl;red255green255blue255;}
    margl1440margr1440vieww10800viewh8400viewkind0
    pardtx720tx1440tx2160tx2880tx3600tx4320tx5040tx5760tx6480tx7200tx7920tx8640pardirnatural



    f0fs24 cf0 hello there}



    So the "hello there" is embedded. This is the problem with all the layout information in a .rtf file.



    I went back to TextEdit and created a true .txt file. After opening a file - Format|Make Plain Text



    Now this same code gave the console output "hello there".



    Hope this helps.






    share|improve this answer























    • Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
      – alttag
      Oct 24 '14 at 13:04










    • Okay, glad you got your problem solved!
      – Steve Rosenberg
      Oct 24 '14 at 14:27











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26538375%2fread-file-in-swift-ios-playground%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    11
    down vote













    You can try creating a class for opening and saving your files:



    update: Xcode 10 • Swift 4.2



    class File {
    class func open(_ path: String, encoding: String.Encoding = .utf8) -> String? {
    if FileManager.default.fileExists(atPath: path) {
    do {
    return try String(contentsOfFile: path, encoding: encoding)
    } catch {
    print(error)
    return nil
    }
    }
    return nil
    }

    class func save(_ path: String, _ content: String, encoding: String.Encoding = .utf8) -> Bool {
    do {
    try content.write(toFile: path, atomically: true, encoding: encoding)
    return true
    } catch {
    print(error)
    return false
    }
    }
    }


    usage: File.save



    let stringToSave: String = "Your text"

    let didSave = File.save("(NSHomeDirectory())/Desktop/file.txt", stringToSave)

    if didSave { print("file saved") } else { print("error saving file") }


    usage: File.open



    if let loadedData = File.open("(NSHomeDirectory())/Desktop/file.txt") {
    print(loadedData)
    } else {
    println("error reading file")
    }


    If you prefer working with URLs (as I do and recommended by Apple):
    Note that in Swift 4, the class URL already exists.



    class Url {
    class func open(url: URL) -> String? {
    do {
    return try String(contentsOf: url, encoding: String.Encoding.utf8)
    } catch {
    print(error)
    return nil
    }
    }

    class func save(url: URL, fileContent: String) -> Bool {
    do {
    try fileContent.write(to: url, atomically: true, encoding: .utf8)
    return true
    } catch {
    print(error)
    return false
    }
    }
    }





    share|improve this answer



























      up vote
      11
      down vote













      You can try creating a class for opening and saving your files:



      update: Xcode 10 • Swift 4.2



      class File {
      class func open(_ path: String, encoding: String.Encoding = .utf8) -> String? {
      if FileManager.default.fileExists(atPath: path) {
      do {
      return try String(contentsOfFile: path, encoding: encoding)
      } catch {
      print(error)
      return nil
      }
      }
      return nil
      }

      class func save(_ path: String, _ content: String, encoding: String.Encoding = .utf8) -> Bool {
      do {
      try content.write(toFile: path, atomically: true, encoding: encoding)
      return true
      } catch {
      print(error)
      return false
      }
      }
      }


      usage: File.save



      let stringToSave: String = "Your text"

      let didSave = File.save("(NSHomeDirectory())/Desktop/file.txt", stringToSave)

      if didSave { print("file saved") } else { print("error saving file") }


      usage: File.open



      if let loadedData = File.open("(NSHomeDirectory())/Desktop/file.txt") {
      print(loadedData)
      } else {
      println("error reading file")
      }


      If you prefer working with URLs (as I do and recommended by Apple):
      Note that in Swift 4, the class URL already exists.



      class Url {
      class func open(url: URL) -> String? {
      do {
      return try String(contentsOf: url, encoding: String.Encoding.utf8)
      } catch {
      print(error)
      return nil
      }
      }

      class func save(url: URL, fileContent: String) -> Bool {
      do {
      try fileContent.write(to: url, atomically: true, encoding: .utf8)
      return true
      } catch {
      print(error)
      return false
      }
      }
      }





      share|improve this answer

























        up vote
        11
        down vote










        up vote
        11
        down vote









        You can try creating a class for opening and saving your files:



        update: Xcode 10 • Swift 4.2



        class File {
        class func open(_ path: String, encoding: String.Encoding = .utf8) -> String? {
        if FileManager.default.fileExists(atPath: path) {
        do {
        return try String(contentsOfFile: path, encoding: encoding)
        } catch {
        print(error)
        return nil
        }
        }
        return nil
        }

        class func save(_ path: String, _ content: String, encoding: String.Encoding = .utf8) -> Bool {
        do {
        try content.write(toFile: path, atomically: true, encoding: encoding)
        return true
        } catch {
        print(error)
        return false
        }
        }
        }


        usage: File.save



        let stringToSave: String = "Your text"

        let didSave = File.save("(NSHomeDirectory())/Desktop/file.txt", stringToSave)

        if didSave { print("file saved") } else { print("error saving file") }


        usage: File.open



        if let loadedData = File.open("(NSHomeDirectory())/Desktop/file.txt") {
        print(loadedData)
        } else {
        println("error reading file")
        }


        If you prefer working with URLs (as I do and recommended by Apple):
        Note that in Swift 4, the class URL already exists.



        class Url {
        class func open(url: URL) -> String? {
        do {
        return try String(contentsOf: url, encoding: String.Encoding.utf8)
        } catch {
        print(error)
        return nil
        }
        }

        class func save(url: URL, fileContent: String) -> Bool {
        do {
        try fileContent.write(to: url, atomically: true, encoding: .utf8)
        return true
        } catch {
        print(error)
        return false
        }
        }
        }





        share|improve this answer














        You can try creating a class for opening and saving your files:



        update: Xcode 10 • Swift 4.2



        class File {
        class func open(_ path: String, encoding: String.Encoding = .utf8) -> String? {
        if FileManager.default.fileExists(atPath: path) {
        do {
        return try String(contentsOfFile: path, encoding: encoding)
        } catch {
        print(error)
        return nil
        }
        }
        return nil
        }

        class func save(_ path: String, _ content: String, encoding: String.Encoding = .utf8) -> Bool {
        do {
        try content.write(toFile: path, atomically: true, encoding: encoding)
        return true
        } catch {
        print(error)
        return false
        }
        }
        }


        usage: File.save



        let stringToSave: String = "Your text"

        let didSave = File.save("(NSHomeDirectory())/Desktop/file.txt", stringToSave)

        if didSave { print("file saved") } else { print("error saving file") }


        usage: File.open



        if let loadedData = File.open("(NSHomeDirectory())/Desktop/file.txt") {
        print(loadedData)
        } else {
        println("error reading file")
        }


        If you prefer working with URLs (as I do and recommended by Apple):
        Note that in Swift 4, the class URL already exists.



        class Url {
        class func open(url: URL) -> String? {
        do {
        return try String(contentsOf: url, encoding: String.Encoding.utf8)
        } catch {
        print(error)
        return nil
        }
        }

        class func save(url: URL, fileContent: String) -> Bool {
        do {
        try fileContent.write(to: url, atomically: true, encoding: .utf8)
        return true
        } catch {
        print(error)
        return false
        }
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 16:11

























        answered Oct 24 '14 at 3:18









        Leo Dabus

        129k30263339




        129k30263339
























            up vote
            6
            down vote













            I have seen this problem with .txt files created from .rtf files using TextEdit.



            I loaded a text.txt file in the resources folder of my playground using similar code to you. The file contents was "hello there" and was made by converting an .rtf file to .txt by changing the extension.



            let path = NSBundle.mainBundle().pathForResource("text", ofType: "txt")//or rtf for an rtf file
            var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
            println(text)


            The output was:



            {rtf1ansiansicpg1252cocoartf1343cocoasubrtf140
            {fonttblf0fswissfcharset0 Helvetica;}
            {colortbl;red255green255blue255;}
            margl1440margr1440vieww10800viewh8400viewkind0
            pardtx720tx1440tx2160tx2880tx3600tx4320tx5040tx5760tx6480tx7200tx7920tx8640pardirnatural



            f0fs24 cf0 hello there}



            So the "hello there" is embedded. This is the problem with all the layout information in a .rtf file.



            I went back to TextEdit and created a true .txt file. After opening a file - Format|Make Plain Text



            Now this same code gave the console output "hello there".



            Hope this helps.






            share|improve this answer























            • Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
              – alttag
              Oct 24 '14 at 13:04










            • Okay, glad you got your problem solved!
              – Steve Rosenberg
              Oct 24 '14 at 14:27















            up vote
            6
            down vote













            I have seen this problem with .txt files created from .rtf files using TextEdit.



            I loaded a text.txt file in the resources folder of my playground using similar code to you. The file contents was "hello there" and was made by converting an .rtf file to .txt by changing the extension.



            let path = NSBundle.mainBundle().pathForResource("text", ofType: "txt")//or rtf for an rtf file
            var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
            println(text)


            The output was:



            {rtf1ansiansicpg1252cocoartf1343cocoasubrtf140
            {fonttblf0fswissfcharset0 Helvetica;}
            {colortbl;red255green255blue255;}
            margl1440margr1440vieww10800viewh8400viewkind0
            pardtx720tx1440tx2160tx2880tx3600tx4320tx5040tx5760tx6480tx7200tx7920tx8640pardirnatural



            f0fs24 cf0 hello there}



            So the "hello there" is embedded. This is the problem with all the layout information in a .rtf file.



            I went back to TextEdit and created a true .txt file. After opening a file - Format|Make Plain Text



            Now this same code gave the console output "hello there".



            Hope this helps.






            share|improve this answer























            • Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
              – alttag
              Oct 24 '14 at 13:04










            • Okay, glad you got your problem solved!
              – Steve Rosenberg
              Oct 24 '14 at 14:27













            up vote
            6
            down vote










            up vote
            6
            down vote









            I have seen this problem with .txt files created from .rtf files using TextEdit.



            I loaded a text.txt file in the resources folder of my playground using similar code to you. The file contents was "hello there" and was made by converting an .rtf file to .txt by changing the extension.



            let path = NSBundle.mainBundle().pathForResource("text", ofType: "txt")//or rtf for an rtf file
            var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
            println(text)


            The output was:



            {rtf1ansiansicpg1252cocoartf1343cocoasubrtf140
            {fonttblf0fswissfcharset0 Helvetica;}
            {colortbl;red255green255blue255;}
            margl1440margr1440vieww10800viewh8400viewkind0
            pardtx720tx1440tx2160tx2880tx3600tx4320tx5040tx5760tx6480tx7200tx7920tx8640pardirnatural



            f0fs24 cf0 hello there}



            So the "hello there" is embedded. This is the problem with all the layout information in a .rtf file.



            I went back to TextEdit and created a true .txt file. After opening a file - Format|Make Plain Text



            Now this same code gave the console output "hello there".



            Hope this helps.






            share|improve this answer














            I have seen this problem with .txt files created from .rtf files using TextEdit.



            I loaded a text.txt file in the resources folder of my playground using similar code to you. The file contents was "hello there" and was made by converting an .rtf file to .txt by changing the extension.



            let path = NSBundle.mainBundle().pathForResource("text", ofType: "txt")//or rtf for an rtf file
            var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
            println(text)


            The output was:



            {rtf1ansiansicpg1252cocoartf1343cocoasubrtf140
            {fonttblf0fswissfcharset0 Helvetica;}
            {colortbl;red255green255blue255;}
            margl1440margr1440vieww10800viewh8400viewkind0
            pardtx720tx1440tx2160tx2880tx3600tx4320tx5040tx5760tx6480tx7200tx7920tx8640pardirnatural



            f0fs24 cf0 hello there}



            So the "hello there" is embedded. This is the problem with all the layout information in a .rtf file.



            I went back to TextEdit and created a true .txt file. After opening a file - Format|Make Plain Text



            Now this same code gave the console output "hello there".



            Hope this helps.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 24 '14 at 1:37

























            answered Oct 24 '14 at 1:28









            Steve Rosenberg

            14.5k63948




            14.5k63948












            • Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
              – alttag
              Oct 24 '14 at 13:04










            • Okay, glad you got your problem solved!
              – Steve Rosenberg
              Oct 24 '14 at 14:27


















            • Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
              – alttag
              Oct 24 '14 at 13:04










            • Okay, glad you got your problem solved!
              – Steve Rosenberg
              Oct 24 '14 at 14:27
















            Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
            – alttag
            Oct 24 '14 at 13:04




            Useful information, but clearly this question does not suffer from the RTF issue. And as stupid as I feel about the mistake I made, I am certain I wouldn't have made this one!
            – alttag
            Oct 24 '14 at 13:04












            Okay, glad you got your problem solved!
            – Steve Rosenberg
            Oct 24 '14 at 14:27




            Okay, glad you got your problem solved!
            – Steve Rosenberg
            Oct 24 '14 at 14:27


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26538375%2fread-file-in-swift-ios-playground%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]