这是我的代码:
import UIKit
import MessageUI
import UserNotifications
class TripDetailViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, MFMessageComposeViewControllerDelegate {
@IBOutlet weak var ScrollView: UIScrollView!
@IBOutlet weak var tripImage: UIImageView!
@IBOutlet weak var tripTitle: UILabel!
@IBOutlet weak var datePickertxt: UITextField!
@IBOutlet weak var numberOfPeopletxt: UITextField!
@IBOutlet weak var meetingPoint: UILabel!
@IBOutlet weak var maxNum: UILabel!
@IBOutlet weak var tripTime: UILabel!
@IBOutlet weak var spokenLanguage: UILabel!
@IBOutlet weak var hostNumber: UILabel!
@IBOutlet weak var tripLocation: UILabel!
@IBOutlet weak var hostName: UILabel!
@IBOutlet weak var tripDescription: UILabel!
@IBOutlet weak var touristName: UITextField!
let datePicker = UIDatePicker()
var numbersOfPeoplePicker = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
let numberOfPeoplePicker = UIPickerView()
var trip = Trip()
@IBAction func bookButton(_ sender: Any)
{
let content = UNMutableNotificationContent()
content.title = "DiscoverAround"
content.subtitle = "Someone who booked up your trip!"
content.body = touristName.text!; "Have booked your trip, and they are"; numberOfPeopletxt.text!; "people, in"; datePickertxt.text!;
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
我收到了这个错误:
“表达式解析为未使用的I值”
在这一行:
content.body = touristName.text!; "Have booked your trip, and they are"; numberOfPeopletxt.text!; "people, in"; datePickertxt.text!;
答案 0 :(得分:2)
您没有正确格式化文本。如果你想连接字符串,你应该这样做:
content.body = "\(touristName.text!) Have booked your trip, and they are \(numberOfPeopletxt.text!) people, in \(datePickertxt.text!)"
如果您不确定该字段的值是否为零,则应先检查它或为其提供默认值:
if let tourist = touristName.text,
let numberOfPeople = numberOfPeopletxt.text,
let datePicker = datePickertxt.text {
content.body = "\(tourist) Have booked your trip, and they are \(numberOfPeople) people, in \(datePicker)"
}
或
content.body = "\(touristName.text ?? "Nobody") Have booked your trip, and they are \(numberOfPeopletxt.text ?? "0") people, in \(datePickertxt.text ?? "nowhere")"