2017-10-28 111 views
0

我正在从Objective-C类调用函数。我从我的Swift类传递一个数组到一个Objective-C函数中。将Swift中的NSArray传递给Objective-C函数

这里是我的雨燕

var videosAutoCompletion:[String] = [] 

completeSearch.autocompleteSegesstions(searchText, self.tableView, self.videosAutoCompletion) 

Objective-C的功能

-(void)autocompleteSegesstions : (NSString *)searchWish :(UITableView*)table :(NSArray*)stringArray 

里面的Objective-C的功能,我有一个块

dispatch_sync(dispatch_get_main_queue(), ^{ 
     self.ParsingArray = [[NSMutableArray alloc]init]; //array that contains the objects. 
     for (int i=0; i != [jsonObject count]; i++) { 
      for (int j=0; j != 1; j++) { 
       //NSLog(@"%@", [[jsonObject objectAtIndex:i] objectAtIndex:j]); 
       [self.ParsingArray addObject:[[jsonObject objectAtIndex:i] objectAtIndex:j]]; 
       //Parse the JSON here... 
       //NSLog(@"Parsing Array - %@",self.ParsingArray); 

       stringArray = [self.ParsingArray copy]; 
       [table reloadData]; 

      } 
    } 

    }); 

我得到这个代码以下行错误。

变量没有分配(缺少__block类型说明符) 在行

stringArray = [self.ParsingArray copy]; 
+0

至少有两个StackOverflow问题可以解决这个错误。这与Swift无关;它与使用“块”(Swift中的“闭包”的Objective-C名称)有关。 [在块中将变量分配给块外的变量](https://stackoverflow.com/q/7962721/1107226)和[“变量不可分配(缺少__block类型说明符)”方法中使用变量时出错在方法块中声明](https://stackoverflow.com/q/36573957/1107226) – leanne

回答

0

您正在尝试块内修改stringArray功能参数的值,所以你应该告诉给编译器。你可以尝试dispatch_sync呼叫这样的权利之前制作副本:

__block NSArray *stringArrayCopy = [stringArray copy]; 

和该块中使用复制。

这将消除编译器错误,但请注意,如果您打算通过在ObjC方法内设置stringArray参数来更改Swift videosAutoCompletion阵列,则需要更改方法。设置stringArray = [self.ParsingArray copy];只对本地的 autocompleteSegesstions::: 函数有效。