Type of NSLayoutAnchor

137 views Asked by At

No matter what I try I can't get the variations of this to work... What am I not getting?

func anchorType(for string:String) -> NSLayoutAnchor<AnyObject> {

    switch string {
    case "x":
        return NSLayoutXAxisAnchor
    case "y":
        return NSLayoutYAxisAnchor
    default:
        return NSLayoutDimension
    }
}

There seems to be no way to determine the class of an anchor.. :(

1

There are 1 answers

2
R. Lin On

I don't think this is exactly what you're looking for but I think something like this should work:

func anchorType(for string:String) -> AnyClass {

    switch string {
    case "x":
        return NSLayoutXAxisAnchor.self
    case "y":
        return NSLayoutYAxisAnchor.self
    default:
        return NSLayoutDimension.self
    }
}

I'm not too familiar with this area but I would think that the reason you can't return NSLayoutAnchor is because that class is a generic class while it's subclasses are not.