简体   繁体   中英

What is the swift equivalent of makeObjectsPerformSelector?

In Objective-C I'm using this code to remove any sub-views:

[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

But how to use it in swift? I saw apple documentation to use that method in swift

func makeObjectsPerformSelector(_ aSelector: Selector)

but when I try it, I get an error: 'AnyObject[]' does not have a member named 'makeObjectsPerformSelector'

Are there any ways to remove sub-views in swift?

Updated for Swift 2.0 (Xcode 7)

Use forEach :

self.view.subviews.forEach { subview in
    subview.removeFromSuperview()
}

Or like this:

view.subviews.forEach { $0.removeFromSuperview() }

It only works on NSArray and NSMutableArray objects.

This will work:

let ar: NSArray = [obj1, obj2, obj3]
ar.makeObjectsPerformSelector("someSelector")

Note that if you have an Array<AnyObject> you can freely convert to NSArray and vise versa.

let anNSArray: NSArray = anArrayOfAnyObject
anNSArray.makeObjectsPerformSelector( "someSelector")

从Xcode 7开始,在Swift中可以使用完整的performSelector方法系列,包括用于NSArray makeObjectsPerformSelector()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM