Swift call//Twitter中缺少参数#1的参数

Swift call//Twitter中缺少参数#1的参数,swift,twitter,parameters,ibaction,Swift,Twitter,Parameters,Ibaction,我正在创建一个每天生成不同报价的应用程序。我想让用户能够与twitter共享给出的报价。我能够让twitter弹出推文框,但没有显示引用。我试图实现这一点,但现在,我得到了一个持续的错误: 调用中参数#1的参数缺少参数 问题: 我想能够得到报价,以填补推文框,因为它弹出后,用户点击推特按钮 这是一个错误的屏幕截图 代码如下: @IBAction func shareTweet(sender: AnyObject) { if SLComposeViewController.isAvaila

我正在创建一个每天生成不同报价的应用程序。我想让用户能够与twitter共享给出的报价。我能够让twitter弹出推文框,但没有显示引用。我试图实现这一点,但现在,我得到了一个持续的错误:

调用中参数#1的参数缺少参数

问题: 我想能够得到报价,以填补推文框,因为它弹出后,用户点击推特按钮

这是一个错误的屏幕截图

代码如下:

@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)
    }

}
以下是共享。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)
    }
}
导入社交网站
结构共享{
让文本:字符串
初始化(文本:字符串){
self.text=文本
}
typealias ShareSheet=SLComposeViewController
func shareTwitter(计数:Int,操作:(ShareSheet->()),错误:(UIAlertController->()){//返回tweetSheet或警报视图
如果(计数<140){
if(SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)){
//推文引用
明细表:SLComposeViewController=SLComposeViewController(用于服务类型:SLServiceTypeTwitter)
sheet.setInitialText(文本)
行动(表格)
}否则{
//没有登录到Twitter
let alert=UIAlertController(标题:“帐户”,消息:“请登录Twitter帐户共享”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Default,处理程序:nil))
错误(警报)
}
}否则{
//字符计数大于140
let alert=UIAlertController(标题:“字符计数”,消息:“抱歉,此消息太长,无法发送tweet”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Default,处理程序:nil))
错误(警报)
}
}
非常感谢您的帮助。提前谢谢您。 如果您需要更多的参考资料来回答,请评论我应该添加到问题中的内容。谢谢。

您的
shareTwitter(计数:操作:)
函数接受两个参数。您调用它时没有参数。然后尝试调用
字符的尾部闭包参数。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
    }
}

此外,您似乎没有在
操作
闭包中返回另一个闭包。我认为您的意思是让闭包参数返回
Void

另一方面,由于您使用了两个闭包参数,一个用于
操作
,另一个用于
错误
,因此我认为即使进行了更改,您的代码也不会编译。您需要这样做:

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

}) { error in 

}
当使用可能有错误的闭包时,最好的方法是使用
Result
monad。自己实现这一点并不难,但您可以查看一个库,它提供了一个现成的
Result
monad

通过这两项更改,您的
shareTwitter()
函数如下所示:

Share(text:Quote).shareTwitter() { sheet in self.presentViewController(sheet, animated: true, completion: nil)};
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
    }
}

抱歉,我刚刚添加了它。好的,我想我理解你现在尝试做的全部意图。我的答案已经修改,应该不仅仅是解决你的问题。非常感谢你的解决方案和解释。错误立即被清除。你太棒了!太棒了!没问题!很高兴听到你现在一切正常。