我正在尝试设置一个接收整数的函数,并在将来的n天内安排本地通知.我收到一个错误,我无法将类型Date转换为DateComponents.我无法弄清楚如何转换它.我在这里和这里找到了一些其他类似的问题,但我无法使这些答案适应Swift 3的工作.
如何将Date转换为DateComponents?有没有更好的方法来安排通知?
在此先感谢您的帮助 :)
带有错误的行,"无法转换类型'日期的值?' 预期的参数类型'DateComponents'":
let trigger = UNCalendarNotificationTrigger(dateMatching: fireDateOfNotification, repeats: false)
Run Code Online (Sandbox Code Playgroud)
功能齐全:
func scheduleNotification(day:Int) {
let date = Date()
let calendar = Calendar.current
var components = calendar.dateComponents([.day, .month, .year], from: date as Date)
let tempDate = calendar.date(from: components)
var comps = DateComponents()
//set future day variable
comps.day = day
//set date to fire alert
let fireDateOfNotification = calendar.date(byAdding: comps as DateComponents, to: tempDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: fireDateOfNotification, repeats: false) //THIS LINE CAUSES …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Swift 2 中在我的地图上设置最小缩放级别。我找不到任何关于如何限制地图被放大得太远的文档。我决定尝试的是监视地图移动(例如拖动或缩放)然后设置MKZoomScale回最小值。
我找到的大多数答案regionDidChangeAnimated都在 Objective C 中,我不知道,而且我无法将它们转换为 Swift。
我尝试实现@hEADcRASH 的答案:https ://stackoverflow.com/a/30924768/4106552 ,但是当地图在模拟器中移动时,它不会触发并打印任何内容到控制台。
谁能告诉我我做错了什么?我是 Swift 的新手,所以这可能是一个小错误。另外,让我知道是否有一种轻量级的方法来解决限制地图上的缩放级别。我担心运动监视器会稍微减慢地图动画的速度。谢谢您的帮助。
这是我的视图控制器。导入 UIKit 导入解析导入 MapKit
class SearchRadiusViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
@IBOutlet weak var menuBtn: UIBarButtonItem!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
//menu button control
if self.revealViewController() != nil {
menuBtn.target = self.revealViewController()
menuBtn.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
//user location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() …Run Code Online (Sandbox Code Playgroud) 我在使用 rest-client gem 向 uber api 发出请求时遇到问题。我认为问题在于我试图将所需的信息作为 url 中的参数而不是数据字段传递。示例 curl 请求在 之后具有该参数-d,但我不知道如何使用 rest-client 实现它。谢谢您的帮助。
此请求在控制台中工作:
curl -v -H "Authorization: Bearer " + session[:request_token] \
-H "Content-Type: application/json" -X POST -d \ '{"start_latitude":"37.334381","start_longitude":"-121.89432","end_latitude":"37.77703","end_longitude":"-122.419571","product_id":"a1111c8c-c720-46c3-8534-2fcdd730040d"}' \
https://sandbox-api.uber.com/v1/requests
Run Code Online (Sandbox Code Playgroud)
来自我的控制器的请求得到 406 错误,不可接受的响应。
@uber_ride = JSON.load(RestClient::Request.execute(
:method => :post,
:url => "https://sandbox-api.uber.com/v1/sandbox/requests/product_id=#{@uberx_id}?start_latitude=#{lat_start}&start_longitude=#{lng_start}&end_latitude=#{lat_end}&end_longitude=#{lng_end}",
:headers => {'Authorization' => 'Bearer ' + session[:request_token], 'content_type' => 'application/json'}
))
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个额外的字段,data但会出现 404 Resource Not Found 错误。
@uber_ride = JSON.load(RestClient::Request.execute(
:method => :post,
:url => "https://sandbox-api.uber.com/v1/sandbox/requests/",
:data => {'start_latitude' …Run Code Online (Sandbox Code Playgroud)