Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've got string like this: {{0,168},{20,20}}, here indicates two points. I want to get them out to initialize my CCPoint. Do you guys know how to convert this?

share|improve this question
1  
If the string is in json format, or you can change to it, use a parser: jsoncpp.sourceforge.net –  nosferat Jul 16 '13 at 9:23

1 Answer 1

An easy fix , if you have string in that specific format:

CGRect rect = CGRectFromString(@"{{0,168},{20,20}}");
CGPoint point1 = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint point2 = CGPointMake(rect.size.width, rect.size.height);

If you have more and you want to get them use this:

NSString *string = @"{{0,168},{20,20},{20,20},{20,20},{20,20}}";
NSRange range;
range.length = string.length - 1;
range.location = 1;
//because you have commas everywhere you need to change it to a different separator
NSString *modString = [[string substringWithRange:range] stringByReplacingOccurrencesOfString:@"},{" withString:@"}#{"];

NSArray *pointList = [modString componentsSeparatedByString:@"#"];

for (NSString *str in pointList)
{
    CGPoint point = CGPointFromString(str);
    NSLog(@"%@",NSStringFromCGPoint(point));
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.