2012-04-24 5 views
0

여기 내 설정입니다. 나는 호출 부호 (callign), 선언 된 주파수 및 amStationInfo라는 NSMutableArray와 같은 여러 문자열이있는 radiostations라는 객체를 가지고 있습니다. 내의 ViewController에, 나는 내가 jQuery과배열에 문제가 있습니다.

채울 그래서 같은 배열을 채 웁니다 SQLite는 데이터베이스 ...

radiostations.h

@interface radiostations : NSObject { 

    NSString *format; 
    NSString *city; 
} 

@property (nonatomic, retain) NSString *format; 
@property (nonatomic, retain) NSString *city; 

ViewController.m

radiostations *amStationClass = [[radiostations alloc] init]; 
NSMutableArray* amStationInfo = [[NSMutableArray alloc] init]; 

while (sqlite3_step(statement) == SQLITE_ROW) 
    { 
    NSString *cityField = [[NSString alloc] initWithUTF8String: 
            (const char *) sqlite3_column_text(statement, 10)]; 
    NSString *formatField = [[NSString alloc] initWithUTF8String: 
            (const char *) sqlite3_column_text(statement, 0)]; 
    [amStationInfo addObject:amStationClass]; 
        [amStationClass setCity:cityField]; 
        [amStationClass setFormat:formatField]; 
    } 
[tabView reloadData]; 
sqlite3_finalize(statement); 

다음에 액세스

NSString *cityValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] city]; 
NSString *formatValue = [(radiostations *)[amStationInfo objectAtIndex:indexPath.row] format]; 
cityLabel.text = cityValue; 
formatLabel.text = formatValue; 

처음에는 몇 개의 어레이를 다루었지만 제대로 작동했습니다. 클래스 객체를 사용하는 하나의 배열 만 처리하도록 변경했습니다. 이제는 작동하지 않습니다. 나는 SQLite 쿼리를 알고 있으며, 어떤 문제가 없다고 생각한다. 그것은 배열이 채워지지 않는 것처럼 보인다.

+0

귀하의 이름은 엉망입니다; 'radioStations','amStationClass'는'amStation'이어야합니다 (클래스가 아니며 인스턴스입니다),'amStationInfo'는 아마도'amStationsArray'이어야하고,'cityField'는'city'이어야합니다. 'cityName' 등 ... 또한 ARC를 사용하지 않는다면 두 개의 문자열도 누출 될 것입니다. – bbum

+0

팁 주셔서 감사합니다, 나는 아주 새롭고 나는 그것을 깨달았습니다. 나는 ARC를 사용하고있다. – StarvingPilot

+0

걱정할 필요가 없습니다. Objective-C, iOS, Cocoa 및 그 사이의 모든 프레임 워크는 수많은 공통 패턴을 기반으로 구축되었습니다. 그 패턴을 배우고 따라 가면, 훨씬 더 간단해질 것입니다! – bbum

답변

2

당신은 같은 radiostations 개체의 속성을 변경하고 배열에 또 다시 그것을 추가 개체를 추가하기 전에, 한 번에 할당해야합니다. 그렇지 않으면 해당 개체를 누출하지 않도록하는 것이 필요하다,

while (...) { 
    // fetch data as before 

    radiostations *record = [[radiostations alloc] init]; 
    [record setCity: cityField]; 
    [record setFormat: formatField]; 
    [amStationInfo addObject: record]; 
    [record release]; 
} 

당신이 라인 [record release];를 제거해야 ARC를 사용하는 경우 : 당신은 당신의 SQLite 데이터베이스에서 각 행에 대해 새로운 radiostations 객체를 생성하고이를 추가해야합니다.

+0

ARC를 사용하고 있기 때문에 amStationClass를 공개하지 않습니다 – StarvingPilot

+0

이제 작동합니다. while 문 내부에 레코드를 할당하고 바깥 쪽을 할당 할 필요가 있었고 문자열을 설정 한 후 배열에 객체를 추가해야했습니다 ... 감사! – StarvingPilot

1

어디에서 mutablearray를 할당 했습니까? 무엇인가 :

NSMutableArray* amStationInfo = [[NSMutableArray alloc] init]; 

당신은

+0

나는 그것을 할당하는 것을 잊었다. 그리고 비록 내가 했어도, 그것은 아직도 채우지 않는다 – StarvingPilot

+0

내가 그 오류를 알아 차렸을 때 나는 당신의 질문을 급하게 읽고 고죄한다. 그러나 아마 다른 것은있다. .. Sven 대답에 관해 무엇? – meronix

+0

스벤의 대답이 효과가있었습니다. 나는 두 가지를 변경했다. 나는 while 루프 안에 인스턴스를 할당하고 문자열을 설정 한 후 배열에 추가했다. – StarvingPilot

관련 문제