Objective C内存管理问题:返回NSObject继承的类类型

时间:2011-03-10 23:58:30

标签: iphone objective-c function return-value nsobject

我有以下功能,但我不知道如何释放定义的临时对象的内存:

#import <UIKit/UIKit.h>

@interface PatientsSet :  NSObject  {
    NSString *tableid;
    NSString *patient_name;
    NSString *patient_surname;
    NSString *city;
    NSString *State;
    NSString *phone;

}
@property (nonatomic, retain) NSString *tableid;
@property (nonatomic, retain) NSString *patient_name;
@property (nonatomic, retain) NSString *patient_surname;
@property (nonatomic, retain) NSString *city;
@property (nonatomic, retain) NSString *State;
@property (nonatomic, retain) NSString *phone;


-(id)initWithSet:(NSString *)dd patient_name:(NSString *)dn patient_surname:(NSString *)dsn city:(NSString *)ct  state:(NSString *)st phone:(NSString *)ph   ;


@end

(我从SQLITE DB获取数据到NSobject派生类) 我不应该使用[set release];某处??

-(PatientsSet *)getPatientById:(NSString *)ID {
    PatientsSet *set;
    // Setup the database object

    sqlite3 *database;
    // Init the doctors_set Array
    doctors_set = [[NSMutableArray alloc] init];
    //NSString * databasePath1=[ [self getDocumentsDirectory] stringByAppendingPathComponent:databaseName   ];  
    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        NSString* myString = [NSString stringWithFormat:@"SELECT patients.id,patients.patient_name,patients.patient_surname,patients.phone FROM patients where  patients.id = '%@'",ID];

        const char *sqlStatement = [myString cString];

        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                // Read the data from the result row
                NSString *aId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *aDurname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *aPhone = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                // Create a new set object with the data from the database
                set=[[PatientsSet alloc] initWithSet:aId patient_name:aName patient_surname:aDurname city:@"" state:@"" phone:aPhone];



            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

    return set;
}

1 个答案:

答案 0 :(得分:4)

你需要像这样返回一个自动释放的对象:

return [set autorelease];