I’m trying to have a way of auto-incrementing a NSString.
For example, if I have a string “Untitled”, I want the incremented version to be “Untitled-1”, then “Untitled-2”, etc.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, there is no built in method for doing this.
You could write your own method to handle the general-purpose case you’re describing, but in practice it probably makes more sense to generate these “unique” string names with outside knowledge of what the index represents. For example, if you’re trying to unique a file name, you might start with @”Untitled” and, if the file exists, proceed to try testing for existing files with a file named:
It’s probably more trouble than it’s worth to build an incrementer method that takes pains to figure out which portion of an arbitrary string is the part that should be “incremented.”