Swift 3 URLSession.shared Ambiguous reference to member 'dataTaskwithcompletionHandler error bug
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
This Swift error usually is not a URLSession bug. It means the compiler cannot decide which overloaded dataTask method you intended to call, usually because the request value or closure type is too ambiguous. The fix is to make the input type explicit enough that Swift can select the correct overload.
Why the Call Becomes Ambiguous
URLSession has several overloads for dataTask, including versions that accept:
- '
URL' - '
URLRequest' - different completion-handler signatures
If Swift cannot tell whether the thing you passed is a URL or a URLRequest, or if optional typing muddies the call, the compiler reports an ambiguous reference.
A Clear Working Example
The safest pattern is to unwrap the URL first and then call the URL overload directly:
Because url is explicitly a non-optional URL, Swift can choose the right overload without guessing.
Be Explicit with URLRequest Too
If you need headers, method, or body, use URLRequest and keep the type obvious:
Again, the critical part is that request is clearly a URLRequest, not something Swift has to infer from a complicated expression.
Common Sources of Ambiguity
A very common problem is optional chaining or weak typing:
That fails because maybeURL is a URL?, not a URL. Unwrap it first:
Another source of ambiguity is passing values typed as Any, AnyObject, or loosely inferred intermediate variables. In overloaded APIs, vague types make the compiler’s job much harder.
Closure Signature Still Matters
The completion handler must also match the expected signature. If you write a closure whose parameters do not line up with the dataTask overload you meant, the compiler can also get confused.
Sticking to the standard form helps:
If you are splitting the closure into a named variable, give that variable an explicit type so Swift does not have to infer everything at once.
This Is Usually a Type Problem, Not a Runtime Problem
Because the error happens at compile time, the fix is almost always:
- make the URL non-optional before the call
- make the request type explicit
- simplify the expression so overload resolution is obvious
Once the compiler can see the intended types clearly, the error usually disappears without any workaround.
Common Pitfalls
The most common mistake is passing an optional URL? directly into dataTask. The session APIs expect a concrete URL or URLRequest, not an optional wrapper.
Another pitfall is building the request inline with so much inference that the compiler sees multiple possible overload matches. Breaking the code into named, explicitly typed variables usually fixes it immediately.
It is also easy to assume the API is broken because the error text mentions ambiguity in a scary way. In practice, this is almost always a typing issue in the call site, not a Foundation bug.
Finally, avoid carrying request values around as Any or AnyObject. Overloaded APIs and erased types are a bad combination in Swift.
Summary
- The error means Swift cannot choose the correct
dataTaskoverload from the types you provided. - Unwrap
URL?values before callingdataTask. - Use explicit
URLorURLRequestvariables to simplify overload resolution. - Keep the completion-handler signature standard and unambiguous.
- This is usually a compile-time typing issue, not a real
URLSessionbug.
Related reading
- Swift add icon/image in UITextField
- Swift add icon/image in UITextField
- Swift addsubview and remove it
- Swift Alamofire How to get the HTTP response status code
- Swift Alamofire VS AFNetworking
- Swift alert view with OK and Cancel which button tapped?
- Swift and mutating struct
- Swift apply .uppercaseString to only the first letter of a string
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.