如何在Swift中使用UILocalNotification [英] How To Use UILocalNotification In Swift
问题描述
我试图弄清楚如何快速设置UILocalNotification,但是运气不佳.我正在尝试:
I am trying to figure out how to setup a UILocalNotification in swift but I am not having a lot of luck. I am trying this:
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
var dateTime = NSDate.date()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
对于初学者来说,我不确定这是否是获取当前日期时间的正确方法.在.Net中,我只执行DateTime.Now().
For starters, I am not sure if this is the proper way to get the current date time. In .Net, I would just do DateTime.Now().
第二,当我尝试此操作时,出现一个错误,内容为:
Second, when I try this, I get an error that says:
'(@ lvalue NSDate!)-> $ T3'与'NSDate'不相同
'(@lvalue NSDate!) -> $T3' is not identical to 'NSDate'
不幸的是,我不知道这意味着什么或如何进行.
Unfortunately I have no idea what this means or how to proceed.
推荐答案
首先,您使用初始化器语法构造NSDate
:
First, you construct an NSDate
using initializer syntax:
let dateTime = NSDate()
文档显示了ObjC便利构造函数如何映射到Swift初始化程序.如果文档显示某个类的init()
,则使用该类的名称进行调用:对于NSDate
,init()
表示您调用NSDate()
,init(timeInterval:sinceDate:)
表示您调用NSDate(timeInterval: x, sinceDate: y)
,依此类推.
The documentation shows how ObjC convenience constructors map to Swift initializers. If the docs show an init()
for a class, you call it using the name of the class: for NSDate
, init()
means you call NSDate()
, init(timeInterval:sinceDate:)
means you call NSDate(timeInterval: x, sinceDate: y)
, etc.
第二个: fireDate
不是方法,而是属性.您应该分配给它而不是尝试调用它:
Second: fireDate
isn't a method, it's a property. You should assign to it instead of trying to call it:
notification.fireDate = dateTime
alertBody
的同上.
您还可以通过在您的Swift源文件中单击一个类名(或其他API符号)来找到Cocoa API的Swift语法.这会导致Xcode生成相关头文件的"Swift化"版本.
You can also find the Swift syntax for Cocoa APIs by command-clicking a class name (or other API symbol) in your Swift source file; this causes Xcode to generate a "Swift-ified" version of the relevant header file.
这篇关于如何在Swift中使用UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!