我正在创建一个每天生成不同报价的应用程序。我想让用户能够通过 Twitter 分享所给出的报价。我能够让 Twitter 与推文框一起弹出,但引用并未显示。我试图让它发生,但现在,我不断收到错误:
Missing argument for parameter for parameter #1 in call
问题: 我希望能够在用户点击 Twitter 按钮后弹出推文框时获取引用来填充推文框
代码如下:
@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)
}
}
编辑: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
}
}
关于swift - call//Twitter 中参数 #1 缺少实参,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925889/