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

非常感谢您的解答和解释。错误立即清除。你真棒!保持好状况! –