Skip to content

Instantly share code, notes, and snippets.

@steipete
Last active December 21, 2017 17:52
Show Gist options
  • Save steipete/f436d8eaeb1e896131ed to your computer and use it in GitHub Desktop.
Save steipete/f436d8eaeb1e896131ed to your computer and use it in GitHub Desktop.
NSFormattingContextDynamic got my attention, so I've digged into how this actually works. (tl;dr: _NSStringProxyForContext!)
// NSFormattingContext is new in iOS 8. For NSFormattingContextDynamic it promises is following:
// The capitalization context is determined dynamically from the set {NSFormattingContextStandalone, NSFormattingContextBeginningOfSentence, NSFormattingContextMiddleOfSentence}. For example, if a date is placed at the beginning of a sentence, NSFormattingContextBeginningOfSentence is used to format the string automatically. When this context is used, the formatter will return a string proxy that works like a normal string in most cases. After returning from the formatter, the string in the string proxy is formatted by using NSFormattingContextUnknown. When the string proxy is used in stringWithFormat:, we can determine where the %@ is and then set the context accordingly. With the new context, the string in the string proxy will be formatted again and be put into the final string returned from stringWithFormat:.
// Here's a simple example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.formattingContext = NSFormattingContextDynamic;
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSString *test = [NSString stringWithFormat:@"XXX %@", dateString];
(lldb) po test
XXX June 19, 2014
(lldb) po dateString
June 19, 2014
(lldb) po [dateString class]
_NSStringProxyForContext
(lldb) po [dateString _shortMethodDescription]
<_NSStringProxyForContext: 0x1117288f0>:
in _NSStringProxyForContext:
Properties:
@property (copy) NSString* string; (@synthesize string = _string;)
@property (copy) NSFormatter* formatter; (@synthesize formatter = _formatter;)
@property (copy) id item; (@synthesize item = _item;)
Instance Methods:
- (void) setItem:(id)arg1; (0x10279e1f4)
- (void) _retainFormatter:(id)arg1; (0x10279dcbe)
- (id) formatter; (0x10279e1b7)
- (id) item; (0x10279e1de)
- (void) setFormatter:(id)arg1; (0x10279e1cd)
- (void) encodeWithCoder:(id)arg1; (0x10279e0ab)
- (id) initWithCoder:(id)arg1; (0x10279df94)
- (id) copyWithZone:(_NSZone*)arg1; (0x10279deee)
- (void) dealloc; (0x10279dbde)
- (id) string; (0x10279e190)
- (unsigned short) characterAtIndex:(unsigned long)arg1; (0x10279dc84)
- (void) getCharacters:(unsigned short*)arg1 range:(_NSRange)arg2; (0x10279dca1)
- (void) setString:(id)arg1; (0x10279e1a6)
- (id) _dynamicContextEvaluation:(id)arg1 patternString:(id)arg2; (0x10279dd0a)
- (unsigned long) length; (0x10279dc67)
(NSString ...)
(lldb) po [dateString _ivarDescription]
<_NSStringProxyForContext: 0x1117288f0>:
in _NSStringProxyForContext:
_string (NSString*): @"June 19, 2014"<__NSCFString: 0x111728850>
_formatter (NSFormatter*): <NSDateFormatter: 0x111728950>
_item (<NSCopying><NSSecureCoding><NSObject>*): <__NSDate: 0x111727c00>
in NSString:
in NSObject:
isa (Class): _NSStringProxyForContext (isa, 0x10290a5e8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment