Saturday, January 11, 2014

Find and Replace question NSArray

An NSArray arr = {@ "aa", @ "bb" @ "cc", "dd", @ "aa", @ "dd", @ "cc", @ "aa"};

Want to convert NSArray arr = {@ "aa", @ "bb" @ "cc", "dd", @ "aa1", @ "dd1", @ "cc1", @ "aa2"};

Is repeated to add the next int type distinction, God horse good way?
Reply:
Only loop handle it
Reply:
cited a floor ultrapro reply:
circulation only deal with it


Judgment is that in the middle of a little do not know how to write
Reply:
I recommend you a way:
 
# Define format (fmtString, args ...) [NSString stringWithFormat: fmtString, args]
NSArray * arr = @ [@ "aa", @ "bb", @ "cc", @ "dd", @ "aa", @ "dd", @ "cc", @ "aa"];

NSMutableDictionary * dict = [NSMutableDictionary dictionary];
for (NSString * value in arr)
{
[Dict setObject: @ "-1" forKey: value];
}

NSMutableArray * finalArray = [NSMutableArray array];
for (NSString * value in arr)
{
for (NSString * key in dict)
{
if ([value isEqualToString: key])
break;
}
dict [value] = format (@ "% d", [dict [value] intValue] +1);

? NSString * obj = (! [Dict [value] intValue]) format (@ "% @", value): format (@ "% @% d", value, [dict [value] intValue]);

[FinalArray addObject: obj];
}

NSLog (@ "% @", finalArray);

Reply:
reference to the third floor wangliangffaflgh reply:
I recommend you a way:
 
# Define format (fmtString, args ...) [NSString stringWithFormat: fmtString, args]
NSArray * arr = @ [@ "aa", @ "bb", @ "cc", @ "dd", @ "aa", @ "dd", @ "cc", @ "aa"];

NSMutableDictionary * dict = [NSMutableDictionary dictionary];
for (NSString * value in arr)
{
[Dict setObject: @ "-1" forKey: value];
}

NSMutableArray * finalArray = [NSMutableArray array];
for (NSString * value in arr)
{
for (NSString * key in dict)
{
if ([value isEqualToString: key])
break;
}
dict [value] = format (@ "% d", [dict [value] intValue] +1);

? NSString * obj = (! [Dict [value] intValue]) format (@ "% @", value): format (@ "% @% d", value, [dict [value] intValue]);

[FinalArray addObject: obj];
}

NSLog (@ "% @", finalArray);


Thank you very much for the object is not enough to grasp such an approach can not think of a good way

No comments:

Post a Comment