Cast array element type in loop

In the delegate method, I am returning a "custom object type result array" and I want to iterate over the elements of the array. I am doing the following now and it works

for result in results {
    if result is XYZClass {     
        //This Works!    
    }
}

      

Is there a way to print objects in a for loop to avoid writing two lines? Does it make it fast? Used to make it pretty easy in Objective - C

for (XYZClass *result in results) {

}

      

However, I have had no success with Swift. I've tried explicit casting with no luck.

for result as XYZClass in results {
    //ERROR: Expected ‘;’ infor’ statements
}

for result:AGSGPParameterValue in results {
    /* ERROR: This prompts down cast as 
    for result:AGSGPParameterValue in results as AGSGPParameterValue { }
    which in turn errors again "Type XYZClass does not conform to Sequence Type"
*/
}

      

Any help is appreciated

+3


source to share


1 answer


Try the following:



for result in results as [XYZClass] {
    // Do stuff to result
}

      

+7


source







All Articles