2014-09-24 19 views
-2
- (IBAction)convert_button:(id)sender { 
NSString * binary_input = _binary_field.text; 
if(binary_input!=NULL) 
{ 
    int count, total, i, j, tmp; 
    total = 0; 
    count = binary_input.length; 

    for (i = 0; i <= count; i++) { 
     if (binary_input[count-i] == '1') {//error here saying Expected method to read array element not found of object type 'NSString *' 
      tmp = 1; 
      for (j = 1; j < i; j++) 
       tmp *= 2; 
      total += tmp; 
     } 
    } 
    long decimal_value = strtol([binary_input UTF8String], NULL, 2); 
    NSString *resultString = [[NSString alloc] 
           initWithFormat: @"%li", decimal_value]; 
    _decimal_output.text = resultString; 
} 

}預計方法來讀取數組元素沒有找到對象類型「的NSString *」

我得到一個錯誤在我的if語句錯誤代碼評論說,讀數組元素不期望方法找到的對象類型'NSString *'

我是新來的objective-c,主要做了C++,任何幫助在如何解決錯誤的讚賞,該邏輯對我有意義。

+0

你不能索引一個NSString。請查閱文檔以瞭解如何訪問個人角色。 – 2014-09-24 01:29:36

+1

改爲使用'[binary_input characterAtIndex:index]'。 – KudoCC 2014-09-24 01:34:43

+0

解決了問題KudoCC – user3746000 2014-09-24 02:57:13

回答

0
for (i = 0; i <= count; i++) { 
    if (binary_input[count-i] == '1') 

在循環的第一次迭代中,您將尋找位於數組邊界外的點-1中的元素。

+0

您還會在數組中的最後一個元素處發生崩潰。你會想要去'我 Oren 2014-09-24 01:31:50

+0

那是怎麼回事?我從0開始,我的計數開始於我的字符串有多長。 – user3746000 2014-09-24 02:20:15

+0

數組基於索引0爲基礎。你有它的方式,如果你的數組中有1個項目,那麼你的循環從0到(和INCLUDING)1。數組中的最後一個項目將比它的計數小1。 – Oren 2014-09-24 03:03:24

相關問題