Problem with For Loop and Array
The below method code is a FOR loop which builds 26 arrays (one for each letter of the alphabet) from an SQLite database.
The problem is that the return array called MultipleArray is only returning 16 arrays, and not 25 as it should. Can anyone spot why it is not returning the full 25 arrays ?
EDIT - The problem seems to be that since the letter Q has no data in the database (which is correct) array 17 (letterQ) has no content. The statement which builds the mulptipleArray then terminates adding arrays at this point. Anyone know how I can fix this ?
Thank you.
- (NSMutableArray *)placesInfo {
NSString *tmpLike = [[NSString alloc] init];
multipleArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 26; i++) {
NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];
tmpLike = @"";
switch (i) {
case 0:
tmpLike = @"A";
break;
case 1:
tmpLike = @"B";
break;
case 2:
tmpLike = @"C";
break;
case 3:
tmpLike = @"D";
break;
case 4:
tmpLike = @"E";
break;
case 5:
tmpLike = @"F";
break;
case 6:
tmpLike = @"G";
break;
case 7:
tmpLike = @"H";
break;
case 8:
tmpLike = @"I";
break;
case 9:
开发者_开发知识库 tmpLike = @"J";
break;
case 10:
tmpLike = @"K";
break;
case 11:
tmpLike = @"L";
break;
case 12:
tmpLike = @"M";
break;
case 13:
tmpLike= @"N";
break;
case 14:
tmpLike = @"O";
break;
case 15:
tmpLike = @"P";
break;
case 16:
tmpLike = @"Q";
break;
case 17:
tmpLike = @"R";
break;
case 18:
tmpLike = @"S";
break;
case 19:
tmpLike = @"T";
break;
case 20:
tmpLike = @"U";
break;
case 21:
tmpLike = @"V";
break;
case 22:
tmpLike = @"W";
break;
case 23:
tmpLike = @"X";
break;
case 24:
tmpLike = @"Y";
break;
case 25:
tmpLike = @"Z";
break;
default:
break;
}
NSString *query = [[NSString alloc] initWithFormat: @"SELECT Name, Description, Postcode, AddressLine1, ImageURL, Free, Area, OpeningTimes, NearestTube, Cost,UniqueID, URL, Number, FirstLetter FROM MainDetails WHERE FirstLetter = '%@%'",tmpLike];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *) sqlite3_column_text(statement, 0);
char *desChars = (char *) sqlite3_column_text(statement, 1);
char *postChars = (char *) sqlite3_column_text(statement, 2);
char *addyChars = (char *) sqlite3_column_text(statement, 3);
char *imageChars = (char *) sqlite3_column_text(statement, 4);
char *freeChars = (char *) sqlite3_column_text(statement, 5);
char *areaChars = (char *) sqlite3_column_text(statement, 6);
char *openChars = (char *) sqlite3_column_text(statement, 7);
char *nearChars = (char *) sqlite3_column_text(statement, 8);
char *costChars = (char *) sqlite3_column_text(statement, 9);
int uniqueID = sqlite3_column_int(statement, 10);
char *urlChars = (char *) sqlite3_column_text(statement, 11);
char *numberChars = (char *) sqlite3_column_text(statement, 12);
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
NSString *description = [[NSString alloc] initWithUTF8String:desChars];
NSString *postcode = [[NSString alloc] initWithUTF8String:postChars];
NSString *address = [[NSString alloc] initWithUTF8String:addyChars];
NSString *image = [[NSString alloc] initWithUTF8String:imageChars];
NSString *free = [[NSString alloc] initWithUTF8String:freeChars];
NSString *area = [[NSString alloc] initWithUTF8String:areaChars];
NSString *openT = [[NSString alloc] initWithUTF8String:openChars];
NSString *near = [[NSString alloc] initWithUTF8String:nearChars];
NSString *cost = [[NSString alloc] initWithUTF8String:costChars];
NSString *url = [[NSString alloc] initWithUTF8String:urlChars];
NSString *number = [[NSString alloc] initWithUTF8String:numberChars];
PlaceObject *info = [[PlaceObject alloc]
initWithUniqueID: uniqueID name : name description:description postCode:postcode addressOne : address image:image free:free area:area openingTimes:openT nearestTube:near cost:cost url:url number:number];
[retval addObject:info];
int x= [retval count];
NSLog(@"%d",i);
if (x ==0) {
NSLog(@"No content");
}
switch (i) {
case 0:
letterA = retval;
break;
case 1:
letterB = retval;
break;
case 2:
letterC = retval;
break;
case 3:
letterD = retval;
break;
case 4:
letterE = retval;
break;
case 5:
letterF = retval;
break;
case 6:
letterG = retval;
break;
case 7:
letterH = retval;
break;
case 8:
letterI = retval;
break;
case 9:
letterJ = retval;
break;
case 10:
letterK = retval;
break;
case 11:
letterL = retval;
break;
case 12:
letterM = retval;
break;
case 13:
letterN = retval;
break;
case 14:
letterO = retval;
break;
case 15:
letterP = retval;
break;
case 16:
letterQ = retval;
break;
case 17:
letterR = retval;
break;
case 18:
letterS = retval;
break;
case 19:
letterT = retval;
break;
case 20:
letterU = retval;
break;
case 21:
letterV = retval;
break;
case 22:
letterW = retval;
break;
case 23:
letterX = retval;
break;
case 24:
letterY = retval;
break;
case 25:
letterZ = retval;
break;
default:
break;
}
[name release];
query = @"";
[query release];
[description release];
[url release];
[number release];
[postcode release];
[address release];
[image release];
[free release];
[area release];
[openT release];
[near release];
[cost release];
[info release];
}
sqlite3_finalize(statement);
}
}
tmpLike = @"";
[tmpLike release];
multipleArray = [NSMutableArray arrayWithObjects: letterA, letterB, letterC, letterD, letterE, letterF, letterG, letterH, letterI, letterJ, letterK, letterL, letterM, letterN, letterO,letterP, letterQ, letterR, letterS, letterT, letterU, letterV, letterW, letterX, letterY, letterZ, nil];
int x= [multipleArray count];
NSLog(@"Mulptilpe Array Contents: %d", x);
NSLog(@"%@",multipleArray);
return multipleArray;
}
EDIT - The problem seems to be that since the letter Q has no data in the database (which is correct) array 17 (letterQ) has no content. The statement which builds the mulptipleArray then terminates adding arrays at this point. Anyone know how I can fix this ?
If you need into insert a nil
value into your array, you use [NSNull null]
. There's nothing magical about this value. It's just an object that you can later test against. You cannot insert a real nil
into an NSMutableArray
.
I would start by simplifying your code. The two big switch statements are unnecessary.
You can get drop the first one by getting rid of the tmpLike
variable and replacing the
query string with:
NSString *query = [[NSString alloc] initWithFormat: @"SELECT ...blah blah blah... WHERE FirstLetter = '%c%'", 'A' + i];
I assume the letterA...letterZ are NSMutableArray
s declared somewhere else. You can get rid of the second switch statement
and all the letter*
arrays by simply adding retval
to multipleArray
.
[multipleArray addObject:retval]
That way you're incrementally building multipleArray
rather than trying to build it all at once at the end. Also if the query fails for any one letter, only that letter will be missing. The following letters will still be added.
I am going to give a little hint about basic characters that will save you a lot of code! you can perform arithmetics on them to change letters.
unichar letter = L'A' + i;
tempLike = [NSString stringWithCharacters:&letter length:1];
Now the actual problem is you need to check each object for nil and replace it with a custom object (usually [NSNull null]
) because once you pass the nil sentinel to the method it thinks that it is the end.
I would suggest you . to implement code that check string before enter into database. if string is null then replace with @""(blank). Thanks
Try this before your second switch statement:
if (!retval) {
retval = [NSMutableArray arrayWithObject:[NSNull null]];
} else if (![retval count]) {
[retval addObject:[NSNull null]];
NSLog(@"No content");
}
....
精彩评论