以下方法代码是FOR循环,它从SQLite数据库构建26个数组(每个字母表一个字母)。
问题是名为MultipleArray的返回数组只返回16个数组,而不是应该返回25个数组。谁能发现为什么它没有返回完整的25个阵列?
编辑 - 问题似乎是因为字母Q在数据库中没有数据(这是正确的)数组17(字母Q)没有内容。构建mulptipleArray的语句然后在此时终止添加数组。有谁知道如何解决这个问题?
谢谢。
- (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;
}
答案 0 :(得分:2)
编辑 - 问题似乎是因为字母Q在数据库中没有数据(这是正确的)数组17(字母Q)没有内容。构建mulptipleArray的语句然后在此时终止添加数组。有谁知道如何解决这个问题?
如果您需要在数组中插入nil
值,请使用[NSNull null]
。这个价值没有什么神奇之处。它只是一个你可以在以后测试的对象。您无法将真实nil
插入NSMutableArray
。
答案 1 :(得分:1)
我首先要简化你的代码。两个大的switch语句是不必要的。
您可以通过删除tmpLike
变量并替换它来删除第一个
查询字符串:
NSString *query = [[NSString alloc] initWithFormat: @"SELECT ...blah blah blah... WHERE FirstLetter = '%c%'", 'A' + i];
我认为字母A ... letterZ是NSMutableArray
在其他地方宣布的。你可以摆脱第二个switch语句
只需将letter*
添加到retval
。
multipleArray
数组
[multipleArray addObject:retval]
这样你就可以逐步构建multipleArray
,而不是试图在最后一次构建它。此外,如果任何一个字母的查询失败,则只会丢失该字母。仍会添加以下字母。
答案 2 :(得分:1)
我将给出一些基本字符的提示,这将为您节省大量代码!你可以对它们进行算术改变字母。
unichar letter = L'A' + i;
tempLike = [NSString stringWithCharacters:&letter length:1];
现在实际的问题是你需要检查每个对象是否为nil并将其替换为自定义对象(通常为[NSNull null]
),因为一旦你将nil sentinel传递给方法,它就会认为它是结束。< / p>
答案 3 :(得分:1)
我会建议你。实现在进入数据库之前检查字符串的代码。如果string为null,则替换为@“”(空白)。 感谢
答案 4 :(得分:1)
在第二个switch语句之前尝试这个:
if (!retval) {
retval = [NSMutableArray arrayWithObject:[NSNull null]];
} else if (![retval count]) {
[retval addObject:[NSNull null]];
NSLog(@"No content");
}
....