2017-10-28 2 views
0

Objective-C 클래스에서 함수를 호출하고 있습니다. Swift 클래스의 배열을 Objective-C 함수로 전달합니다. 여기 Swift에서 NSArray를 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]; 

      } 
    } 

    }); 

나는이 무엇입니까 있던 목표 - C 함수 내부 스위프트

var videosAutoCompletion:[String] = [] 

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

목표 - C 기능

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

내 코드입니다 다음 행에 대한 오류.

변수 (실종 __block 유형 지정자) 할당 할 수없는 라인에서

stringArray = [self.ParsingArray copy]; 
+0

이 오류를 해결하는 StackOverflow 질문이 두 개 있습니다. 그것은 스위프트와 관련이 없습니다. "블록"(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::: 기능에서만 로컬로 적용됩니다.