대기열 (CHCircularBufferQueue)에 일련의 char * 객체를 추가하는 데 문제가 있습니다. Queue는 객체를 입력으로 받아들입니다 (이해하는 한). 따라서 간단하게 char * tokenName을 포함하는 tokenObject라는 NSObject에서 상속받은 간단한 객체를 생성했습니다. Queue에 새로운 tokenObject를 추가 할 때마다 새로운 tokenObject를 할당 한 다음 Queue에 다음 번에 추가 할 때 Queue에 새로운 tokenObject를 전달합니다. 그러나 tokenObject.tokenName을 새로운 char * 문자열로 설정하면 Queue의 모든 tokenObject가 무시되므로 전체 Queue를 인쇄 할 때 고유 한 문자열 대신 동일한 문자열이 연속으로 나타납니다. 내가 뭘 잘못하고 있니? 개체를 큐 또는 스택에 추가 할 때마다 개체의 새 인스턴스를 만들어야합니까? Objective-C
이
목표 -C 이하main.m &에서 코드 tokenObject.h 하다에 main.m :
int main (int argc, const char * argv[])
{
CHCircularBufferQueue *tokenQueue = [[CHCircularBufferQueue alloc] init];
tokenObject *myTokenObject = [[tokenObject alloc] init];
TK currentToken;
initScanner();
scanToken();
[myTokenObject setTokenName:getToken() ];
currentToken = getTkrep();
int x=0;
while (x<5/*currentToken != TK_EOF*/) {
//printf("line number: %d, token value: %d, token string: %s\n", getLinenumber(), getTkrep(), getToken()); /* replace line with "add to queue" */
scanToken();
myTokenObject.tokenName = getToken();
[tokenQueue addObject:myTokenObject];
myTokenObject = [[tokenObject alloc] init];
currentToken = getTkrep();
x = x+1;
}
while(x != 1){
printf("%s", [[tokenQueue firstObject] tokenName]);
[tokenQueue removeFirstObject];
x = x-1;
printf("%i", x);
}
return 0;
}
tokenObject.h :
@interface tokenObject : NSObject{
char *tokenName;
}
@property (assign) char* tokenName;
@end
무언가를 추가 할 때마다 새 개체를 만들 필요는 없지만 사용자 지정 개체에 tokenName 포인터가 하나만 있기 때문에 고유 한 개체가 필요합니다. (왜 NSString 객체를 추가하지 않을까요?) –
'getToken'은 정확히 무엇을할까요? 해제를 담당하는 새로 할당 된 문자열을 반환합니까? 아니면 내부적으로 유지하고 수정할 수있는 것입니까? 즉, NSString을 왜 사용하지 않는지에 대한 두 번째 Licks의 질문도 있습니다. – nil
나는 서브 클래 싱하는 대신 NSString을 구현하려고 시도하고 있는데, 정확히 구현할 수 있을까요? getToken()은 char *를 반환하고 교수가 제공 했으므로 코드의 해당 부분을 수정할 수 없습니다. NSString에 대한 setter 함수는 무엇입니까? – kabirpong