2014-11-16 5 views
1

약한 객체 참조를 저장하기위한 래퍼 클래스를 생성했습니다. 내부에 유효한 참조가없는 객체를 제거하고 싶습니다.스위프트 필터 함수로 인해 컴파일 오류가 발생합니다.

class Weak<T: AnyObject> { 
    weak var value : T? 
    init (value: T) { 
     self.value = value 
    } 
} 

protocol CSLocationServicesDelegate : class{ 

} 


class conformance : CSLocationServicesDelegate{ 

} 


class Dispatcher{ 
    var dispatchArray = Array<Weak<CSLocationServicesDelegate>>() 

    func add(delegate : CSLocationServicesDelegate!){ 
     dispatchArray.append(Weak(value: delegate)); 
    } 
    func remove(obj : CSLocationServicesDelegate!){ 
     dispatchArray.filter { (weakRef : Weak<CSLocationServicesDelegate>) -> Bool in 
      return weakRef.value != nil; //change this line to "return true" and it works!!! 

     } 
    } 
} 

그러나 Xcode 컴파일이 실패하여 오류가보고되지 않으며 오류가 전혀 표시되지 않습니다. 나는 Xcode가 내가하고 싶은 것을 이해할 수 없다는 잘못된 방식으로 언어를 사용하고 있다고 의심합니다. 주석을 주석이 말하는대로 변경하면 주석이 작동합니다.

내가 원하는 것을 달성하는 데 도움을 줄 수있는 사람이 있습니까?

답변

1

코드에 아무런 이상이 보이지 않지만 컴파일 할 때 컴파일러가 충돌합니다. 분명히 reported to Apple이어야합니다.

한 가지 해결 방법은 Weakclass 대신 으로 간단히 선언하는 것입니다. 이것은 잘 컴파일됩니다.

struct Weak<T: AnyObject> { 
    weak var value : T? 
    init (value: T) { 
     self.value = value 
    } 
} 

protocol CSLocationServicesDelegate : class{ 

} 


class conformance : CSLocationServicesDelegate{ 

} 


class Dispatcher{ 
    var dispatchArray = Array<Weak<CSLocationServicesDelegate>>() 

    func add(delegate : CSLocationServicesDelegate!){ 
     dispatchArray.append(Weak(value: delegate)); 
    } 
    func remove(obj : CSLocationServicesDelegate!){ 
     dispatchArray.filter { (weakRef : Weak<CSLocationServicesDelegate>) -> Bool in 
      return weakRef.value != nil 
     } 
    } 
} 
+0

우수한 찾기. 넌 나를 구했다. – csotiriou

관련 문제