Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在类内实现委托(iOS/Swift)_Java_Android_Ios_Swift_Delegates - Fatal编程技术网

Java 在类内实现委托(iOS/Swift)

Java 在类内实现委托(iOS/Swift),java,android,ios,swift,delegates,Java,Android,Ios,Swift,Delegates,在移动开发中,接口和委托的实现如下(以位置服务为例): 1。Android、Java 1。iOS,Swift 这种安卓系统的iOS/Swift等价物是什么: 2。Android、Java 2。iOS,Swift? 我没有受过学术训练,所以我的编程行话真的很糟糕。还有谁能解释一下这两种实现之间的区别,以及它们的名称 很多TIA。在Swift中,协议(如CLLocationManagerDelegate提供了类、结构或枚举必须实现的“东西”蓝图,以表示它“符合”该特定协议。(它也可以有不需要实现的可

在移动开发中,接口和委托的实现如下(以位置服务为例):

1。Android、Java

1。iOS,Swift

这种安卓系统的iOS/Swift等价物是什么:

2。Android、Java

2。iOS,Swift?

我没有受过学术训练,所以我的编程行话真的很糟糕。还有谁能解释一下这两种实现之间的区别,以及它们的名称


很多TIA。

在Swift中,协议(如
CLLocationManagerDelegate
提供了类、结构或枚举必须实现的“东西”蓝图,以表示它“符合”该特定协议。(它也可以有不需要实现的可选方法。)

在您的第一个示例中:

class myVC: UIViewController, CLLocationManagerDelegate { }
myVC
表示它实现/符合协议
CLLocationManagerDelegate
。为此,必须实现该协议的任何必要方法,也可以实现任何可选方法;例如
locationManager(uuquo:didUpdateLocations:)

如果我理解你的问题,你想知道你是否会最终实现
myClass
,而不说它实现了
CLLocationManagerDelegate
。大概是这样的:

class myVC: UIViewController {
   var locationManagerDelegate = SomethingImplementingTheDelegate()
}

class SomethingImplementingTheDelegate: CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { /*...*/ }
}
正如您所看到的,您仍然需要一些类/结构/枚举来实现协议/委托
CLLocationManagerDelegate
所说的内容,因为(协议)只是一个蓝图


(技术说明:协议可以通过协议扩展提供一些实现,但它是针对一致性类型的,您仍然无法实例化协议)。

如果您想增强位置在IOS中的工作方式,则可以通过这种方式来实现

在Info.plist中添加这些行

<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
1.1-声明locationManager对象

let locationManager = CLLocationManager()
步骤:2

Define one method where you configure all this for Location method
let say

func setupLocationAcess()
{
   locationManager = CLLocationManager()
   locationManager.delegate = self
   locationManager.requestWhenInUseAuthorization()
   locationManager.desiredAccuracy = kCLLocationAccuracyBest
   locationManager.startUpdatingLocation()
}
步骤:3 定义此方法以显示视图

步骤:4 在类中实现委托方法

public func locationManager(_ manager: CLLocationManager, didUpdateLocations 
locations: [CLLocation]) 
{
    guard let location = locations.first else { return }
    currentLocationListeners.forEach { $0.action(location) }
    currentLocationListeners = currentLocationListeners.filter { !$0.once }
    manager.stopUpdatingLocation()
}

我希望它能帮助您

var代表:CLLocationManagerDelegate?强烈建议您在启动
LocationListener
这是
接口
CLLocationManagerDelegate
这是
协议
之前先阅读swift文档,两者之间存在很大差异。读这篇文章并不是因为这两种语言的不同。将它们放在类的顶部和在类中实例化它们之间有什么区别?谢谢您的解释。
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
Declare the delegate in class where you need location update
Let say 

class LocationPickerViewController: CLLocationManagerDelegate 
let locationManager = CLLocationManager()
Define one method where you configure all this for Location method
let say

func setupLocationAcess()
{
   locationManager = CLLocationManager()
   locationManager.delegate = self
   locationManager.requestWhenInUseAuthorization()
   locationManager.desiredAccuracy = kCLLocationAccuracyBest
   locationManager.startUpdatingLocation()
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations 
locations: [CLLocation]) 
{
    guard let location = locations.first else { return }
    currentLocationListeners.forEach { $0.action(location) }
    currentLocationListeners = currentLocationListeners.filter { !$0.once }
    manager.stopUpdatingLocation()
}