如何将此代码转换为swift 4?我认为这段代码是用swift 2编写的。我不太懂。我下载了Xcode 8,但我无法迁移。我想把它变成Swift 3然后转到Swift 4,但我没有成功。你会以清楚的方式告诉我还是翻译它?
NSCalendar *gregCalrendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalrendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromdate:[NSDate date]];
[dateComponent setYear:2012];
[dateComponent setMonth:9];
[dateComponent setDay:28];
[dateComponent setHour:16];
[dateComponent setMinute:11];
UIDatePicker *dd = [[UIDatePiclet alloc]init];
[dd setDate:[gregCalrendar dateFromComponents:dateComponent]];
UILocalNotification *notification: [[UILocalNotification alloc]init];
[notification setAlertBody:@"UMUT CAN ALPARSLAN"];
[notification setFireDate:dd.date];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notificaion]];
答案 0 :(得分:0)
UILocalNotification
已弃用。试试这段代码:
let gregCalrendar = Calendar(identifier: .gregorian)
var dateComponent = gregCalrendar.dateComponents([.year, .month, .day, .hour, .minute], from: Date())
dateComponent.year = 2012
dateComponent.month = 9
dateComponent.day = 28
dateComponent.hour = 16
dateComponent.minute = 11
let dd = UIDatePicker()
dd.setDate(gregCalrendar.date(from: dateComponent)!, animated: true)
let content = UNMutableNotificationContent()
content.title = "UMUT CAN ALPARSLAN"
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats:false)
let request = UNNotificationRequest(identifier: "uniqueId", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
您还需要在顶部添加import UserNotifications