• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTextView - read line by line?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextView - read line by line?


  • Subject: Re: NSTextView - read line by line?
  • From: Greg Titus <email@hidden>
  • Date: Wed, 16 Apr 2003 14:01:31 -0700

On Wednesday, April 16, 2003, at 01:38 PM, John C. Randolph wrote:

On Wednesday, April 16, 2003, at 01:15 PM, Steve Woodward wrote:

I've been RTFMing but have not discovered the right/best way to read text that's in a TextView line by line. What I am looking to do is get a list of items that the user types in and process that list one item at a time. Is there another more appropriate control for this purpose? Many thanks in advance!

NSString *thisLine, *allTheText = [myTextView string];
NSEnumerator *lines = [allTheText componentsSeparatedByString:@"\n"];

John meant to do this, not the previous line:

NSEnumerator *lines = [[allTheText componentsSeparatedByString:@"\n"] objectEnumerator];


while (thisLine = [lines nextObject])
[self doSomethingWithLine:thisLine];

This will be fine if the user is typing, but will fail if the user copies and pastes text in from an application that is using classic Mac OS line endings. (Like BBEdit can, for instance.) If you want your code to work correctly with all of Unix/Mac/Windows/Unicode line ending characters, you need to do something more complex:

NSString *thisLine, *allTheText = [myTextView string];
int length = [allTheText length];
NSRange aRange = NSMakeRange(0, 1);

while (aRange.location < length) {
unsigned int start, end, contentEnd;

[allTheText getLineStart:&start end:&end contentsEnd:&contentEnd forRange:aRange];

if (contentEnd > start)
[self doSomethingWithLine:[allTheText substringWithRange:NSMakeRange(start, contentEnd - start)];
aRange.location = end;
}

Hope this helps,
- Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: NSTextView - read line by line?
      • From: "John C. Randolph" <email@hidden>
References: 
 >Re: NSTextView - read line by line? (From: "John C. Randolph" <email@hidden>)

  • Prev by Date: Re: Programatically waking from sleep
  • Next by Date: Re: Inconsistent Memory Management Rules
  • Previous by thread: Re: NSTextView - read line by line?
  • Next by thread: Re: NSTextView - read line by line?
  • Index(es):
    • Date
    • Thread