2016-08-23 10 views
1

Swift에서 performSelector를 대체 할 수있는 것은 무엇입니까? 제발 아래 코드를 제안하십시오. DFURLPrepare 클래스 메서드에서 주석 처리 한 문을 업데이트해야합니다.Swift에서 performSelector의 필수 대안

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

class DFURLPrepare { 
    func sendRequest (delegate:AnyObject, url : String, dictParams: Dictionary <String, String>,successMethod: String?, errorMethod:String?){ 
     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{ 
        /// *************Here as in objeactive C **************// 
        /// [self.delegate performSelector:successMethod withObject:data]]; 
        ///// What would be code at place of above statement 
      } 
     } 
    } 
} 

}

+0

체크 무엇을 델리게이트 타입은 그것이며 그냥 delegate에 대한 메소드를 실행한다. 이상한 선택기가 아니라 j ust method –

답변

1

당신은 NSObject -descendants에 대한 performSelector(_:withObject:)를 사용할 수 있습니다

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) 
       } 
      } 
     } 
    } 
} 

그러나 폐쇄를 사용하여 많은 스위프트 프로그래머에 의해 더욱 바람직한 방법이 될 것이다 :

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) 
       } 
      } 
     } 
    } 
}