I have this text read into an NSString *responce which I am trying to parse into an array.
我将此文本读入NSString * responce,我试图将其解析为数组。
total: used: free: shared: buffers: cached:
Mem: 30412800 16805888 13606912 0 1581056 4837376
Swap: 0 0 0
MemTotal: 29700 kB
MemFree: 13288 kB
MemShared: 0 kB
Buffers: 1544 kB
Cached: 4724 kB
SwapCached: 0 kB
Active: 1197 kB
Inactive: 699 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 29700 kB
LowFree: 13288 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
Mapped: 277 kB
Slab: 132 kB
CommitLimit: 14848 kB
Committed_AS: 3400 kB
PageTables: 1567 kB
VmallocTotal: 1048404 kB
VmallocUsed: 17208 kB
VmallocChunk: 1031168 kB
If I read into an array like this, I get over 300 objects in the array!
如果我读入这样的数组,我会在数组中获得300多个对象!
NSMutableArray *items2 = [NSMutableArray arrayWithArray:[responce componentsSeparatedByString:@" "]];
NSLog(@"count of memory array = %i",[items2 count]);
I add this to try and remove all the blank ones, but still end up with 170.
我添加这个以尝试删除所有空白的,但最终仍然是170。
for (int i=0; i<[items2 count]; i++) {
NSString *str = [items2 objectAtIndex:i];
if([str length]==0 || !str || str==nil) {
[items2 removeObjectAtIndex:i];
}
}
This NSLog statement tells me that most of them are zero length, why where they not removed?
这个NSLog语句告诉我,大多数都是零长度,为什么不删除?
for (int i=0; i<[items2 count]; i++) {
NSLog(@"%i=%@ / length=%d",i,[items2 objectAtIndex:i],[[items2 objectAtIndex:i] length]);
}
5 个解决方案
#1
4
It looks like what you really want is a NSDictionary
+ NSScanner
:
看起来你真正想要的是NSDictionary + NSScanner:
NSDictionary *scanString(NSString *str)
{
NSScanner *scanner = [NSScanner scannerWithString:str];
NSMutableDictionary *results = [NSMutableDictionary dictionary];
// read away the first three lines (headers, mem, and swap). If you need this data, parse it here.
for (int i = 0; i < 3; i++) {
[scanner scanUpToString:@"\n" intoString:NULL];
}
NSString *line = nil;
while ([scanner scanUpToString:@"\n" intoString:&line]) {
// trim the line
line = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// read up to the ':'
int loc = [line rangeOfString:@":"].location;
NSString *key = [line substringToIndex:loc];
// read the value associated with the key
NSString *value = [[line substringFromIndex:loc + 1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
value = [value substringToIndex:[value rangeOfString:@" "].location];
[results setObject:value forKey:key];
}
return results;
}
This puts it in a dictionary that you can access by using the following:
这将它放在您可以使用以下内容访问的字典中:
NSDictionary *parsed = scanString(response);
NSLog(@"Active Memory: %@", [parsed objectForKey:@"Active"]);
#2
1
That is to be expected. Here's what the NSArray Class Reference has to say about componentsSeparatedByString:
这是可以预料的。以下是NSArray类参考对componentsSeparatedByString所说的内容:
The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty. For example, this code fragment:
数组中的子串按接收器中的顺序显示。相邻的分隔符字符串出现在结果中产生空字符串。类似地,如果字符串以分隔符开头或结尾,则第一个或最后一个子字符串分别为空。例如,这段代码片段:
P.S. Wouldn't it be easier if you split the input into lines first, and then split by spaces?
附:如果先将输入拆分为行,然后用空格分割,会不会更容易?
#3
1
The code you've written treats each individual space as a field separator (giving a huge number of empty fields), while the data you're trying to parse treats any number of spaces as a field separator. You'll want to either use NSScanner or NSRegularExpression to skip arbitrary numbers of spaces.
您编写的代码将每个单独的空间视为字段分隔符(提供大量空字段),而您尝试解析的数据将任意数量的空格视为字段分隔符。您将要使用NSScanner或NSRegularExpression跳过任意数量的空格。
#4
0
First, investigate using a NSScanner to get your information from your string instance. By default, scanners skip over white space and newline characters.
首先,使用NSScanner调查从字符串实例中获取信息。默认情况下,扫描程序会跳过空格和换行符。
Second, it appears that the first two lines of your information contain more information than the subsequent lines. Given this inconsistency, you might consider creating a model class to hold your data, rather than a simple array.
其次,您的信息的前两行似乎包含的信息多于后续行。鉴于这种不一致,您可以考虑创建一个模型类来保存数据,而不是简单的数组。
Good luck to you in your endeavors.
祝你在努力中好运。
#5
0
If you have new line characters between every two items in the string you can use:
如果您在字符串中的每两个项目之间有新的行字符,则可以使用:
NSArray *array = [yourResponseString componentsSeparatedByString:@"\n"];
Also if it is not a newline character which separates two of your components in each of the cases then you need to find a delimiter which separates each of the two elements of your array.
此外,如果它不是在每种情况下分隔两个组件的换行符,那么您需要找到一个分隔符,用于分隔数组的两个元素。
Hope this helps you.
希望这对你有所帮助。
#1
4
It looks like what you really want is a NSDictionary
+ NSScanner
:
看起来你真正想要的是NSDictionary + NSScanner:
NSDictionary *scanString(NSString *str)
{
NSScanner *scanner = [NSScanner scannerWithString:str];
NSMutableDictionary *results = [NSMutableDictionary dictionary];
// read away the first three lines (headers, mem, and swap). If you need this data, parse it here.
for (int i = 0; i < 3; i++) {
[scanner scanUpToString:@"\n" intoString:NULL];
}
NSString *line = nil;
while ([scanner scanUpToString:@"\n" intoString:&line]) {
// trim the line
line = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// read up to the ':'
int loc = [line rangeOfString:@":"].location;
NSString *key = [line substringToIndex:loc];
// read the value associated with the key
NSString *value = [[line substringFromIndex:loc + 1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
value = [value substringToIndex:[value rangeOfString:@" "].location];
[results setObject:value forKey:key];
}
return results;
}
This puts it in a dictionary that you can access by using the following:
这将它放在您可以使用以下内容访问的字典中:
NSDictionary *parsed = scanString(response);
NSLog(@"Active Memory: %@", [parsed objectForKey:@"Active"]);
#2
1
That is to be expected. Here's what the NSArray Class Reference has to say about componentsSeparatedByString:
这是可以预料的。以下是NSArray类参考对componentsSeparatedByString所说的内容:
The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty. For example, this code fragment:
数组中的子串按接收器中的顺序显示。相邻的分隔符字符串出现在结果中产生空字符串。类似地,如果字符串以分隔符开头或结尾,则第一个或最后一个子字符串分别为空。例如,这段代码片段:
P.S. Wouldn't it be easier if you split the input into lines first, and then split by spaces?
附:如果先将输入拆分为行,然后用空格分割,会不会更容易?
#3
1
The code you've written treats each individual space as a field separator (giving a huge number of empty fields), while the data you're trying to parse treats any number of spaces as a field separator. You'll want to either use NSScanner or NSRegularExpression to skip arbitrary numbers of spaces.
您编写的代码将每个单独的空间视为字段分隔符(提供大量空字段),而您尝试解析的数据将任意数量的空格视为字段分隔符。您将要使用NSScanner或NSRegularExpression跳过任意数量的空格。
#4
0
First, investigate using a NSScanner to get your information from your string instance. By default, scanners skip over white space and newline characters.
首先,使用NSScanner调查从字符串实例中获取信息。默认情况下,扫描程序会跳过空格和换行符。
Second, it appears that the first two lines of your information contain more information than the subsequent lines. Given this inconsistency, you might consider creating a model class to hold your data, rather than a simple array.
其次,您的信息的前两行似乎包含的信息多于后续行。鉴于这种不一致,您可以考虑创建一个模型类来保存数据,而不是简单的数组。
Good luck to you in your endeavors.
祝你在努力中好运。
#5
0
If you have new line characters between every two items in the string you can use:
如果您在字符串中的每两个项目之间有新的行字符,则可以使用:
NSArray *array = [yourResponseString componentsSeparatedByString:@"\n"];
Also if it is not a newline character which separates two of your components in each of the cases then you need to find a delimiter which separates each of the two elements of your array.
此外,如果它不是在每种情况下分隔两个组件的换行符,那么您需要找到一个分隔符,用于分隔数组的两个元素。
Hope this helps you.
希望这对你有所帮助。