NSDate来自周数(NSDate from week number)
我正在尝试使用Swift创建每周重复的本地通知。 通知将设置为每周的第一天和10:00。 当然我不想为过去的日期设置通知,因此我需要查看它是否已经过了第10周的第一天。 如果没有,我将在今天10:00创建通知,否则在下周一周一10:00创建通知。
我创建了一个扩展来计算当前日期的周数。
extension NSDate { func weekOfYear() -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components(NSCalendarUnit.WeekOfYear, fromDate: self) let weekOfYear = components.weekOfYear return weekOfYear } }
我不能再这样了。 任何帮助表示赞赏。 问候。
I am trying to create a weekly recurring local notification using Swift. Notification is going to be set to first day of every week, and in 10:00. Ofcourse I don't want to set a notification for a past date, so I need to see if now it has passed first day of week 10:00. If not, I will create notification for today 10:00, else next week monday 10:00.
I created an extension to calculate week number for current date.
extension NSDate { func weekOfYear() -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components(NSCalendarUnit.WeekOfYear, fromDate: self) let weekOfYear = components.weekOfYear return weekOfYear } }
I couldn't go any further than this. Any help is appreciated. Regards.
原文:
最满意答案
你的
"#{price}".to_i
没有做任何有意义的事情。 它只是在代码中的该位置留下一个整数,并且不使用。 如果你做price = price.to_i
,那么它可能会起作用。作为一般性提示,当您想要查看正在发生的事情时,请使用
p
而不是puts
。 你的使用puts
让你很难在比较之前判断price
仍然是一个字符串。 如果您使用了p
,那么您就可以区分字符串和数字。Your
"#{price}".to_i
is not doing anything meaningful. It is just leaving an integer in that position in the code, and is not used. If you doprice = price.to_i
, then it will probably work.As a general tip, when you want to look into what is happening, use
p
rather thanputs
. Your usingputs
made it difficult for you to tell thatprice
is still a string right before the comparison. If you had usedp
, then you would have been able to distinguish a string and numeral.
相关问答
更多-
你可以使用Convert.ToSByte 例如: string x = "aa"; sbyte v = Convert.ToSByte(x, 16); My solution was to put the first take the first 8 bits of the 16 bit integer and store them in an sbyte.
-
你得到的错误是因为这个: https : //docs.python.org/2/library/sys.html#sys.stdout stdout和stderr不需要内置文件对象:只要它有一个带有字符串参数的write()方法,任何对象都是可以接受的。 stdout和stderr是写入数据的位置,您不应该将数据本身分配给stdout,这就是您看起来像是在做的事情。 正如其他人所提到的,您可以将stdout分配到您想要的位置,然后使用print命令来实际获取数据。 但是,如果你所要做的只是将一行文本打印到 ...
-
一种可能性是使用正则表达式进行测试,如下所示: function isInteger(value) { return /^\d+$/.test(value); } One possibility is to use a regex to test, like so: function isInteger(value) { return /^\d+$/.test(value); }
-
Ruby on Rails模型创建字符串未转换为整数(Ruby on Rails Model creation string not converted to integer)[2024-02-11]
找到了原因。 在我的模型release.rb中,我定义了“attr_accessible:id,:name,:code,:as =>:tmp_use”。 然后它只接受id,name和code。 我添加:base_release_id后,它的工作原理。 不确定如何:as =>:tmp_use工作。 我想默认所有变量都可以访问。 在某些地方,我使用的是Release.new(“id”=> row [:id],“name”=> row [:name],“code”=> row [:code],:as =>:tmp ... -
代码段create_code()[index]执行以下操作: 创建一个整数列表,即[7, 4, 7, 0] 选择并返回位于索引处的整数,即等于2索引处的整数,这将返回7 in语句不能用于搜索整数中的字符串字符。 The snippet create_code()[index] does the following: creates a list of integers, i.e. [7, 4, 7, 0] selects and returns the integer located at index i. ...
-
json对象的所有值都是字符串。 你需要自己施展。 Integer.valueOf(STRING_YOU_WANT_TO_CONVERT); 您也可以使用JsonDeserializer。 这里有一个例子: 返回响应时Android JsonDeserializer中的错误 Finally I could make changes according to my requirements. My requirement was to validate the JSON request against th ...
-
myInt是一个值类型而不是引用。 所以,一旦你将myInt转换为一个字符串(应该这样做: var myString = "\(myInt)" ),对myInt所做的任何更改都不会对myString产生任何影响。 如果你想确保myString总是等于myInt的字符串表示,你可以使myString成为一个计算属性... var myString: String { get { return "\(myInt)" } } myInt is a value type and not a ref ...
-
#+在String上实现,并在添加之前对Number进行强制转换。 Squeak在其核心代码库中有很多eToys(针对孩子的Smalltalk变体)代码。 这可能是String实现所有数学运算符的原因。 在Pharo中,数学运算符大部分都是从String移除的,因此'1' + '2'会引发像其他任何Smalltalk一样的错误。 #+ is implemented on String and does a coercion to a Number before doing the addition. Squ ...
-
COALESCE不会用NULL值替换空字符串。 相反,它返回列表中的第一个非NULL表达式。 要用NULL替换零长度字符串,可以使用各种表达式... NULLIF(expr,'') IF(expr='',NULL,expr) CASE WHEN expr = '' THEN NULL ELSE expr END 要将字符串(CHAR,VARCHAR)表达式转换为数字值,您可以使用速记: expr + 0 但是不能保证返回UNSIGNED INT,甚至是整数值。 下面的原始答案 这里缺少的是` ...
-
字符串未转换为整数(String not converted to integer)[2021-07-26]
你的"#{price}".to_i没有做任何有意义的事情。 它只是在代码中的该位置留下一个整数,并且不使用。 如果你做price = price.to_i ,那么它可能会起作用。 作为一般性提示,当您想要查看正在发生的事情时,请使用p而不是puts 。 你的使用puts让你很难在比较之前判断price仍然是一个字符串。 如果您使用了p ,那么您就可以区分字符串和数字。 Your "#{price}".to_i is not doing anything meaningful. It is just leav ...