草庐IT

swift - call//Twitter 中缺少参数 #1 的参数

coder 2023-09-15 原文

我正在创建一个每天生成不同报价的应用程序。我想让用户能够与 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 类:

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/

有关swift - call//Twitter 中缺少参数 #1 的参数的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  3. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  4. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  5. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  6. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  7. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  8. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  9. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  10. ruby - 怎么来的(a_method || :other) returns :other only when assigning to a var called a_method? - 2

    给定以下方法:defsome_method:valueend以下语句按我的预期工作:some_method||:other#=>:valuex=some_method||:other#=>:value但是下面语句的行为让我感到困惑:some_method=some_method||:other#=>:other它按预期创建了一个名为some_method的局部变量,随后对some_method的调用返回该局部变量的值。但为什么它分配:other而不是:value呢?我知道这可能不是一件明智的事情,并且可以看出它可能有多么模棱两可,但我认为应该在考虑作业之前评估作业的右侧...我已经在R

随机推荐