objective-cnsstringnsinputstream

Loading an NSString to NSInputStream


In java I have something like this:

String data = "someFile.txt";
InputStream fin = Thread.currentThread().getContextClassLoader()
                                      .getResourceAsStream(data);

It basically initializes fin with the contents of a text file.

I am trying to convert this java code to objectiveC and I am really lost as how to do it for the second line i.e what is the equivalent of Thread.currentThread().getContextClassLoader().getResourceAsStream(data) in objectiveC ?

NOTE: I can't directly read the file onto the InputStream as the data is globally provided in a string variable.

I am new to objectiveC and sorry if this is trivial but any help would be much appreciated.


Solution

  • Create NSData from NSString.

    Then create NSInputStream from NSData

    NSInputStream* is = [NSInputStream inputStreamWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];