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

Missing argument for parameter #1 in call // Twitter

我正在创建一个每天生成不同报价的应用程序。我想让用户能够与 twitter 分享 给出的报价。我能够让推特弹出推文框,但引述没有出现。我试图让它发生,但现在,我不断收到错误消息:

Missing argument for parameter for parameter #1 in call

问题: 我希望能够在用户点击推特按钮后弹出推文框时获取报价来填充推文框

代码如下:

@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 class:

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

编辑:21 年 12 月替换图像以省略项目名称。

您的 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 monad。自己实现这个并不难,但您可以查看 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
    }
}