NSData base64Encoding vs. base64EncodedStringWithOptions


Alex Zavatone
 

Xcode 9.2, iOS 11.2, Objective-C


Looking at the header for NSData, and base64EncodedStringWithOptions: NSDataBase64EncodingOptions,

the options are these starting on line 50 of NSData.h:

typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
    // Use zero or one of the following to control the maximum line length after which a line ending is inserted. No line endings are inserted by default.
    NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
    NSDataBase64Encoding76CharacterLineLength = 1UL << 1,

    // Use zero or more of the following to specify which kind of line ending is inserted. The default line ending is CR LF.
    NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
    NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,

}

Questions

- What did the deprecated base64Encoding do with the line endings?  How long were lines and what happened at a line ending?

The typedef for NSDataBase64EncodingOptions, 0 is given the text, NSDataBase64Encoding64CharacterLineLength, indicating that a line is 64 characters.  

The documentation for this states that “NSDataBase64Encoding64CharacterLineLength Set the maximum line length to 64 characters, after which a line ending is inserted.”.

The comment on line 52 for NSDataBase64Encoding64CharacterLineLength states something different.     // Use zero or one of the following to control the maximum line length after which a line ending is inserted. No line endings are inserted by default. 

It states that a line ending is inserted and no line endings are inserted by default.

- Which is the default value, 0?  nil?   I’d expect that it is 0, but the documentation for 0 states that a line ending is inserted and that no line endings are inserted by default.  If 0 is the default, how is this possible?

- What happens for NSDataBase64Encoding76CharacterLineLength?

Can someone please clarify what Apple’s confusing documentation doesn’t?

Thank you,

Alex Zavatone

Join cocoa@apple-dev.groups.io to automatically receive all group messages.