从文本文件中读取NSPoint

时间:2013-04-29 12:16:52

标签: iphone ios

我将两个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);
}

5 个答案:

答案 0 :(得分:3)

您有一行代码可以获取两个点的数组,然后将其转换为字符串:

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

这会给你:

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

您恰好在getPoints方法中执行此操作,但如果您已在savePoints方法中执行此操作,则会获得相同的结果。您的问题与保存到文件或从文件中检索无关。这只是您想要从CGPoint数组中提取points值的问题。

所以,如果你想让你的两点退出(我认为这是真正的意图),你会做类似的事情:

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

或者您可以使用最新版本的编译器执行:

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

或者你可以这样做:

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

然后你可以用你的两点做任何你想做的事。

答案 1 :(得分:2)

试试这个

     NSString *strCG=@"{66.5, 87}";
     CGPoint myPoint = CGPointFromString([NSString stringWithFormat:@"%@",strCG]);//StrCG is your Strin
     ScrollObj.contentOffset=myPoint;

答案 2 :(得分: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);
    }
}

答案 3 :(得分: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);

答案 4 :(得分:1)

Xcode文档窗口中,搜索“NSValue UIKit添加参考”(或谷歌)。

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