0

我将两个 CGPoints(在一个数组中)保存到 Documents 目录中的一个文本文件中。当我阅读文件时,这是我得到的:

NSPoint: {66.5, 87}, NSPoint: {198.5, 314.5}

如何获得这样的输出:

66.5 87
198.5 314.5

这是我的代码:

//save the coordinates of two points into a text file in the Documents directory
- (void)savePoints
{
    //get Document directory's path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"POINTS.txt"];

    //create an points array
    NSMutableArray *points = [[NSMutableArray alloc] init];
    [points addObject:[NSValue valueWithBytes:&fromPoint objCType:@encode(CGPoint)]]; //insert object at the end of the array
    [points addObject:[NSValue valueWithBytes:&toPoint objCType:@encode(CGPoint)]];

    //write points array to file
    BOOL result = [NSKeyedArchiver archiveRootObject:points toFile:fullPath];
    NSLog(@"Archival result: %d", result);
}

//read the file
-(void)getPoints
{
    //get Document directory's path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"POINTS.txt"];

    //read points array from file
    NSMutableArray *points = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSString *str = [points componentsJoinedByString:@","];
    NSLog(@"%@", str);
}
4

5 回答 5

3

You have a line of code that takes your array of two points and then converts it to a string:

NSString *str = [points componentsJoinedByString:@","];

This gives you your:

NSPoint: {66.5, 87},NSPoint: {198.5, 314.5}

You happen to be doing this in the getPoints method, but you would have gotten the same result if you had done this in the savePoints method, too. Your problem has nothing to do with the saving to the file or retrieving from the file. It's just a matter of how you want to extract the CGPoint values from your points array.

So, if you wanted to get your two points back out (which I presume is the real intent), you would do something like:

CGPoint fromPoint = [[points objectAtIndex:0] CGPointValue];
CGPoint toPoint = [[points objectAtIndex:1] CGPointValue];

Or you could, using recent versions of the compiler, do:

CGPoint fromPoint = [points[0] CGPointValue];
CGPoint toPoint = [points[1] CGPointValue];

Or you could do something like:

for (NSValue *value in points)
{
    CGPoint point = [value CGPointValue];
    NSLog(@"point = %.1f, %.1f", point.x, point.y);
}

Then you can do whatever you want with your two points.

于 2013-04-29T12:40:18.753 回答
2

好的,您得到了答案,但为了方便发布此答案,请尝试使用NSStringFromCGPointCGPointFromString

//save the coordinates of two points into a text file in the Documents directory
- (void)savePoints
{
    //get Document directory's path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"POINTS.txt"];

    CGPoint fromPoint = CGPointMake(66.5, 87);
    CGPoint toPoint = CGPointMake(198.5, 314.5);

    //create an points array
    NSMutableArray *points = [[NSMutableArray alloc] init];
    [points addObject:NSStringFromCGPoint(fromPoint)]; //Create string from CGPoint
    [points addObject:NSStringFromCGPoint(toPoint)]; //Create string from CGPoint

    //write points array to file
    BOOL result = [NSKeyedArchiver archiveRootObject:points toFile:fullPath];
    NSLog(@"Archival result: %d", result);
}

//read the file
-(void)getPoints
{
    //get Document directory's path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"POINTS.txt"];

    //read points array from file
    NSMutableArray *points = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    for (NSString *point in points) {
        CGPoint tmpPoint  = CGPointFromString(point); //Create CGPoint from NSString
        NSLog(@"%f %f",tmpPoint.x,tmpPoint.y);
    }
}
于 2013-04-29T12:58:22.923 回答
2

尝试这个

     NSString *strCG=@"{66.5, 87}";
     CGPoint myPoint = CGPointFromString([NSString stringWithFormat:@"%@",strCG]);//StrCG is your Strin
     ScrollObj.contentOffset=myPoint;
于 2013-04-29T12:43:21.597 回答
1

试试这样可能对你有帮助

NSString * val = @"NSPoint: {66.5, 87}, NSPoint: {198.5, 314.5}";
NSString * strippedNumber = [val stringByReplacingOccurrencesOfString:@"[^.,0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [val length])];
    NSLog(@"%@", strippedNumber);
于 2013-04-29T12:28:06.630 回答
1

Xcode文档窗口中,搜索“ NSValue UIKitAdditions Reference”(或谷歌搜索)。

您可以直接encode将其与CGPoint (+ (NSValue *)valueWithCGPoint:(CGPoint)point)) 。decode(- (CGPoint)CGPointValue

于 2013-04-29T12:28:47.837 回答