gpt4 book ai didi

iphone - 如何在 Objective C 中从 NSString 中拆分换行符

转载 作者:可可西里 更新时间:2023-11-01 03:07:15 28 4
gpt4 key购买 nike

对于我的新项目。我已将我的 csv 文件内容加载为 NSString。首先,我需要用换行符分隔它,然后用逗号分隔每一行。我怎么能循环这一切?你能帮帮我吗?

CSV 内容

"^GSPC",1403.36,"4/27/2012","4:32pm",+3.38,1400.19,1406.64,1397.31,574422720 "^IXIC",3069.20,"4/27/2012","下午 5:30",+18.59,3060.34,3076.44,3043.30,0

ViewController.m

NSString* pathToFile = [[NSBundle mainBundle] pathForResource: @"quotes" ofType: @"csv"];
NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil];
if (!fileString) {
NSLog(@"Error reading file.");
}

NSArray *array = [fileString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

for(int i=0; i<[array count]; i++){
//
}

最佳答案

可能需要查看 NSMutableArray

// grab your file
NSMutableArray *data = [[fileString componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]] mutableCopy];
for (int i = 0; i < [data count]; i++)
{
[data replaceObjectAtIndex: i
withObject: [[data objectAtIndex: i] componentsSeparatedByString: @","]];
}

相关地,Dave DeLong 有一个合适的库来满足这个需求:https://github.com/davedelong/CHCSVParser

关于iphone - 如何在 Objective C 中从 NSString 中拆分换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10370516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com