I'll keep this as short and simple as I can.
我将尽量保持简洁。
I have an NSDate extension that I use for a number of things throughout my app, but I have run into a problem with (strangely) a very simple function.
我有一个NSDate扩展,我在我的应用程序中使用了很多东西,但是我遇到了一个非常简单的问题。
The relevant part of the extension is:
延长的有关部分是:
import Foundation
extension NSDate {
func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
Its purpose is simply to return an NSDate which is referring to a Monday morning.
它的目的只是返回一个NSDate,它指的是一个周一的早晨。
So far so good...
到目前为止一切顺利……
The issue is when I try to call this function, I get an error:
问题是当我尝试调用这个函数时,我得到一个错误:
Missing Argument For Parameter #1 In Call
在调用中丢失参数#1的参数
I read here https://*.com/a/26156765/3100991 about someone who had a similar problem, however their code was long and confusing and I didn't understand the answer. I think it would be useful for the general community if a simple explanation and fix was provided.
我在这里读到https://*.com/a/26156765/3100991关于某人有类似的问题,但是他们的代码又长又混乱,我不明白答案。我认为,如果能提供一个简单的解释和解决办法,将对一般社会有用。
Thanks for your help in advance!
提前感谢您的帮助!
Loic
卢瓦
1 个解决方案
#1
2
I just try that (in the playground) and it works. I think your error is in the call of your method. since your method is an instance method you have to call it on an existing date object. So try to call it like that:
我只是(在操场上)试了一下,就行了。我认为你的错误在于你的方法。因为您的方法是一个实例方法,所以您必须在现有的日期对象上调用它。所以试着这样称呼它:
import Foundation
extension NSDate {
func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
// Call
var date = NSDate();
date.nineAMMonday()
But in your case the best thing to do is a class func.
但在你的情况下,最好的方法是一个类func。
import Foundation
extension NSDate {
class func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
// Call
NSDate.nineAMMonday()
#1
2
I just try that (in the playground) and it works. I think your error is in the call of your method. since your method is an instance method you have to call it on an existing date object. So try to call it like that:
我只是(在操场上)试了一下,就行了。我认为你的错误在于你的方法。因为您的方法是一个实例方法,所以您必须在现有的日期对象上调用它。所以试着这样称呼它:
import Foundation
extension NSDate {
func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
// Call
var date = NSDate();
date.nineAMMonday()
But in your case the best thing to do is a class func.
但在你的情况下,最好的方法是一个类func。
import Foundation
extension NSDate {
class func nineAMMonday() -> NSDate {
let greg = NSCalendar.currentCalendar()
let componenets = NSDateComponents()
componenets.weekday = 2
componenets.hour = 9
componenets.minute = 0
componenets.second = 0
return greg.dateFromComponents(componenets)!
}
}
// Call
NSDate.nineAMMonday()