2016-04-28 31 views
2

我正在創建一個每天都會生成不同報價的應用程序。我想讓用戶能夠與推特分享給出的報價。我可以讓推特與推文箱一起彈出,但報價沒有顯示出來。我試圖讓這樣的事情發生,現在,我得到一個恆定的錯誤:參數#1在調用中失去參數// Twitter

Missing argument for parameter for parameter #1 in call

問題: 我想能夠得到的報價,以填補鳴叫箱,因爲它的用戶後彈出水龍頭Twitter的按鈕

下面是與錯誤enter image description here

下面的截圖是代碼:

@IBAction func shareTweet(sender: AnyObject) { 
    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) { 
     Share(text:Quote).shareTwitter().characters.count{ sheet in self.presentViewController(sheet, animated: true, completion: nil)}; 
     let tweetShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 

     self.presentViewController(tweetShare, animated: true, completion: nil) 

    } else { 

     let alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to tweet.", preferredStyle: UIAlertControllerStyle.Alert) 

     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

     self.presentViewController(alert, animated: true, completion: nil) 
    } 

} 

這裏是share.swift類:

import Social 
struct Share { 
let text: String 

init(text: String) { 
    self.text = text 
} 

typealias ShareSheet = SLComposeViewController 

func shareTwitter(count: Int, action: (ShareSheet ->()), error: (UIAlertController ->())) { // Returns either tweetSheet or alert view 
    if (count < 140) { 
     if (SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)) { 
      // Tweets Quote 
      let sheet: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 
      sheet.setInitialText(text) 
      action(sheet) 
     } else { 
      // Not logged into Twitter 
      let alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share", preferredStyle: UIAlertControllerStyle.Alert) 
      alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      error(alert) 
     } 
    } else { 
     // Character Count is greater then 140 
     let alert = UIAlertController(title: "Character Count", message: "Sorry this is too long to tweet", preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
     error(alert) 
    } 
} 

幫助將不勝感激。先謝謝你。 如果您需要更多參考答案,請評論我應該添加到問題中。謝謝。

回答

2

您的shareTwitter(count: action:)函數有兩個參數。你正在調用它沒有參數。然後你試圖調用characters.count上的追蹤閉包參數。我假設你的意思做的是這樣的:

Share(text:Quote).shareTwitter(Quote.characters.count) { sheet in self.presentViewController(sheet, animated: true, completion: nil)}; 

我不認爲你真的需要你的shareTwitter()功能count參數雖然。 Share結構已將text存儲在屬性中。爲什麼不使用它?

func shareTwitter(action: (ShareSheet ->()), error: (UIAlertController ->())) { // Returns either tweetSheet or alert view 
    if (text.characters.count < 140) { 
     // Code removed for brevity 

    } else { 
     // Code removed for brevity 
    } 
} 

然後你就可以讓你的函數調用是這樣的:

Share(text:Quote).shareTwitter() { sheet in self.presentViewController(sheet, animated: true, completion: nil)}; 

此外,你似乎並不在你的action關閉將返回另一個關閉。我想你的意思是讓你的閉包參數返回Void

另一方面,因爲你使用了兩個閉包參數,一個用於action,另一個用於error,我認爲你的代碼即使有這種變化也不會編譯。你需要做的是:

Share(text:Quote).shareTwitter(action: { sheet in 
    self.presentViewController(sheet, animated: true, completion: nil) 

}) { error in 

} 

當使用可能有錯誤的封閉,要做到這一點的最好辦法是使用Result單子。實現這個功能並不難,但是您可以在antitypical/Result中查看一個庫,該庫提供了一個開箱即用的Result monad。

有了這兩方面的變化,你的shareTwitter()功能如下:

func shareTwitter(action: (Result<ShareSheet, NSError> -> Void)) { // Returns either tweetSheet or alert view 
    if (text.characters.count < 140) { 
     // Code removed for brevity 

    } else { 
     // Code removed for brevity 
    } 
} 

,它被稱爲是這樣的:

Share(text:Quote).shareTwitter() { result in 
    switch result { 
    case .Success(let sheet): 
     self.presentViewController(sheet, animated: true, completion: nil)}; 

    case .Failure(let error): 
     // Do something with your error 
    } 
} 
+1

對不起,我只是說這在 –

+0

好吧,我想。我明白你現在想要做的全部意圖。我的答案已經修改,應該不僅僅是解決你的問題。 – AnthonyM

+1

非常感謝您的解答和解釋。錯誤立即清除。你真棒!保持好狀況! –