Alternative to Performselector in Swift

Required alternative for performSelector in Swift

You can use performSelector(_:withObject:) for NSObject-descendants:

class Login: NSObject { //<-Login needs to be a subclass of `NSObject`.
func loginRequest(url:String, dictParams: Dictionary <String, String>)
{
let urlPrepare = DFURLPrepare()
urlPrepare.sendRequest(self, url: url, dictParams: dictParams, successMethod: #selector(getDefaultItemsResponse), errorMethod: nil)
}

func getDefaultItemsResponse(data: NSData?) {

}
}

class DFURLPrepare {
func sendRequest(delegate: NSObject, url : String, dictParams: Dictionary <String, String>,successMethod: Selector, errorMethod: Selector){
//Type of `delegate` needs to be `NSObject`.
let networkObj = Network()
let requestResource = Resource(url:url,paramdict: dictParams)
networkObj.load(requestResource){ data, response, error in
if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if statusCode == 200 && data != nil{
delegate.performSelector(successMethod, withObject: data)
}
}
}
}
}

But using closure would be a more preferred way by many Swift programmers:

class Login {
func loginRequest(url: String, dictParams: Dictionary <String, String>) {
let urlPrepare = DFURLPrepare()
urlPrepare.sendRequest(url, dictParams: dictParams, successHandler: getDefaultItemsResponse, errorHandler: nil)
}

func getDefaultItemsResponse(data: NSData?) {

}
}

class DFURLPrepare {
func sendRequest(url : String,
dictParams: [String: String],
successHandler: ((NSData?)->Void)?,
errorHandler: ((NSError?)->Void)?
) {
let networkObj = Network()
let requestResource = Resource(url:url,paramdict: dictParams)
networkObj.load(requestResource){ data, response, error in
if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if statusCode == 200 && data != nil{
successHandler?(data)
}
}
}
}
}

Swift alternative to performSelectorOnMainThread

This simple C-function:

dispatch_async(dispatch_get_main_queue(), {

// DO SOMETHING ON THE MAINTHREAD
self.tableView.reloadData()
})

What about launching your function with:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

loadAlbums()

})

in viewDidLoad()?

What is alternative to performSelector in swift?

You should try this:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.showMainView()

Alternative method for performSelector in SKAction in Swift

Try this out

class MyScene: SKScene {

func doAction() {
let releaseBalls = SKAction.sequence([
SKAction.runBlock(self.createMyNode),
SKAction.waitForDuration(1)
])
// run action
}

func createMyNode() {
// create the nodes
}
}

Alternative to performSelector:withObject:afterDelay: for animating views consequantially

You don't specify how you exactly perform the animations themselves, but with UIView animation blocks you can also specify a delay.(animateWithDuration:delay:options:animations:completion:).

Alternatively you can use NSTimerfor performing delays.

But for a detailed answer you should provide more details on how you perform your animation.

Update

Using animation blocks is very straightforward...In the completion block (see code below) you can start the follow up animation (for series of animations). For parallel animation processing you just start multiple blocks of animations...there are different messages. If you don't need to handle the animation end, you can use the ones without completion block.

Here is an example:

[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{

// perform animation code here

} completion:^(BOOL finished) {

// This code is performed after the animation is completed. E.g. you can start a new animation here
// (for serial animation processing):

}];


Related Topics



Leave a reply



Submit