0

I have a file like this:

1.0 1.0
2.0 2.0
-3.0 2.0

each line is the coordinate of a point.

I don't know how to write a code to do following thing: read these coordinates from the file, convert them into double and store them in an array as NSPoint.

BTW, I tried to write Objective-C++, but it seems that ifstream does not work, which is a bug of XCode.

4

1 回答 1

0

这样做:

注意:您不能将 NSPoint 存储在 NSArray 中,因为所有集合类都需要 obj-c 对象来存储并且 NSPoint 是结构。所以你需要把它转换成NSValue。

NSString *yourPath=[@"~/Desktop/myFile.txt" stringByExpandingTildeInPath];
NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:yourPath];
NSData  *myData=[inFile readDataToEndOfFile];

NSString *myText=[[NSString alloc]initWithData:myData encoding:NSASCIIStringEncoding];

NSArray *values = [myText componentsSeparatedByString:@"\n"];

NSMutableArray *points=[NSMutableArray new];
for (NSString *string in values) {
    NSArray *lines=[string componentsSeparatedByString:@" "];
    NSPoint point=NSMakePoint([lines[0]floatValue], [lines[1]floatValue]);
    points[points.count]=[NSValue valueWithPoint:point];
}


for (NSValue *value in points) {
    NSLog(@"->%@",value);
}
于 2013-04-02T12:51:42.500 回答