General

  • 'join' an array of strings into a single string
    1. NSArray *chunks = ... get an array, say by splitting it;
    2. string = [chunks componentsJoinedByString: @" : | "];
    NSArray *chunks = ... get an array, say by splitting it;
    string = [chunks componentsJoinedByString: @" : | "];

    would produce something like

    oop : | ack :- | bork :- | greeble :- | ponies
  • 'split' a string into an array
    1. NSString *string = @"oop:ack:bork:greeble:ponies";
    2. NSArray *chunks = [string componentsSeparatedByString: @":"];
    NSString *string = @"oop:ack:bork:greeble:ponies";
    NSArray *chunks = [string componentsSeparatedByString: @":"];
  • Converting string to an integer
    1. NSString *string = ...;
    2. int value = [string intValue];
    NSString *string = ...;
    int value = [string intValue];

    Similarly, there is a floatValue and doubleValue NSString methods.

  • Counting number of words in a string
    1. - (NSUInteger) countWords: (NSString *) string {
    2. NSScanner *scanner = [NSScanner scannerWithString: string];
    3. NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    4. NSUInteger count = 0; while ([scanner scanUpToCharactersFromSet: whiteSpace intoString: nil]) { count++; } return count;
    5. } // countWords
    - (NSUInteger) countWords: (NSString *) string {
    NSScanner *scanner = [NSScanner scannerWithString: string];
    NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSUInteger count = 0; while ([scanner scanUpToCharactersFromSet: whiteSpace intoString: nil]) { count++; } return count;
    } // countWords
  • Iterating attributes in an attributed string This prints out each of the attribute runs from an attributed string:
    1. NSAttributedString *string = ...;
    2. NSRange totalRange = NSMakeRange (0, string.length);
    3. [string enumerateAttributesInRange: totalRange options: 0 usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop)
    4. { NSLog (@"range: %@ attributes: %@", NSStringFromRange(range), attributes); }];
    NSAttributedString *string = ...;
    NSRange totalRange = NSMakeRange (0, string.length);
    [string enumerateAttributesInRange: totalRange options: 0 usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop)
    { NSLog (@"range: %@ attributes: %@", NSStringFromRange(range), attributes); }];

    and if you're in 10.5 or earlier:

    1. - (void) iterateAttributesForString: (NSAttributedString *) string {
    2. NSDictionary *attributeDict;
    3. NSRange effectiveRange = { 0, 0 }; do { NSRange range;
    4. range = NSMakeRange (NSMaxRange(effectiveRange), [string length] - NSMaxRange(effectiveRange));
    5. attributeDict = [string attributesAtIndex: range.location longestEffectiveRange: &
    6. effectiveRange inRange: range];
    7. NSLog (@"Range: %@ Attributes: %@", NSStringFromRange(effectiveRange), attributeDict); }
    8. while (NSMaxRange(effectiveRange) < [string length]); } // iterateAttributesForString
    - (void) iterateAttributesForString: (NSAttributedString *) string {
    NSDictionary *attributeDict;
    NSRange effectiveRange = { 0, 0 }; do { NSRange range;
    range = NSMakeRange (NSMaxRange(effectiveRange), [string length] - NSMaxRange(effectiveRange));
    attributeDict = [string attributesAtIndex: range.location longestEffectiveRange: &amp;
    effectiveRange inRange: range];
    NSLog (@"Range: %@ Attributes: %@", NSStringFromRange(effectiveRange), attributeDict); }
    while (NSMaxRange(effectiveRange) < [string length]); } // iterateAttributesForString
  • Making localizable strings You will need a file named Localizable.strings that lives in your English.lproj directory (or whatever localization directory is appropriate). It has this syntax:
     "BorkDown" = "BorkDown";
    "Start Timer" = "Start Timer";
    "Stop Timer" = "Stop Timer";

    That is, a key followed by a localized value.In your code, you can then use NSLocalizedString() or one of its variants:

    1. [statusItem setTitle: NSLocalizedString(@"BorkDown", nil)];
    [statusItem setTitle: NSLocalizedString(@"BorkDown", nil)];

    The second argument is ignored by the function. Obstensively it is a /* comment */ in the strings file so that you can match the key back to what it is supposed to actually be.

  • NSLog without the extra crud NSlog puts too much crud in front of the logging line. For a foundation tool that output stuff, it gets in the way. I'd still like for a replacement to expand %@, which the printf() family won't do. Here's a some code that'll do that.
    1. #include <stdarg.h> void LogIt (NSString *format, ...) {
    2. va_list args;
    3. va_start (args, format);
    4. NSString *string;
    5. string = [[NSString alloc] initWithFormat: format arguments: args];
    6. va_end (args);
    7. fprintf (stderr, "%s\n", [string UTF8String]);
    8.  [string release];
    9. } // LogIt
    #include <stdarg.h> void LogIt (NSString *format, ...) {
    va_list args;
    va_start (args, format);
    NSString *string;
    string = [[NSString alloc] initWithFormat: format arguments: args];
    va_end (args);
    fprintf (stderr, "%s\n", [string UTF8String]);
     [string release];
    } // LogIt
  • Putting an image into an attributed string You'll need to use a text attachment.
    1. - (NSAttributedString *) prettyName {
    2. NSTextAttachment *attachment;
    3. attachment = [[[NSTextAttachment alloc] init] autorelease];
    4. NSCell *cell = [attachment attachmentCell];
    5. NSImage *icon = [self icon]; // or wherever you are getting your image
    6. [cell setImage: icon];
    7. NSString *name = [self name]; NSAttributedString *attrname; attrname = [[NSAttributedString alloc] initWithString: name];
    8. NSMutableAttributedString *prettyName; prettyName = (id)[NSMutableAttributedString attributedStringWithAttachment: attachment]; // cast to quiet compiler warning
    9. [prettyName appendAttributedString: attrname];
    10. return (prettyName); } // prettyName
    - (NSAttributedString *) prettyName {
    NSTextAttachment *attachment;
    attachment = [[[NSTextAttachment alloc] init] autorelease];
    NSCell *cell = [attachment attachmentCell];
    NSImage *icon = [self icon]; // or wherever you are getting your image
    [cell setImage: icon];
    NSString *name = [self name]; NSAttributedString *attrname; attrname = [[NSAttributedString alloc] initWithString: name];
    NSMutableAttributedString *prettyName; prettyName = (id)[NSMutableAttributedString attributedStringWithAttachment: attachment]; // cast to quiet compiler warning
    [prettyName appendAttributedString: attrname];
    return (prettyName); } // prettyName

    This puts the image at the front of the string. To put the image in the middle of the string, you'll need to create an attributedstring with attachment, and then append that to your final attributed string.

  • Stripping out newlines from a string So you have an NSString and want to yank out the newlines. You can do a splitand join, like in scripting languages, or you can make a mutable copy and manipulate that:
    1. NSMutableString *mstring = [NSMutableString stringWithString:string];
    2. NSRange wholeShebang = NSMakeRange(0, [mstring length]);
    3. [mstring replaceOccurrencesOfString: @" " withString: @"" options: 0 range: wholeShebang]; return [NSString stringWithString: mstring];
    NSMutableString *mstring = [NSMutableString stringWithString:string];
    NSRange wholeShebang = NSMakeRange(0, [mstring length]);
    [mstring replaceOccurrencesOfString: @" " withString: @"" options: 0 range: wholeShebang]; return [NSString stringWithString: mstring];

    (this can also be used for generic string manipulations, not just stripping out newlines).This technique takes half the time (at least) of split/join. But probably not enough to make an impact. In a simple test, split/join took 0.124 seconds to strip 36909 newlines in a 1.5 meg textfile, and 0.071 seconds to do the same.

  • Substring matching
    1.  NSRange range = [[string name] rangeOfString: otherString options: NSCaseInsensitiveSearch];
     NSRange range = [[string name] rangeOfString: otherString options: NSCaseInsensitiveSearch];
  • Today's date as a string The general solution for converting a date to a string is NSDateFormatter. Sometimes you need to generate a date string in a particular format easily. For instance if you need "December 4, 2007", you can use:
    1. [[NSDate date] descriptionWithCalendarFormat: @"%B %e, %Y" timeZone: nil locale: nil]
    [[NSDate date] descriptionWithCalendarFormat: @"%B %e, %Y" timeZone: nil locale: nil]

    (Thanks to Mike Morton for this one)
    (Also Mac OS X only)

  • Trimming whitespace from ends of a string
    1. NSString *ook = @"\n \t\t hello there \t\n \n\n";
    2. NSString *trimmed =
    3. [ook stringByTrimmingCharactersInSet:
    4. [NSCharacterSet whitespaceAndNewlineCharacterSet]];NSLog(@"trimmed: '%@'", trimmed);
    NSString *ook = @"\n \t\t hello there \t\n \n\n";
    NSString *trimmed =
    [ook stringByTrimmingCharactersInSet:
    [NSCharacterSet whitespaceAndNewlineCharacterSet]];NSLog(@"trimmed: '%@'", trimmed);

    produces

    2009-12-24 18:24:42.431 trim[6799:903] trimmed: 'hello there'

     

