私の友達、私はあなたが私を助けてくれることを願っている問題を抱えています。だから..これが私のコードです:
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,
,
)
よろしくお願いします。