嘗試將NSString轉換爲大寫時,出現線程斷點錯誤。 我想將問題的輸入設置爲大寫字母,以便用戶可以鍵入no,no,nO,並且仍然會將其內部轉換爲NO。 我知道我可以讓它只需要一個0或1,但它更方便用戶這樣做。 我有調試模式,這將提供一些額外的數據,使這更簡單。 我在[string uppercaseString]行獲取線程斷點。 對於輸出,我第一次得到預期的調試器消息,但程序在顯示第二個消息之前停止。將NSString轉換爲大寫線程斷點
#define DEBUG 1
NSLog(@"Do you have an account already? YES or NO.");
char yesOrNo [20];
fgets (yesOrNo, sizeof yesOrNo, stdin);
int c;
while ((c = getchar()) != '\n' && c != EOF);
if (yesOrNo [strlen(yesOrNo) - 1] == '\n') { //In case the input string has # characters plus \n
yesOrNo[strlen(yesOrNo) - 1] = '\0';} //Plus '\0', the '\n' isn't added and the if condition is false
NSString *string = [NSString stringWithUTF8String:yesOrNo];
#ifdef DEBUG
NSLog(@"DEBBUGER MESSAGE: string == %@", string);
#endif
NSString *stringUppercase = [string uppercaseString];
#ifdef DEBUG
NSLog(@"DEBBUGER MESSAGE: stringUppercase == %@", stringUppercase);
#endif
爲什麼areyou使用'與fgets '*和*'getchar'? –
fgets用於用戶輸入,getchar是每次使用後刷新標準輸入的行的一部分。我從c-faq.com那裏得到了這個信息,對於像我這樣以前曾經在刷新標準輸入時遇到問題的人,我一次又一次地共享了這個信息。 –