Graphics

  • Draw a string in bold
    1. - (void) drawLabel: (NSString *) label
    2. atPoint: (NSPoint) point
    3. bold: (BOOL) bold {
    4. NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    5. NSFont *currentFont = [NSFont userFontOfSize: 14.0];if (bold) {
    6. NSFontManager *fm = [NSFontManager sharedFontManager];
    7. NSFont *boldFont = [fm convertFont: currentFont
    8. toHaveTrait: NSBoldFontMask];
    9. [attributes setObject: boldFont
    10. forKey: NSFontAttributeName];
    11. } else {
    12. [attributes setObject: currentFont
    13. forKey: NSFontAttributeName];
    14. }[label drawAtPoint: point withAttributes: attributes];;} // drawLabel
    - (void) drawLabel: (NSString *) label
    atPoint: (NSPoint) point
    bold: (BOOL) bold {
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    NSFont *currentFont = [NSFont userFontOfSize: 14.0];if (bold) {
    NSFontManager *fm = [NSFontManager sharedFontManager];
    NSFont *boldFont = [fm convertFont: currentFont
    toHaveTrait: NSBoldFontMask];
    [attributes setObject: boldFont
    forKey: NSFontAttributeName];
    } else {
    [attributes setObject: currentFont
    forKey: NSFontAttributeName];
    }[label drawAtPoint: point withAttributes: attributes];;} // drawLabel

