0
我有這樣大的疑問: 我有我的功能在一類這樣的:IOS使用對象返回一個函數,並用它
-(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類?
感謝
謝謝chang,所以如果我改變創建Shot *爲autorelase就是解決方案... – ghiboz
@ghiboz是的。自動釋放 – ARC