Ambigious reference to member request() issues with Alamofire after migration to swift 3
I have Alamofire swift3
branch working in Xcode 8.0 ß6 with the following code:
Alamofire.request("https://\(ip)/api/version", withMethod: .get,
parameters: nil, encoding: .json, headers: headers)
.validate()
.responseJSON { response in
//debugPrint(response)
switch response.result {
case .success:
if let JSON = response.result.value {
let version = Mapper<Version>().map(JSON)
print("Version \(version?.server!)")
}
case .failure(let error):
print (error)
}
}
Pay close attention to the order and types of your arguments to .request
You should have only one Alamofire framework active. Try to redo it in another clone, or maybe try the following in the clone you have?
pod cache clean --all
pod install
What does your Podfile
have in it?
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'NewApp' do
pod 'Alamofire',
:git => 'https://github.com/Alamofire/Alamofire.git',
:branch => 'swift3'
end
Swift 3.1 Migration Dispatch.main.async ambiguous reference to member 'async(execute:)'
The issue was not with DispatchQueue.main.async
but with Alamofire's uploadProgress syntax, replaced it with the following block and issue got resolved:
uploads.uploadProgress { (progress: Progress) in
DispatchQueue.main.async {
progressview.setProgress(progress.fractionCompleted, animated: true)
}
}
I cannot compile my alamofire code due to ambiguous reference to member jsonObject. Why?
You have used responseJSON
with Alamofire
so you will get Serialized JSON in the completion block, no need to use JSONSerialization
use directly it.
switch response.result {
case .success:
if let jsonData = JSON(response.result.value) {
for requestJSON in jsonData["geojson"]["features"] {
if let request = SingleCluster.fromJSON(requestJSON){
let pinOne = CustomCluster()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.amount = request.amount
self.eventsArray.append(pinOne);
}
}
}
case .failure(let error):
print("error")
}
Swift 3 Alamofire multipart upload
For example, using Alamofire 4.0.0 in Swift 3:
(make sure you are 4.0.0 ready as it looks like you haven't updated your Alamofire yet)
Alamofire.upload(multipartFormData: { (multipartFormData) in
// code
}, to: URL, encodingCompletion: { (result) in
// code
})
or
Alamofire.upload(multipartFormData: { (multipartFormData) in
// code
}, with: URL, encodingCompletion: { (result) in
// code
})
So headers
need to be passed by URL request:
let URL = try! URLRequest(url: "http://example.com", method: .get, headers: headers)
Ambiguous reference to member 'upload(_:to:method:headers:interceptor:)'
I just installed pods with
pod 'Alamofire', '~> 5.0.0-beta.5'
// sample code
let image: UIImage? = self.uploadImg.image;
let uploadDict = ["user_id":getUserId] as [String:String]
Alamofire.upload(multipartFormData: { MultipartFormData in
let image :Data = (image?.jpegData(compressionQuality: 1))!
MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
for(key,value) in uploadDict{
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}
}, to: "http://XXXXXXXXXXXXXXXX/uploadImage", encodingCompletion: {
EncodingResult in
switch EncodingResult{
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint("SUCCESS RESPONSE: \(response)")
}
case .failure(let encodingError):
print("ERROR RESPONSE: \(encodingError)")
} })
Swift 3.0: Type of Expression is ambiguous without more context?
The error you mentioned tells you that the compiler cannot determine the exact type of the value you entered.
You started with a period, something has to be before the period. Sometimes the compiler can understand without your help. That's not this case, it has several options so it's ambiguous and it asks you to tell exactly the class name that you meant.
URLSession.shared.dataTask issues
In Swift 3, you should not use the NS
-prefixed types, when there is a native Swift type available. For example, instead of NSURL
, use URL
, and instead of Objective-C's NSString
, use String
. Generally, try to use Swift-Foundation as much as possible.
let url = URL(string: "https://s3-us-west-2.amazonaws.com/youtubeassets/home.json")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error)
return
}
let str = String(data: data!, encoding: .utf8)
print(str)
}.resume()
Related Topics
Tvos Remote Notification Replacement
Calling Stop() on Avaudioplayernode After Finished Playing Causes Crash
Gcdasyncsocket Multiple Connections Wont Accept Data from Multiple Sockets
How to Implement a Generic Struct That Manages Key-Value Pairs for Userdefaults in Swift
Why Does This Swiftui Picker Code Not Work
How to Pass Image Value to The Imageslideshow Using Swift
How to Remove Node from Parent If Touched More Than Once
Iwatch: Wkinterfacelabel How to Stop Text from Being Cut Off with "..." at The End of a Label
Provide Simple Method to Get Current Speed (Implementing Speedometer)
Get All Available Characters from a Font
Convert String to Staticstring
Nspredicate in Query from Array Elements
Get the Type of Anyobject Dynamically in Swift
Changing Tab Bar Color (Swift)
How to Test Required Init(Coder:)