使用Swift 2 + AFNetworking

时间:2015-08-06 20:59:10

标签: ios swift afnetworking-2 swift2

我有以下代码

let manager = AFHTTPRequestOperationManager()
    manager.POST(
        "http://shipeala.redmintlabs.com/api/orders/create",
        parameters: params,
        constructingBodyWithBlock: { (formData : AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(SHOrderImage.getInstance().image!, name: "photo", fileName: "image.jpg", mimeType: "image/*")
        },
        success: { (operation: AFHTTPRequestOperation!,
            ""
        },
        failure: { (operation: AFHTTPRequestOperation!,
            error: NSError!) -> Void in
            ""
        }
    )

我收到了这个错误:

  

无法调用' POST'使用类型'的参数列表(String,参数:NSDictionary,constructBodyWithBlock:(AFMultipartFormData!) - > Void,成功:(AFHTTPRequestOperation!,AnyObject!) - > Void,失败:(AFHTTPRequestOperation!,NSError! ) - > Void)'

任何想法??

4 个答案:

答案 0 :(得分:1)

你可以这样做,

        var manager = AFHTTPRequestOperationManager()
        var operation = manager.POST("http://shipeala.redmintlabs.com/api/orders/create", parameters: params, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(imageData, name: "Photo", fileName: "photo.jpg", mimeType: "image/jpeg")
        }, success: { (operation, responseObject) -> Void in
            println(responseObject)
        }) { (operation, error) -> Void in
            println(error)
        }

        operation.start()

答案 1 :(得分:0)

您忘记了成功处理程序的返回类型“ - > Void”。这个编译:

    manager.POST("", parameters: params, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
        //
        }, success: { (operation: AFHTTPRequestOperation!, response: AnyObject?) -> Void in
        //
        }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
        //
    }

答案 2 :(得分:0)

// add this line of code for image to NSData conversion
let imageData = UIImageJPEGRepresentation(UIImage(named: "yourImageNAme"), 0.5)


let op : AFHTTPRequestOperation = manager.POST("", parameters: parameters, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(imageData!, name: "Photo", fileName: "photo.jpg", mimeType: "image/jpeg")
            },
            success:
            {
                (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in

                   print(responseObject)                 

            },
            failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
                print(error)

        })!


op.start()

答案 3 :(得分:0)

在Swift 4中使用AFNetworking上传图片

Sub FindAndBold()
Dim sFind As String
Dim rCell As Range
Dim rng As Range
Dim lCount As Long
Dim iLen As Integer
Dim iFind As Integer
Dim iStart As Integer

On Error Resume Next
Set rng = ActiveSheet.UsedRange. _
  SpecialCells(xlCellTypeConstants, xlTextValues)
On Error GoTo ErrHandler
If rng Is Nothing Then
    MsgBox "There are no cells with text"
    GoTo ExitHandler
End If

sFind = InputBox( _
  Prompt:="Skriv in dina initialer", _
  Title:="Dina initialer")
If sFind = "" Then
    MsgBox "Du skrev inget"
    GoTo ExitHandler
End If

iLen = Len(sFind)
lCount = 0

For Each rCell In rng
    With rCell
        iFind = InStr(.Value, sFind)
        Do While iFind > 0
            .Characters(iFind, iLen).Font.Bold = True
            .Characters(iFind, iLen).Font.Color = RGB(255, 0, 0)
            .Characters(iFind, iLen).Font.ColorIndex = 4
            lCount = lCount + 1
            iStart = iFind + iLen
            iFind = InStr(iStart, .Value, sFind)
        Loop
    End With
Next

If lCount = 0 Then
    MsgBox "Fanns inget" & _
      vbCrLf & "' " & sFind & " '" & _
      vbCrLf & "att markera"
ElseIf lCount = 1 Then
    MsgBox "Det fanns en" & _
      vbCrLf & "' " & sFind & " '" & _
      vbCrLf & "markerades"
Else
    MsgBox lCount & " hittade" & _
      vbCrLf & "' " & sFind & " '" & _
      vbCrLf & "och markerades"
End If

ExitHandler:
Set rCell = Nothing
Set rng = Nothing
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler