我创建了一个简单的应用程序,它可以跟踪用户位置,并在每次更新位置时创建本地通知.
我启用了下面的背景模式,
let locationManager = CLLocationManager()
open override func viewDidLoad() {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 10
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
}
open func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let notification = UILocalNotification()
notification.alertBody = "location updated"
notification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(notification)
}
Run Code Online (Sandbox Code Playgroud)
我设置字符串NSLocationAlwaysUsageDescription
并请求许可.用户授予第一次加载应用时始终使用的权限.
当应用程序处于前台时它运行良好,当背景仍然工作至少在5-40分钟的时间范围内,可以通过电池或其他打开的应用程序进行更改.
问题是为什么它停止工作,它不会继续工作?
我从未在Apple文档中看到时间限制.
这段代码
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
internal class Program
{
public static void Main()
{
var values = new[] {1, 2, 3, 3, 2, 1, 4};
var distinctValues = GetDistinctValuesUsingWhere(values);
Console.WriteLine("GetDistinctValuesUsingWhere No1: " + string.Join(",", distinctValues));
Console.WriteLine("GetDistinctValuesUsingWhere No2: " + string.Join(",", distinctValues));
distinctValues = GetDistinctValuesUsingForEach(values);
Console.WriteLine("GetDistinctValuesUsingForEach No1: " + string.Join(",", distinctValues));
Console.WriteLine("GetDistinctValuesUsingForEach No2: " + string.Join(",", distinctValues));
Console.ReadLine();
}
private static IEnumerable<T> GetDistinctValuesUsingWhere<T>(IEnumerable<T> items)
{
var set=new HashSet<T>();
return items.Where(i=> set.Add(i));
}
private static IEnumerable<T> GetDistinctValuesUsingForEach<T>(IEnumerable<T> …
Run Code Online (Sandbox Code Playgroud) 我有基类作为动物和儿童类作为狗
这是代码:
class Animal
{
public int Legs { get; set; }
public string Name { get; set; }
}
class Dog : Animal
{
public int noOfTail { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的主要方法中,当我在下面执行时,我得到异常:
static void Main(string[] args)
{
Animal a = new Animal();
Dog d = new Dog();
d = (Dog)a;
}
Run Code Online (Sandbox Code Playgroud)
但是当我第一次将我的派生类转换为父类时,我没有任何异常,这就是为什么我感到困惑,有人可以解释其背后的原因.
static void Main(string[] args)
{
Animal a = new Animal();
Dog d = new Dog();
a = d;
d = (Dog)a;
}
Run Code Online (Sandbox Code Playgroud)