없는 모호한 순서 나는 원시 데이터 클래스를 통해 래퍼 abcContainer
을 반환하는 사용자 정의 반복자를 만들려고 abc
스위프트 3 - 더 상황
// raw data class
class abc {
var name : String = "";
init(_ value : String) {
name = value;
}
}
// with container, only "name" is to be visible
class abcContainer {
private var _abc : abc;
init(_ obj : abc) {
_abc = obj;
}
// + extra methods here
func getName() -> String {
return _abc.name
}
}
점은 사전 abcContainer
의 인스턴스를 반환하는 대신 단지 것이 될 것이다 일반 raw abc
클래스입니다.
[String:abc]
[String:abcContainer]
로 변환 할 수 없습니다 : 아마 여기에 아주 기본적인 뭔가를 누락
// the iterator is implemented just iterating the inner basic dict
// but wrapping the result value as abcContainer
class abcIterator : Sequence, IteratorProtocol {
private var __source : [String:abc]?;
var index = 0
var myIterator : DictionaryIterator<String, abc>;
init(_ ctxArray: [String:abc]) {
self.__source = ctxArray
index = 0;
myIterator = (__source?.makeIterator())!
}
func next() -> abcContainer? {
let nextItem = myIterator.next();
if(nextItem != nil) {
return abcContainer((nextItem?.value)!);
}
return nil;
}
}
// this was supposed to be the wrapper over the collection
class abcCollection : Sequence {
private var __source : [String:abc]?;
init(_ list: [String:abc]) {
self.__source = list
}
func makeIterator() -> abcIterator {
return abcIterator(self.__source!);
}
}
. 나는이 같은 컬렉션을 사용하려고하면 :
var dict : [String:abc] = [String:abc]();
dict["abba"] = abc("John Smith");
for (key,value) in abcCollection(dict) {
print(key, value.getName());
}
을 나는 오류가 발생합니다 : 표현 형식 "abcCollection는"
사람이 작동하는 방법을 생각을 가지고 있는가보다 컨텍스트없이 모호? 없어진 물건 있어요? 난 당신이 직접 단지 같은 링크 대답 할 좋아 반복자를 정의 할 수,
Swift 2 to 3 Migration for Swift Sequence Protocol
고마워요! 좋은 미묘한 점. –