2016-03-19 29 views
1

我對Swift比較陌生,仍然在理解閉包的概念。 我已閱讀此文章(Anonymous closure can not be used inside a closure that has explicit arguments)。 但是,答案是將過濾器從()更改爲{},但我不知道如何將其實現到我的函數中。匿名閉包參數不能用在具有明確參數的閉包中

<<< ImageRow() 
      { 
       $0.tag = "Image" 
       $0.title = "Choose your profile pic" 
       if let tutorPic = currentuser!.objectForKey("ProfPhoto") as! PFFile! 
       { 
        tutorPic.getDataInBackgroundWithBlock({(imageData:NSData?,error:NSError?)->Void in 
         if(error == nil) 
         { 
          let image = UIImage(data: imageData!) 
          print("YOOWAHH") 
          print(image) 
          print("***********") 
          self.imagez = image 
          print(self.imagez) 
          $0.value = imagez 


         } 
        }) 


       } 


     } 

的錯誤是在線路$0.value = imagez

我從Parse下載了圖像數據,並希望將其設置爲我的窗體的默認值。但編譯器說我已經有明確的參數,所以它不知道如何引用窗體的參數。我該如何修復這個?

回答

2

問題是,因爲每個塊都是分開處理的,因此它不知道如何正確地爲$0返回其他塊的引用。無論您是否明確定義了封閉塊,編譯器都會假設這是您在說$0時所表示的塊。

要解決此問題,只需在頂部的塊中說:let myButton = $0,然後在封閉塊中參考myButton。在未來,如果您不知道塊的形式應該是什麼,只需重新寫出函數調用,自動完成將使塊格式的其餘部分恢復。

相關問題