Search code examples
iphoneobjective-ctokenizensscanner

When to use NSScanner vs. componentsSeparatedByCharactersInSet: to tokenize NSString?


I need to tokenize many strings on an iPhone. Obviously, resources are at a minimum. I have been using componentsSeparatedByCharactersInSet: to tokenize my strings, but it is slow. Would it be better to use NSScanner? What, generally, are the guidelines one should follow when choosing to use one or the other to tokenize?


Solution

  • IMHO NSScanner is way more flexible than componentsSeparatedByCharactersInSet, so if you need more flexibility - use NSScanner. Regarding performance - you'd have to test for that. So if NSScanner is faster - it has flexibility AND performance advantage (although disadvantage on ease of use).