2011-03-10 38 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