NSArray

  • Processing every line in a string
    Say you got a string that's composed of one interesting data item per line, like /usr/share/dict/words or from a script, and you want to process them. In this case turning a bunch of strings into floats:

    1. NSMutableArray *altitudes = [NSMutableArray array];
    2. NSString *altitudeString = [self altitudeStringFromGoogle: coords];[altitudeString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
    3. float value = [line floatValue];
    4. [altitudes addObject: [NSNumber numberWithFloat: value]];
    5. }];
    NSMutableArray *altitudes = [NSMutableArray array];
    NSString *altitudeString = [self altitudeStringFromGoogle: coords];[altitudeString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
    float value = [line floatValue];
    [altitudes addObject: [NSNumber numberWithFloat: value]];
    }];

Random

  • Put a string on the pasteboard
    Here's a category for easily putting a string on the paste|clipboard:

    1. @implementation NSString (PasteboardGoodies)- (void) sendToPasteboard
    2. {
    3. [[NSPasteboard generalPasteboard]
    4. declareTypes: [NSArray arrayWithObject: NSStringPboardType]
    5. owner:nil];
    6. [[NSPasteboard generalPasteboard]
    7. setString: self
    8. forType: NSStringPboardType];
    9. } // sendToPasteboard
    @implementation NSString (PasteboardGoodies)- (void) sendToPasteboard
    {
    [[NSPasteboard generalPasteboard]
    declareTypes: [NSArray arrayWithObject: NSStringPboardType]
    owner:nil];
    [[NSPasteboard generalPasteboard]
    setString: self
    forType: NSStringPboardType];
    } // sendToPasteboard
    1. @end // PasteboardGoodies
    @end // PasteboardGoodies