Reputation: 1028
My friends, I have an issue that I hope you can help me. So..Here is my code:
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];
}
My csv file:
Usuario1# [email protected]# http://facebook.com/Usuario1# 99999999#Company# Cidade
Usuario2# [email protected]# http://Usuario2.com# 737373773# Company# Fortaleza
Usuario3# [email protected]# http://Usuario3.com.br# 83838484# Company Solutions# City
The problem is when the line below is executed is recognized 7 objects when the right is only 3. I believe thats because the " newlineCharacterSet " them add a " , " and confuses the execution.
NSMutableArray* pointStrings = [[NSMutableArray alloc] initWithArray:[fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
NSMutableArray pointStrings execution:
<__NSArrayM 0x1c586990>(
Usuario1# [email protected]# http://facebook.com/Usuario1# 99999999#Company# City,
,
Usuario2# [email protected]# http://Usuario2.com# 737373773# Company# City,
,
Usuario3# [email protected]# http://Usuario3.com.br# 83838484# Company# City,
,
)
Thanks so much in advanced.
Upvotes: 0
Views: 277
Reputation: 47749
Mostly unrelated to your bug (whatever it is) --
NSArray* arr = [currentPointString componentsSeparatedByString:@"#"];
NSDictionary *registro = [[NSDictionary alloc] initWithObjects:arr forKeys:[NSArray arrayWithObjects:@"nome", @"email", @"website", @"telefonee", @"empresa", @"cidade", nil]];
The code formerly between those two lines is unnecessary, as is the [arr removeAllObjects]. And the above eliminates the need for the [arr release].
(And if you don't remove the bogus [[NSString alloc] initWithFormat:@"%@", ...
calls you're leaking a lot of storage.)
(Note that I provide this largely because (by doing copy/paste without really knowing what you're doing) you've gotten your code too complex for you to understand it. By simplifying you can better zero in on problems.)
Upvotes: 0
Reputation: 17659
This is probably happening because the file you are using was created in Windows which uses two characters (CR+LF
) at the end of a line. Your code splitting your lines at CR
and then splitting again at LF
which creates an empty line.
Just check if the count of arr
is less than 6, or the length of currentPointString
is 0 and if so continue:
for(int idx = 0; idx < [pointStrings count]; idx++) {
NSString *currentPointString = [pointStrings objectAtIndex:idx];
if ([currentPointString length] == 0) {
continue;
}
NSMutableArray *arr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
if ([arr count] < 6) {
continue;
}
...
}
This will also take care of cases where there is a blank line in the file to begin with.
Upvotes: 1
Reputation: 6773
'csv' is an acronym for 'comma separated values', your data (as shown) is separated by spaces, not commas.
Upvotes: 0