How To Use UILocalNotification In Swift

前端 未结 6 2069
温柔的废话
温柔的废话 2021-02-06 01:59

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         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 02:09

    First, you construct an NSDate using initializer syntax:

    let dateTime = NSDate()
    

    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.

    Second: fireDate isn't a method, it's a property. You should assign to it instead of trying to call it:

    notification.fireDate = dateTime
    

    Ditto for alertBody.

    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.

提交回复
热议问题