Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift字典的自定义序列_Ios_Swift_Generator_Sequence_Ios8 - Fatal编程技术网

Ios Swift字典的自定义序列

Ios Swift字典的自定义序列,ios,swift,generator,sequence,ios8,Ios,Swift,Generator,Sequence,Ios8,我有一个容器类,它有一个底层字典。我已经实现了这个类的下标来访问底层字典的成员。现在,我尝试在这个类上创建一个序列,这样我就可以通过在类实例本身上使用“for in”循环来迭代底层字典的所有元素。我一直在为Swift字典寻找一些序列的例子,但找不到任何能很好解释这些东西的东西。我已经看到了一些用于Swift数组的自定义序列示例,但没有一个用于Swift字典。如果有人能解释我是如何做到这一点的,我将不胜感激。下面是该类的代码(由于我不确定从哪里开始,还没有序列代码) (注意:我重新思考了这个问题—

我有一个容器类,它有一个底层字典。我已经实现了这个类的下标来访问底层字典的成员。现在,我尝试在这个类上创建一个序列,这样我就可以通过在类实例本身上使用“for in”循环来迭代底层字典的所有元素。我一直在为Swift字典寻找一些序列的例子,但找不到任何能很好解释这些东西的东西。我已经看到了一些用于Swift数组的自定义序列示例,但没有一个用于Swift字典。如果有人能解释我是如何做到这一点的,我将不胜感激。下面是该类的代码(由于我不确定从哪里开始,还没有序列代码)

注意:我重新思考了这个问题——通过编辑页面的原始答案…)

Swift有一个generic
GeneratorOf
类型,可用于创建生成器。您只需提供一个在初始值设定项中返回下一个值的闭包:

class STCQuestionList : SequenceType {

    private var questionDict: [String : STCQuestion] = [ : ];

    subscript(key : String?) -> STCQuestion? {
        get {
            if (key != nil) {
                return self.questionDict[key!];
            }
            return nil;
        }
        set(newValue) {
            if (key != nil) {
                self.questionDict[key!] = newValue;
            }
        }
    }

    /// Creates a generator for each (key, value)
    func generate() -> GeneratorOf<(String, STCQuestion)> {
        var index = 0
        return GeneratorOf<(String, STCQuestion)> {
            if index < self.questionDict.keys.array.count {
                let key = self.questionDict.keys.array[index++]
                return (key, self.questionDict[key]!)
            } else {
                return nil
            }
        }
    }
}
类STCQuestionList:SequenceType{
私有变量questionDict:[字符串:STCQuestion]=[:];
下标(键:字符串?->STCQuestion{
得到{
如果(键!=零){
返回self.questionDict[key!];
}
返回零;
}
设置(新值){
如果(键!=零){
self.questionDict[key!]=newValue;
}
}
}
///为每个(键、值)创建一个生成器
func generate()->GeneratorOf{
var指数=0
返回发生器{
如果索引
如果你不关心顺序,你就不能调用dictionary的相同方法,或者让你的类成为dictionary的子类吗?例如:

func generate() -> GeneratorType {
    return self.questionDict.generate()
}


func next() -> (String, STCQuestion)? {
    return self.questionDict.next()
}

如果我理解正确的话,那就直接在generate上转发怎么样

func generate() -> DictionaryGenerator<String, STCQuestion> {
    return questionDict.generate()
}

您关心顺序,还是只想像普通字典一样在循环中使用类?如果你想要排序,你应该描述它是什么。@sanz-在这一点上,我不关心排序,只关心我可以成功地迭代字典中的所有值;
func generate() -> DictionaryGenerator<String, STCQuestion> {
    return questionDict.generate()
}
// Playground - noun: a place where people can play

import Foundation

class STCQuestion {
    let foo: String
    init(_ foo: String) {
        self.foo = foo
    }
}

class STCQuestionList : SequenceType {

    private var questionDict: [String : STCQuestion] = [ : ];

    subscript(key : String?) -> STCQuestion? {
        get {
            if key != nil {
                return self.questionDict[key!];
            }
            return nil;
        }
        set(newValue) {
            if key != nil {
                self.questionDict[key!] = newValue;
            }
        }
    }

    func generate() -> DictionaryGenerator<String, STCQuestion> {
        return questionDict.generate()
    }
}


var list = STCQuestionList()
list["test"] = STCQuestion("blah")
list["another"] = STCQuestion("wibble")
list["third"] = STCQuestion("doodah")

for (key, value) in list {
    println("Key: \(key) Foo: \(value.foo)")
}

// Output:
// Key: test Foo: blah
// Key: another Foo: wibble
// Key: third Foo: doodah