2016-12-05 88 views
0

我想在Swift doc中使用AFNetworking(這是一個必要的約束條件,否則我很樂意學習AlamoFire)。我與這裏做什麼掙扎,有人很新的雨燕:將Obj-C塊轉換爲Swift進行AFNetworking調用

- (nullable AFHTTPRequestOperation *)GET:(NSString *)URLString 
        parameters:(nullable id)parameters 
         success:(nullable void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
         failure:(nullable void (^)(AFHTTPRequestOperation * __nullable operation, NSError *error))failure; 

特別是,我不清楚什麼失敗塊做。這裏是我的嘗試:

manager.GET(
"random_url", 
parameters: [...random parameters...], 
success: { (operation: AFHTTPRequestOperation!, 
responseObject: AnyObject!) in 
print("JSON: " + responseObject.description) 
}, 
failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in 
     print("there was an error") 
} 
) 

我得到一個錯誤,我failure塊:

Cannot convert value of type '() ->()' to expected argument type '((AFHttpRequestOperation?, NSError) -> Void)?'

我很感激,如果人們可以告訴我哪裏我會錯上述​​。非常感謝。

+0

@PEEJWEEJ你看過我的留言嗎?該選擇不在我的控制之下。 – helloB

回答

0

我知道這是不是從錯誤清楚,但我們的移植斯威夫特2應用斯威夫特3.問題,當我有一個類似的問題,至少對我們來說,是關於封閉中的參數。將它們指定爲可選類型,而不是強制展開可選。換句話說,嘗試:

failure: { (operation: AFHTTPRequestOperation?, error: NSError?) in 
    print("there was an error") 
} 

同樣,錯誤似乎並沒有表明這是問題,但這正是我們在三個月前遇到的情況。

+0

非常感謝!這正是問題所在。 – helloB

+1

很高興幫助!當我們處理這個問題時,我知道它有多令人沮喪。 –

0

看來你錯過了「中」:

failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in 
     print("there was an error") 
} 
+0

感謝您的建議。我修改了代碼,但錯誤消息仍然保持不變,即使在代碼清理後也是如此。 – helloB