开发者

Is there a prebuilt method that auto-increments a string?

开发者 https://www.devze.com 2023-04-06 13:09 出处:网络
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 \"Untitle开发者_运维知识库d-2\", etc

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 "Untitle开发者_运维知识库d-2", etc.


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:

[NSString stringWithFormat:@"Untitled-%lu", thisIndex]

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."

0

精彩评论

暂无评论...
验证码 换一张
取 消