2011-03-10 64 views
1

我有以下的功能,但我不知道如何释放定义的临时对象的内存:目标C的内存管理问题:返回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; 
} 

回答

4

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

return [set autorelease]; 
当然
+0

我将不得不探测如果设置=零吧!? – PartySoft 2011-04-12 08:48:09

+0

你可以。这一切都取决于你希望一切工作。我的猜测是,如果它是零,你会想返回一个空的'PatientSet'。你可以这样做:'[set autorelease]; return set!= nil? set:[[[PatientSet alloc] init] autorelease];'。 – FreeAsInBeer 2011-04-12 12:45:23