2017-12-21 5 views
0

내가 코드Swift.ArraySlice를 Swift.Array로 변환하는 방법?

if(currentArgument == "") 
    { 
     numberOfArgInCurrentFunction -= 1 
     var lastExp = expressionStack.last! 
     lastExp["ArgList"] = (lastExp["ArgList"] as! [String]).dropLast() 
     expressionStack.remove(at: expressionStack.count - 1) 
     expressionStack.append(lastExp) 
     if(lastExp.count > 0) 
     { 
      let argList:[String:Any] = expressionStack.last! 
      currentArgument = (argList["ArgList"] as! [String]).last! // ***Error on this line 
     } 
    } 

아래에서 오류를 얻고있다 '는 Swift.Array'(0x107ef2538)에 유형 'Swift.ArraySlice'(0x107ef4b40)의 값을 캐스팅 할 수 없습니다.

지금 Swift.Array에 Swift.ArraySlice을 변환 할 수있는 방법이나

편집 -

나는 경우 중복를 들어 Swift.ArraySlice

에서 마지막 요소를 얻을 수있는 방법이 알고 싶어요 사용 울부 짖는 코드를 변환 할

let slice = argList["ArgList"] as ArraySlice //*** Cannot convert value of type 'Any?' to type 'ArraySlice<_>' in coercion because 
let array:[String] = Array(slice) 
currentArgument = array.last! 
+0

@MartinR 나는 당신이 언급 한 질문 다르다는 것을 설명하기 위해 질문을 편집 한, 만약 내가 잘못 알려 주시기 바랍니다 . –

답변

0

오류의 실제 이유는이 라인에

16,당신은 새 배열을 만들 필요가Array 아직 가 목적이 아니라 유형 ArraySlice을 재 할당하는

lastExp["ArgList"] = Array((lastExp["ArgList"] as! [String]).dropLast()) 

그리고 결코 이제까지 체크 문자열 및 수집 유형 공란은 .count == 0입니다. 이 isEmpty 특성을 최적화 된 그리고 스위프트 괄호에 if 표현을 포장하지 않습니다

if !lastExp.isEmpty { ... 
+0

"Swift의 괄호 안에'if' 식을 포함하지 않는 특별한 이유가 있습니까? –

+0

그것은 단지 중복입니다. Objective-C와는 달리 Swift에서는 필요하지 않습니다. 덜 입력하십시오. – vadian

관련 문제