show a button on getting json response equal to a string like @“unapproved”

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

Here I am fetching some clips from API. And i want to display a button when the status of clip is unapproved so in json response currently i am showing "0" for "unapproved". so now want to display a button when
staus is "0". i got below code from stack only and is working fine upto [park enumerateObjectUsingBlock ..] i am getting response as state equal to "1" and of some clips "0". but my code doesn't work after NSSring *title it shows title as nil so tell me how to get the value in *title

 // log debugger response -- park=(__NSArrayM) *    @"3 objects"                              [0]    (MyVideos *)    //  _clip_name=(NSTaggedPointerString) *   @"Kotak1"         // _state=(NSTaggedPointerString) *    @"1"      // _clip_image_path=(__NSCFString *)@"1EKNA1464617788.jpg"    // status = (NSString *)nil        NSURL *url = [NSURL URLWithString:@"task=webapi.getJClips"];   park = [jsonLoader videosFromJSONFile:url];  [park enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {   // if i add a breakpoint at above line and on this line it give me response state = "1" but on below line it crashes   //here it crashes on below line in debugger it shows title nil    NSString *title = object[@"state"];    }    // here below is code for showing hidden button after getting response "0"   if ([title isEqual:@"0"]) {          //If so, get the correct button from the enumerate idk and set hidden NO and text = title   //      UIButton *button = [buttonArray objectAtIndex:idx];    //     button.hidden = NO;     //    button.text = title;     } 

回答1:

  1. You are declaring NSString *title within a block, so this not going to work outside of the block.
  2. if([title isEqual:@"0"]) is comparing object pointers and should be if([title isEqualToString:@"0"]), to compare an NSString to another NSString
  3. Since the block is run independently, your title will not be set before the "if([title..." line is executed
  4. UI operations must be run on the main thread

So, after all of that, a better approach would be:

[park enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {     if([object[@"state"] isEqualToString:@"0"]) {         dispatch_async(dispatch_get_main_queue(), ^{             UIButton *button = [buttonArray objectAtIndex:idx];             button.hidden = NO;             button.text = title;          });     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!