Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That's a great question. I think <em>@Diederik</em> has a good answer, although it's unfortunate that Cocoa doesn't have a mechanism for exactly what you want to do.</p> <p><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSInputStream_Class/" rel="nofollow noreferrer"><code>NSInputStream</code></a> allows you to read chunks of N bytes (very similar to <code>java.io.BufferedReader</code>), but you have to convert it to an <code>NSString</code> on your own, then scan for newlines (or whatever other delimiter) and save any remaining characters for the next read, or read more characters if a newline hasn't been read yet. (<a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFileHandle_Class/" rel="nofollow noreferrer"><code>NSFileHandle</code></a> lets you read an <code>NSData</code> which you can then convert to an <code>NSString</code>, but it's essentially the same process.)</p> <p>Apple has a <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/Streams/Streams.html" rel="nofollow noreferrer">Stream Programming Guide</a> that can help fill in the details, and <a href="https://stackoverflow.com/questions/1002652/">this SO question</a> may help as well if you're going to be dealing with <code>uint8_t*</code> buffers.</p> <p>If you're going to be reading strings like this frequently (especially in different parts of your program) it would be a good idea to encapsulate this behavior in a class that can handle the details for you, or even subclassing <code>NSInputStream</code> (it's <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSInputStream_Class/Reference/Reference.html#//apple_ref/doc/uid/20001982-64356" rel="nofollow noreferrer">designed to be subclassed</a>) and adding methods that allow you to read exactly what you want.</p> <p>For the record, I think this would be a nice feature to add, and I'll be filing an enhancement request for something that makes this possible. :-)</p> <hr> <p><strong>Edit:</strong> Turns out this request already exists. There's a Radar dating from 2006 for this (rdar://4742914 for Apple-internal people).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload