NSString containsString crashes

If you want your code to work on iOS 7 as well as iOS 8 you should use one of the rangeOfString calls instead. Basically if the range returned has a length of zero, the substring is not there. /* These methods return length==0 if the target string is not found. So, to check for … Read more

UILabel – string as text and links

You can add custom actions to any of the available UILabel replacements that support links using a fake URL scheme that you’ll intercept later: TTTAttributedLabel *tttLabel = <# create the label here #>; NSString *labelText = @”Lost? Learn more.”; tttLabel.text = labelText; NSRange r = [labelText rangeOfString:@”Learn more”]; [tttLabel addLinkToURL:[NSURL URLWithString:@”action://show-help”] withRange:r]; Then, in your … Read more

How to add commas to number every 3 digits in Objective C?

Use NSNumberFormatter. Specifically: NSNumberFormatter *formatter = [NSNumberFormatter new]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important! NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithInteger:2000000]]; [formatter release]; By default NSNumberFormatter uses the current locale so the grouping separators are set to their correct values by default. The key thing is to remember to set a number style.

Strip Non-Alphanumeric Characters from an NSString

We can do this by splitting and then joining. Requires OS X 10.5+ for the componentsSeparatedByCharactersInSet: NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; NSString *strippedReplacement = [[someString componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@””];