如何将具有浮点值的数组保存到Mac可读的文本文件中?

时间:2021-09-13 18:03:03

I want to save float values stored in an array into a text file and read the file directly on Mac. This is how I create the array:

我想将存储在数组中的浮点值保存到文本文件中,并直接在Mac上读取文件。这是我创建数组的方式:

dataArray = [[NSMutableArray alloc] init];
NSNumber *numObj = [NSNumber numberWithFloat:3.14];
[dataArray insertObject:numObj atIndex:0];
NSNumber *numObj = [NSNumber numberWithFloat:2.3];
[dataArray insertObject:numObj atIndex:1];
...

This is how I save the array:

这是我保存数组的方式:

NSData *savedData = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
NSString *filePath = @"/Users/smith/Desktop/dataArray.txt";
[savedData writeToFile:filePath options:NSDataWritingAtomic error:nil];

When I open the file, the contents are just garbled letters. Instead, I want to make it something like this:

当我打开文件时,内容只是乱码。相反,我想做到这样的事情:

3.14
2.3
1.4
...

3 个解决方案

#1


1  

You can use componentsJoinedByString: to make an in-memory representation first, and then write that representation into a file, like this:

您可以使用componentsJoinedByString:首先创建内存中表示,然后将该表示写入文件,如下所示:

NSString *fileRep = [dataArray componentsJoinedByString:@"\n"];
NSString *filePath = @"/Users/smith/Desktop/dataArray.txt";
[fileRep writeToFile:filePath options:NSDataWritingAtomic error:nil];

This assumes that the number of items is relatively small, because the string representation is created entirely in memory.

这假定项的数量相对较小,因为字符串表示完全在内存中创建。

Reading back is not as nice as writing out, though: you start by reading back a string, theb split it to components using [fileRep componentsSeparatedByString:@"\n"], and then go through components in a loop or with a block, adding [NSNumber numberWithDouble:[element doubleValue]] for each element of your split.

但回读并不像写出那样好:首先回读一个字符串,然后使用[fileRep componentsSeparatedByString:@“\ n”]将它拆分为组件,然后遍历循环中的组件或块,为拆分的每个元素添加[NSNumber numberWithDouble:[element doubleValue]]。

#2


2  

the program you've written saves the object representation as an array of NSNumbers, while the result you want/expect is a text file separated by newlines.

您编写的程序将对象表示保存为NSNumbers数组,而您希望/期望的结果是由换行符分隔的文本文件。

to save those float values into a text file of that format, you could to this:

要将这些浮点值保存到该格式的文本文件中,您可以:

...
NSMutableString * string = [NSMutableString new];
[string appendFormat:@"%f\n", 3.14];
[string appendFormat:@"%f\n", 2.3];

NSError * error = nil;
BOOL written = [string writeToURL:url atomically:YES encoding:someEncoding error:&error];
...

#3


1  

You probably want to create an XML plist from it to make it human-readable:

您可能希望从中创建XML plist以使其具有人类可读性:

[dataArray writeToFile:filePath atomically:YES];

This creates a property list, which is human-readable XML (except if the file already exists AND it's a binary plist, in this case the new plist will also be binary).

这将创建一个属性列表,该列表是人类可读的XML(除非该文件已存在且它是二进制plist,在这种情况下,新plist也将是二进制)。

#1


1  

You can use componentsJoinedByString: to make an in-memory representation first, and then write that representation into a file, like this:

您可以使用componentsJoinedByString:首先创建内存中表示,然后将该表示写入文件,如下所示:

NSString *fileRep = [dataArray componentsJoinedByString:@"\n"];
NSString *filePath = @"/Users/smith/Desktop/dataArray.txt";
[fileRep writeToFile:filePath options:NSDataWritingAtomic error:nil];

This assumes that the number of items is relatively small, because the string representation is created entirely in memory.

这假定项的数量相对较小,因为字符串表示完全在内存中创建。

Reading back is not as nice as writing out, though: you start by reading back a string, theb split it to components using [fileRep componentsSeparatedByString:@"\n"], and then go through components in a loop or with a block, adding [NSNumber numberWithDouble:[element doubleValue]] for each element of your split.

但回读并不像写出那样好:首先回读一个字符串,然后使用[fileRep componentsSeparatedByString:@“\ n”]将它拆分为组件,然后遍历循环中的组件或块,为拆分的每个元素添加[NSNumber numberWithDouble:[element doubleValue]]。

#2


2  

the program you've written saves the object representation as an array of NSNumbers, while the result you want/expect is a text file separated by newlines.

您编写的程序将对象表示保存为NSNumbers数组,而您希望/期望的结果是由换行符分隔的文本文件。

to save those float values into a text file of that format, you could to this:

要将这些浮点值保存到该格式的文本文件中,您可以:

...
NSMutableString * string = [NSMutableString new];
[string appendFormat:@"%f\n", 3.14];
[string appendFormat:@"%f\n", 2.3];

NSError * error = nil;
BOOL written = [string writeToURL:url atomically:YES encoding:someEncoding error:&error];
...

#3


1  

You probably want to create an XML plist from it to make it human-readable:

您可能希望从中创建XML plist以使其具有人类可读性:

[dataArray writeToFile:filePath atomically:YES];

This creates a property list, which is human-readable XML (except if the file already exists AND it's a binary plist, in this case the new plist will also be binary).

这将创建一个属性列表,该列表是人类可读的XML(除非该文件已存在且它是二进制plist,在这种情况下,新plist也将是二进制)。