2009-10-12 5 views
0

에서 생성 배열이 인 나는 배열에 SQL 데이터를로드하기 위해 다음 코드를 사용하여 계획입니다 iPhone SDK: loading UITableView from SQLite아이폰 SDK : SQLite는의 로딩 jQuery과 - SQLite는

FORR 후속. 배열의 각 요소는 각 데이터베이스 항목을 나타내는 클래스가됩니다.

@interface 행 : NSObject { int PK; NSString * desc;

}

@property int PK; @property (비 원자력, 보유) NSString * desc;

@end

로드 작업은 다음과 유사합니다 첫 번째 루프

물론
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:1]; 
Row *myRow = [[Row alloc] init]; 


for (int i=0; i<10; i++) 
{ 
    myRow.PK = i; 
    myRow.desc = [[NSString alloc] initWithFormat:@"Hello: %d", i]; 
    [array addObject:myRow]; 
} 
[myRow release]; 

for (int i=0; i < [array count]; i++) 
{ 
    Row *myNrow = [array objectAtIndex:i] ; 
    NSLog (@"%@ %d", [myNrow desc], [myNrow PK]); 
    myNrow = nil; 
} 

는 SELECT 문에서 루프 될 것입니다. 다른 루프 (또는 해당 루프의 요소)는 cellInRowIndex 메서드에서 데이터를 렌더링합니다.

메모리 누수에 대해 질문이 있습니다. 위의 코드는 메모리 누수가 있습니까? Row 클래스의 decs string 속성은 (retain)으로 선언됩니다. 그것은 어딘가에 공개해서는 안되는가?

감사합니다.

답변

1

myRow.desc에 넣을 문자열을 해제해야합니다. 당신이 (당신이 코멘트에서 언급 한 바와 같이) 중간있는 NSString를 사용하려면, 당신은 할 수 : 당신은

myRow.desc = [[[NSString alloc] initWithFormat:@"Hello: %d", i] autorelease]; 

또는

중 하나
myRow.desc = [NSString stringWithFormat:@"Hello: %d", i]; 

EDIT가

myRow.desc = [[NSString alloc] initWithFormat:@"Hello: %d", i]; 

을 변화시킬 수 다음과 같이하십시오.

NSString *foo = [[NSString alloc] initWithFormat:@"Hello: %d", i]; 
myRow.desc = foo; 
[foo release]; 

나 : 당신이 그것을 해제하지 않아야하므로 두 번째 예제 foo는 이미, 오토 릴리즈되어

NSString *foo = [NSString stringWithFormat:@"Hello: %d", i]; 
myRow.desc = foo; 

참고.

+0

감사합니다. 내가이 선언과 같은 것을 가지고있다 : NSString * foo; 는 또한이 작업을 수행해야 : foo는 = [있는 NSString initWithFormat을 : @ "안녕하세요가 % d", i]를 또는 foo는 = [[[있는 NSString ALLOC] initWithFormat : @ "안녕하세요가 % d", i]는 오토 릴리즈를 ] 지금이 있습니다 : foo = [[NSString alloc] initWithFormat : @ "Hello % d", d]; ... [foo release] – leon

+0

정리하려고합니다 .... 고맙습니다.내가이 선언과 같은 것을 가지고있다 : NSString * foo; 해야 할 일 :

 foo = [NSString initWithFormat: @"Hello %d", i] or foo = [[[NSString alloc] initWithFormat:@"Hello %d", i] autorelease] Now I have this: foo = [[NSString alloc] initWithFormat: @"Hello %d", d]; ... [foo release] 
leon