我有这个大疑问: 我在这样的课程中有我的功能:
-(Shot*) getShot:(int)shot {
NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM tbShots where nShot = %d ", shot];
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex:0];
NSString *dbFilePath = [documentFolderPath stringByAppendingPathComponent:DATABASE_NAME_EXT];
if (dbFilePath == NULL) {
NSLog(@"dbFilePath is NULL");
}
sqlite3 *dbHandle;
if (sqlite3_open([dbFilePath UTF8String], &dbHandle)) {
NSLog(@"sqlite3_open: failed");
}
sqlite3_stmt *preparedStatement;
const char* queryStatement = [sqlStr UTF8String];
sqlite3_prepare_v2(dbHandle, queryStatement, -1, &preparedStatement, NULL);
Shot *s = nil;
NSString * note = @"";
while( sqlite3_step(preparedStatement) == SQLITE_ROW)
{
s = [[Shot alloc] initWithShot:shot];
}
sqlite3_finalize(preparedStatement);
sqlite3_close(dbHandle);
return s;
}
在本课程之外我用这个:
Shot* sP = [appDelegate getShot:nTarget];
[self drawShoot:sP];
但我认为这不是检索对象实例并使用它的正确方法...... 什么是最好的方式??? 在使用实例sP之后,我需要发布吗?如果我释放操作影响appDelegate类?
感谢
答案 0 :(得分:2)
s = [[Shot alloc] initWithShot:shot]; 将它自动释放的对象改为[[[Shot alloc] initWithShot:shot] autorelease];然后你的方法返回自动释放的对象。 Shot * sP = [appDelegate getShot:nTarget]; [self drawShoot:sP]; 应该没问题。否则,当你的回归s时,你会在Shot对象“s”上留下保留计数1;在你的方法中。