Removing all CALayer's sublayers

Solution 1:

The simplest way to remove all sublayers from a layer is to set the sublayer property to nil:

rootLayer.sublayers = nil;

Solution 2:

The following should work:

for (CALayer *layer in [[rootLayer.sublayers copy] autorelease]) {
    [layer removeFromSuperlayer];
}

Solution 3:

Swift (short version):

layer.sublayers?.forEach { $0.removeFromSuperlayer() }

Solution 4:

[rootLayer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];