2013-10-25 47 views
-1

我試圖開發出Funcationality,使收藏夾 最喜歡的是工作完美的,但是當我按一個或下一個按鈕它崩潰了...如何檢查NsmutableArray中可用的當前字符串..?

我不知道如何來檢查當前的消息是存在於FavouriteArray .. ? if is Exist Then FavouriteButton images更改爲Favorite-ON.png,否則爲其圖像Favorite-OFF.png ..

我的代碼在下面================= =

// Adding Current Message to Favourite Array 

    - (IBAction)FavouritebtnClick:(id)sender //Favourite Button Work Perfectly 
    { 

     Make_Fav_Text = [NSString stringWithFormat:@"%@",[[[TabBarTutorialAppDelegate shareDelegate].level_array valueForKey:@"SMS"] objectAtIndex:row_no]]; 

     NSLog(@"Make Favourite TExt====%@",Make_Fav_Text); 

     [DatabaseFiles InsertFav:[NSString stringWithFormat:@"%@",Make_Fav_Text]];//insert to database 

     [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal]; 
    } 

//檢查當前消息中FavouriteArray

是可用
-(void)Check_Fav_Available 
{ 
    for (NSString *Myfav in Check_Fav_Avail) 
    { 
     NSLog(@"MYFAV===%@",Myfav); 
     if ([Myfav isEqual:sms.text]) 
     { 
      [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal]; 
      NSLog(@"Found Favourite"); 

      //break; 
     } 
     else{ 
      NSLog(@"NOt Found"); 
      [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-copy-iphone"] forState:UIControlStateNormal]; 
     } 
    } 

} 

//Next Button 

- (IBAction)Nextbtn:(id)sender 
{ 


    row_no=row_no+1; 
    lblno=row_no; 
    lblno=lblno+1; 

    [self Check_Fav_Available]; //Check Current Message in Favourite Array; 

    if (row_no==[TabBarTutorialAppDelegate shareDelegate].level_array.count) 
    { 
     row_no=row_no-1; 
//  lblno=lblno-1; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is last SMS" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
     NSLog(@"this is last"); 
     NSLog(@"row no is %d",row_no); 
    } 
    else 
    { 
     if (row_no <=4) 
     { 
      sms.text=[[[TabBarTutorialAppDelegate shareDelegate].level_array objectAtIndex:row_no] objectForKey:@"SMS"]; 
      NSString *str = [NSString stringWithFormat:@"%d - %d",lblno,[TabBarTutorialAppDelegate shareDelegate].level_array.count]; 
      no_lbl.text=str; 

      NSLog(@"row no is %d",row_no); 
      [self Check_Fav_Available]; 


     } 
     else 
     { 
      UIAlertView *Purchasealert = [[UIAlertView alloc] initWithTitle:@"Purchase Category" message:@"Purchase to Enjoy More" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy Message", nil]; 
      Purchasealert.tag=1710; 
      [Purchasealert show]; 

      [self gostore]; 
      NSLog(@"this is last"); 
      NSLog(@"row no is %d",row_no); 

     } 
    } 

} 


//Previous Button 

- (IBAction)Previousbtn:(id)sender 
{ 

    //[FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-copy-iphone.png"] forState:UIControlStateNormal]; 

    row_no=row_no-1; 
    lblno=lblno-1; 

    if (row_no<0) 
    { 
     row_no=row_no+1; 
     lblno=lblno+1; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is First SMS" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
     NSLog(@"this is last"); 
    } 
    else 
    { 
     [self Check_Fav_Available]; //Check Favourite available 

     sms.text=[[[TabBarTutorialAppDelegate shareDelegate].level_array objectAtIndex:row_no] objectForKey:@"SMS"]; 

     NSString *str = [NSString stringWithFormat:@"%d - %d",lblno,[TabBarTutorialAppDelegate shareDelegate].level_array.count]; 
     no_lbl.text=str; 
     [self Check_Fav_Available]; 

     NSLog(@"arr is %d",[TabBarTutorialAppDelegate shareDelegate].level_array.count); 
     NSLog(@"row no is %d",row_no); 
    } 

} 
+0

什麼是Crash Exception ..? –

+0

使用'isEqualToString'與字符串綁定在數組中或不使用isEqual –

+0

@kumarKI:***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [NSConcreteMutableAttributedString countByEnumeratingWithState:objects:count:]:無法識別的選擇器發送到實例0xaa62e90' ***第一次拋出調用堆棧: – Saytovishal

回答

0

試試這個代碼.....

-(void)Check_Fav_Available 
    { 

      if (!([Check_Fav_Avail indexOfObject:sms.text]==NSNotFound)) 
      { 
       [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal]; 
       NSLog(@"Found Favourite"); 

       //break; 
      } 
      else 
       { 
       NSLog(@"NOt Found"); 
       [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-copy-iphone"] forState:UIControlStateNormal]; 
      } 


    } 
+0

@Saytovishal什麼是崩潰日誌? – Raon

+0

:由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:' - [NSConcreteMutableAttributedString countByEnumeratingWithState:objects:count:]:無法識別的選擇程序發送到實例0xaa62e90' – Saytovishal

+0

: - 謝謝很多Mr.Raon ....我已經使用你的代碼爲你建議...及其作品完全... – Saytovishal

0

改變您的代碼以

for (NSString *Myfav in Check_Fav_Avail){ 
    NSRange favRange = [Myfav rangeOfString:sms.text options:NSCaseInsensitiveSearch]; 

    if (favRange.location != NSNotFound) { 
     NSLog(@"Found"); 
    } else { 
     NSLog(@"Not found"); 
    } 
} 
+0

: - 感謝兄弟...我會立即嘗試.. – Saytovishal

+0

@Touki:其工作,但它只顯示最後一個對象和Remainig對象沒有找到..請幫助 – Saytovishal

+0

它只發現陣列的最後一個對象..關於其他...請幫助... – Saytovishal

0

使用此來比較...

if ([Myfav isEqualToString:sms.text]) 
     { 
      [FavouriteBtnChange setImage:[UIImage imageNamed:@"FAv_On-iphone.png"] forState:UIControlStateNormal]; 
      NSLog(@"Found Favourite"); 

      //break; 
     } 

這可能是錯的..

Make_Fav_Text = [NSString stringWithFormat:@"%@",[[[TabBarTutorialAppDelegate shareDelegate].level_array valueForKey:@"SMS"] objectAtIndex:row_no]]; 

相反,使用此...

Make_Fav_Text = [NSString stringWithFormat:@"%@",[[[TabBarTutorialAppDelegate shareDelegate].level_array objectAtIndex:row_no] valueForKey:@"SMS"]]; 
+0

: - 沒有它的工作完美加載正確的價值每次...但仍然即時goint嘗試,因爲你建議...謝謝 – Saytovishal

+0

: - 我已經使用代碼你建議..它找到最喜歡的,但是當我按Nextbutton然後它的崩潰.. – Saytovishal