This Question have 2 answers right now.

For in loop in swift with dynamic range

By : Duarte
Source: Stackoverflow.com
Question!

In swift, I always used the C notation like:

for (var i=0;i<mybuffer.count;i++) {}

Now in 3.0 this can not be used. The problem is that using

for i in..<mybuffer.count {} 

will not check the value of mybuffer.count for each iteration, which leads to index out of range, in case the mybuffer.count changes.

Is there a way to overcome this?

I could use a while statement, but I guess that there must be a way to have a for in loop with a dynamic range, right?

By : Duarte


Answers

One alternative would be to use a while loop:

var i = 0
while i < mybuffer.count {
    // do stuff
    i += 1
}
By : rmaddy


How about call updateJSON in callback of updateHash

updateHash: function updateHash(){
    something.getUpdateHash(function(data){
        var hashR = data.responseJSON;
        if(hashR!==this.state.hash){
            this.updateJSON(hashR)
        }
    }.bind(this));
},

updateJSON: function(hash){
    //This is an asynchronous call
    something.getJSON(function(data){
        var stepJ = jQuery.parseJSON(data.responseJSON);
        this.setState({json: stepJ,hash});
    }.bind(this));
},
By : Guigui


Video about For in loop in swift with dynamic range