Ich habe versucht, eine benutzerdefinierte Iterator zu schaffen, die Wrapper abcContainer
über Rohdaten-Klasse gibt abc
Swift 3 - Sequenz vieldeutig, ohne mehr Kontext
// 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
}
}
Der Punkt wäre, dass das Wörterbuch Instanzen abcContainer
anstatt nur die zurückkehren einfach roh abc
Klasse.
Ich wollte das Sequenz-Protokoll verwendet die Umwandlung automatische zu machen, aber ich war nicht in der Lage, die [String:abc]
in [String:abcContainer]
automatisch wie folgt zu transformieren:
// 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!);
}
}
ich wahrscheinlich hier etwas sehr Grundlegendes fehlt bin. Wenn ich versuche, die Sammlung wie folgt zu verwenden:
var dict : [String:abc] = [String:abc]();
dict["abba"] = abc("John Smith");
for (key,value) in abcCollection(dict) {
print(key, value.getName());
}
ich Fehler: Expression Typ „abcCollection“ ist mehrdeutig, ohne mehr Kontext
jemand Idee Hat, wie es funktioniert? Was fehlt? Ich habe das Gefühl, dass diese Antwort die Informationen, die ich brauche ...
Swift 2 to 3 Migration for Swift Sequence Protocol
Danke! Schöne subtile Punkte. –