2009-11-17 73 views
1

我有我希望成爲一個非常簡單的問題。我非常簡單地試圖將char轉換爲小寫版本。這裏是我的代碼:整型在處理可可中的字符串中鑄造

- (IBAction)click:(id)sender { 
    [outputLabel setText:[inputField text]]; 
    NSString* textFieldString = [inputField text]; 
    NSLog(@"String is %@", textFieldString); 
    int textFieldLength = textFieldString.length; 
    UniChar* currChar = [textFieldString characterAtIndex:0]; 
    NSLog(@"First char is %c", currChar); 
    NSLog(@"Length is %i", textFieldLength); 
    //currChar = [currChar lowercaseString]; 
    //NSLog(@"Lowercase char is %c", currChar); 
} 

它給了我下面的錯誤:

Initialization makes pointer from integer without a cast on the line:
NSString* currChar = [textFieldString characterAtIndex:0];

然而,在文檔中觀察時,它說,characterAtIndex:返回一個字符,而不是一個整數的方法。是什麼賦予了?

回答

4

characterAtIndex回報unichar這是一個unsigned short。該行應

unichar currChar = [textFieldString characterATtIndex:0]; 

另外,你可以使用lowercaseString讓整個NSString的小寫:

NSString *lowercase = [textFieldString lowercaseString]; 
2

這對於函數定義:

- (unichar)characterAtIndex:(NSUInteger)index 

甲單字符是一個C數據類型,並且不是的NSString *。您需要做

unichar currChar = [textFieldString characterAtIndex:0]; 

您收到的消息是由於它試圖將unichar的整數值轉換爲指針值。

編輯:

順便說一句,如果你真的需要一個NSString的從最簡單的方法很可能是(幹碼)回:

NSString *currChar = [textFieldString substringWithRange:NSMakeRange(0,1)];