2010-07-06 5 views
1

NSArray 활성화 및 정수 추가에 문제가 있습니다. 여기에 내 코드가 있는데, 나는 이것을 달성하기 위해 노력하고 있다고 말했다. 개체를 추가 할 때 내 응용 프로그램이 충돌하지만 배열을 올바르게 지우는 지 알 수 없습니다. NSArray 초기화 및 NSInteger 추가

//CREATE AND INITIALIZE AN ARRAY 
NSArray *ticket;               
ticket = [[NSArray alloc] init]; 

//slotA,slotB,slotC are of type NSInteger that I am trying to add 
//to the array (THIS CRASHES) 

[ticket addObject:[NSNumber numberWithInt:slotA]]; 
[ticket addObject:[NSNumber numberWithInt:slotB]]; 
[ticket addObject:[NSNumber numberWithInt:slotC]]; 

//I never got to this line of code but I think it has to be wrong 
//because this would throw the whole //array away. I dont want it 
//to be thrown away I just wanna clear it out but keep it instanciated. 

[ticket release]; 

나는이 시도,하지만 내가 "는 sentinal 함수 호출이 없습니다"하고 말 또한

NSArray *ticket; 
NSString *sltA=[NSString stringWithFormat:@"%d", slotA]; 
NSString *sltB=[NSString stringWithFormat:@"%d", slotB]; 
NSString *sltC=[NSString stringWithFormat:@"%d", slotC]; 
ticket = [[NSArray alloc] initWithObjects:sltA,sltB,sltC]; 

, 내가 배열에 넣어하는 문자열로 정수를 변경해야합니까?

+0

나는 이것을 시험해 보았지만, 나는 "sentinal function call이 없다"는 말을했다. NSArray * ticket; \t \t \t \t \t \t \t \t \t \t는 NSString sltA * = [있는 NSString stringWithFormat : @ "가 % d"slotA]; \t \t \t \t \t NSString * sltB = [NSString stringWithFormat : @ "% d", slotB]; \t \t \t \t \t NSString * sltC = [NSString stringWithFormat : @ "% d", slotC]; \t \t \t \t \t \t \t \t \t \t 티켓 = [NSArray를 ALLOC의] initWithObjects : sltA, sltB, SLTC]; 또한 배열에 넣으려면 문자열을 정수로 변경해야합니까? –

답변

4

NSArray를 NSMutableArray로 변경하십시오. 이렇게하면 개체를 추가하고 제거 할 수 있습니다.

NSArray는 생성 후 변경되지 않는 배열입니다.

NSMutableArray는 NSArray의 하위 클래스이며 배열의 어느 지점에서나 객체를 추가하고 제거하는 데 도움이되는 많은 기능을 제공합니다.

+1

... 또는'-initWithObjects :'를 사용하십시오. 배열이 변경되지 않는 경우에만 작동하는 –

+0

입니다. 슬롯 A를 배열에 추가하기 전에 슬롯 A를 바꿀 것입니다. –

+0

코드 예제를 –

-1

코드가 정확합니다 (기술적으로는 +numberWithInt: 대신 +numberWithInteger:을 사용해야하지만 분명히 충돌을 일으키지는 않습니다). 충돌하는 내용은 무엇입니까? GDB/Xcode에서 출력물을 게시 할 수 있습니까?

+1

코드가 올바르지 않습니다. NSArray 객체는 변경할 수 없습니다. – zneak

+0

그리고'numberWithInt :'는 더 이상 사용되지 않으므로'numberWithInteger :'를 선호 할 이유가 없습니다. – zneak

+2

Oof, 개체의 클래스를 간과. '+ numberWithInt :'는 더 이상 사용되지 않지만 'NSInteger'와 함께 사용하는 것은 잘못된 방법입니다. '+ numberWithInt :'는'int'를위한 것으로, 다른 형식입니다 (iPhone 및 32 비트 Mac에서는 typedef로, 64 비트 Mac에서는 실제 유형으로). –