Search code examples
swiftnssetoptional-values

Swift Optional values out of an NSSet


Self.vertices is an NSSet. I'm having trouble getting the Vertex value out of the optional

The code below crashes in playgrounds.

func getVertex (x: Double, y: Double,z: Double) -> Vertex?
{

    for v : Vertex! in self.vertices {

        if v.isEqualTo(x, y: y, z: z) {
            return v
        }
    }
    return nil

}

Solution

  • that may be helpful:

    let set: NSSet = // ...
    
    for object : AnyObject in set {
        if let vertex = object as? Vertex {
            // do the main course
        }
    }