Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3598382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:14:07+00:00 2026-05-18T20:14:07+00:00

(This question has been rewritten from an issue with NSTextView following some further research)

  • 0

(This question has been rewritten from an issue with NSTextView following some further research)

UPDATE: You can download a very basic project that displays the issue here:

http://w3style.co.uk/~d11wtq/DocumentApp.tar.gz

(Do a grep -c "\r" file.txt on the file you save to get a line count where \r occurs… repeat for \n).

I’ve realised all files created by NSDocument have \r is line endings, not the standard \n, even though the NSData my document subclass returns does not contain \r, it only contains \n. Is there a way to configure this?

I thought Macs used UNIX line endings these days, so it seems weird that AppKit is still using the antiquated Mac endings. Weirder is that NSDocument asks for NSData, then rather unkindly corrupts that NSData by transforming the line endings.

The switch to \r is happening after producing NSData, so NSDocument itself is doing some replacements on the bytes:

 const char *bytes = [data bytes];
 int i, len;
 for (i = 0, len = [data length]; i < len; ++i) {
  NSLog(@"byte %d = %02x", i, bytes[i]);
 }

Outputs (note 0a is the hex value of \n):

> 2010-12-17 12:45:59.076
> MojiBaker[74929:a0f] byte 0 = 66
> 2010-12-17 12:45:59.076
> MojiBaker[74929:a0f] byte 1 = 6f
> 2010-12-17 12:45:59.076
> MojiBaker[74929:a0f] byte 2 = 6f
> 2010-12-17 12:45:59.077
> MojiBaker[74929:a0f] byte 3 = 0a
> 2010-12-17 12:45:59.077
> MojiBaker[74929:a0f] byte 4 = 62
> 2010-12-17 12:45:59.077
> MojiBaker[74929:a0f] byte 5 = 61
> 2010-12-17 12:45:59.077
> MojiBaker[74929:a0f] byte 6 = 72
> 2010-12-17 12:45:59.077
> MojiBaker[74929:a0f] byte 7 = 0a

If NSDocument is going to ask for NSData then it should respect that and not modify it.

Here’s the full code from the method: -dataOfType:error: method in my document:

    -(NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
 NSString *string = [textView string];

 // DEBUG CODE...
 NSArray *unixLines = [string componentsSeparatedByString:@"\n"];
 NSArray *windowsLines = [string componentsSeparatedByString:@"\r\n"];
 NSArray *macLines = [string componentsSeparatedByString:@"\r"];

 NSLog(@"TextView has %d LF, %d CRLF, %d CR", [unixLines count] - 1, [windowsLines count] - 1, [macLines count] - 1);

 NSData *data = [NSData dataWithBytes:[string cStringUsingEncoding:NSUTF8StringEncoding]
          length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

 const char *bytes = [data bytes];
 int i, len;
 for (i = 0, len = [data length]; i < len; ++i) {
  NSLog(@"byte %d = %02x", i, bytes[i]);
 }

 if (data != nil) {
  [textView breakUndoCoalescing];
 }

 return data;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:14:07+00:00Added an answer on May 18, 2026 at 8:14 pm

    NSDocument doesn’t care about line termination; it’s a semi-abstract class, designed to be subclassed. By itself it imposes nothing on a file format.

    It’s the particular implementation of an NSDocument subclass – one that happens to read and write plain text – that will care about line termination characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's quite possible a question like this has been asked before, but I can't
This question has been asked before ( link ) but I have slightly different
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),
I know this question has been asked before, but I ran into a problem.
I know this question has been asked a bit before. But looking around I
Being new to test based development, this question has been bugging me. How much
If this question has already been asked I appologies, please point me in the
I know this specific question has been asked before , but I am not
Preamble So, this question has already been answered, but as it was my first
I saw this same question for VIM and it has been something that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.