从大括号格式字符串初始化CCPoint

时间:2013-07-16 07:19:13

标签: string cocos2d-iphone

我有这样的字符串:{{0,168},{20,20}},这里表示两点。我想让他们初始化我的CCPoint。你们知道怎么转换吗?

1 个答案:

答案 0 :(得分:1)

如果你有特定格式的字符串,那么很容易解决:

    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);

如果你有更多,并且你想让他们使用它:

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));
}