【问题标题】:Extraneous argument label "completion" in call, swift 2调用中的无关参数标签“完成”,swift 2
【发布时间】:2015-11-17 17:55:35
【问题描述】:

我有一个函数,它在完成后返回一些数组。函数如下所示:

func fetchCalendarEvents (completion: (eventArray: [Meeting]) -> Void) -> Void {

    let eventStore : EKEventStore = EKEventStore()


    eventStore.requestAccessToEntityType(EKEntityType.Event, completion: {
        granted, error in
        if (granted) && (error == nil) {
            print("access granted: \(granted)")
            .......
        completion (eventArray: arrayOfEvents)
    }
        }
        else {
            print("error: access not granted  \(error)")
            completion (eventArray: [])
        }
    })
}

当尝试像这样调用这个函数时,我收到以下错误:

//error in this line: Extraneous argument label "completion" in call:
CalendarController.fetchCalendarEvents(completion:{(eventArray:[Meeting]) -> Void in
        for meeting in eventArray {
            print("Meeting: \(meeting.title)")
        }
    })

我试图围绕完成处理程序展开我的头脑,我使用了这个例子: http://alanduncan.me/2014/06/08/Swift-completion-blocks/ 但是我不明白这段代码有什么问题?

此外,当我删除完成标签时,我会得到以下信息:

【问题讨论】:

  • @matt:不,但是你用什么代替?我需要获取日历事件,你是怎么做的(我们支持ios8、ios9,以防万一)
  • 对不起,我只是糊涂了。一大早来这里!
  • 哈哈,没问题 :)) 我通过调整@Eric D. 的答案找到了正确的答案

标签: ios swift2


【解决方案1】:

您不能使用data 参数标签,因为您的参数标签未命名为data,而是命名为eventArrayCalendarController.fetchCalendarEvents 调用也有错误。

修复后,您的代码应如下所示:

func fetchCalendarEvents (completion: (eventArray: [Meeting]) -> Void) -> Void {

    let eventStore : EKEventStore = EKEventStore()

    eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { granted, error in
        if granted {
            print("access granted: \(granted)")
            completion(eventArray: arrayOfEvents)
        } else {
            print("error: access not granted  \(error)")
            completion(eventArray: [])
        }
    })
}

还有:

let calController = CalendarController()
calController.fetchCalendarEvents { (eventArray) -> Void in
    // ...
}

【讨论】:

  • 吹毛求疵:&& error == nil 永远无法到达。如果授予访问权限,则错误始终为零
  • @EricD。请看编辑问题中的图像,我修复了数组的名称,并删除了无关的标签,仍然得到错误
【解决方案2】:

好的,在@Eric D 的帮助下,正确答案是:

let f = CalendarController()
f.fetchCalendarEvents{(eventArray) -> Void in
    ....
}

问题是我尝试将方法应用于类,而不是类类型的变量。我不知道为什么,但它只能这样工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多