朋友们,我有一个问题,希望你们能帮助我。所以..这是我的代码:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"convidados" ofType:@"csv"];
NSString* fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSMutableArray* pointStrings = [[NSMutableArray alloc] initWithArray:[fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
for(int idx = 0; idx < [pointStrings count]; idx++)
{
NSString* currentPointString = [pointStrings objectAtIndex:idx];
NSMutableArray* arr = [[NSMutableArray alloc] initWithArray:[currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]]];
NSString *nome = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:0]];
NSString *email = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:1]];
NSString *website = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:2]];
NSString *telefone = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:3]];
NSString *empresa = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:4]];
NSString *cidade = [[NSString alloc] initWithFormat:@"%@", [arr objectAtIndex:5]];
NSDictionary *registro = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects: nome, email , telefone, empresa, website, cidade, nil] forKeys:[NSArray arrayWithObjects:@"nome", @"email", @"telefone", @"empresa", @"website", @"cidade", nil]];
[PessoaDAO adicionar:registro];
[arr removeAllObjects];
[arr release];
}
我的 csv 文件:
Usuario1# Usuario1@gmail.com# http://facebook.com/Usuario1# 99999999#Company# Cidade
Usuario2# Usuario2@gmail.com# http://Usuario2.com# 737373773# Company# Fortaleza
Usuario3# Usuario3@gmail.com# http://Usuario3.com.br# 83838484# Company Solutions# City
问题是当执行下面的行时,当右边只有 3 个对象时识别出 7 个对象。我相信那是因为“newlineCharacterSet”它们添加了一个“,”并混淆了执行。
NSMutableArray* pointStrings = [[NSMutableArray alloc] initWithArray:[fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
NSMutableArray pointStrings 执行:
<__NSArrayM 0x1c586990>(
Usuario1# Usuario1@gmail.com# http://facebook.com/Usuario1# 99999999#Company# City,
,
Usuario2# Usuario2@gmail.com# http://Usuario2.com# 737373773# Company# City,
,
Usuario3# Usuario3@gmail.com# http://Usuario3.com.br# 83838484# Company# City,
,
)
非常感谢。