0

I have an api class, which make api calls and most of the calls are the same but some returns something (JSON data) and others don't. Also there is a call to check only if a param is registered and return true or false. So at this moment we have three api calls, with exactly the same code but little differences or returns values.

these are two examples, the first one will set an internal class property with JSON data, the second will return true or false:

///Call an api with the given url param
private func call(){
    let request = NSMutableURLRequest(URL: NSURL(string: userApiCallUrl)!)
    request.HTTPMethod = "POST"

    request.HTTPBody = postParam.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
        guard error == nil && data != nil else {
            // check for fundamental networking error
            print("error=\(error)")
            return
        }
        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
            // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)

        self.setJsonData(responseString!)
    }
    task.resume()
}
///Call to check if UDID exist. Returns true or false. For the moment returns true allways
private func callToCheck()->Bool{
    let request = NSMutableURLRequest(URL: NSURL(string: userApiCallUrl)!)
    request.HTTPMethod = "POST"

    request.HTTPBody = postParam.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
        guard error == nil && data != nil else {
            // check for fundamental networking error
            print("error=\(error)")
            return             }
        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
            // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")
    }
    task.resume()
    return true
}

The code is almost identical. My question is what is the best practise to do this? As far as I know, duplicate code is not the best thing to do. But right now I don't see clearly another way.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alfro
  • 1,524
  • 2
  • 21
  • 37
  • Make the return type generic. Like [AnyObject or Any](http://stackoverflow.com/questions/25809168/anyobject-and-any-in-swift) – NSNoob May 12 '16 at 07:39
  • If the method is same, but the return types are different then return "Object" and check the instance of the desired class in that object instance – Abhishek May 12 '16 at 07:40

2 Answers2

0

Use an extra argument when you call function let's say of type int. Then switch int (case 1 will return some data, case2 other kind of data). As a general advice. In your final functionreturn use a NSMutableDictionary,assign initial int as value for a key, the result of each case as other key with value of result. After all you will known in your call back witch case you treat and which data you use.

ares777
  • 3,590
  • 1
  • 22
  • 23
0

If you really want two functions, change the first one to

private func call(){
    callToCheck ()
}

And I suppose one "return" should be "return false".

gnasher729
  • 51,477
  • 5
  • 75
  • 98