当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - 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.

网友答案:

Create NSData from NSString.

Then create NSInputStream from NSData

NSInputStream* is = [NSInputStream inputStreamWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
分享给朋友:
您可能感兴趣的文章:
随机阅读: