Reputation: 1532
In mobile development, interfaces and delegates are implemented as follows (using the location service as an example):
1. Android, Java
public class myClass implements LocationListener {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
}
1. iOS, Swift
class myVC: UIViewController, CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
}
}
What would be the iOS/Swift equivalent of this type of Android implementation:
2. Android, Java
public class myClass {
LocationListener GPS = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
}
}
2. iOS, Swift?
class myVC: UIViewController {
???
}
I'm not academically-trained, so my programming lingo is really bad. Could anyone also please help explain the differences between these two types of implementations, and what they are called?
Much TIA.
Upvotes: 0
Views: 203
Reputation: 747
If you want to enhance how location work with IOS then you can do by this way
Add these lines in Info.plist
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
Step : 1
Declare the delegate in class where you need location update
Let say
class LocationPickerViewController: CLLocationManagerDelegate
1.1 - Declare the locationManager object
let locationManager = CLLocationManager()
Step : 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()
}
Step : 3 Define this method to viewWillAppear
Step : 4 Implement delegate method in your class
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()
}
I hope it will help you
Upvotes: 0
Reputation: 423
In Swift, a protocol, such as CLLocationManagerDelegate
provides a blueprint of "stuff" that a class, struct or enum must implement to say it "conforms" to that specific protocol. (It could also have optional methods that are not required to be implemented.)
In your first example:
class myVC: UIViewController, CLLocationManagerDelegate { }
myVC
is saying that it implements/conforms-to protocol CLLocationManagerDelegate
. For this, it must implement any required methods of that protocol and might implement any optional ones too; such as locationManager(_:didUpdateLocations:)
.
If I understand your question, you would like to know if you might end up implementing myClass
without saying it implements CLLocationManagerDelegate
. Something like this:
class myVC: UIViewController {
var locationManagerDelegate = SomethingImplementingTheDelegate()
}
class SomethingImplementingTheDelegate: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { /*...*/ }
}
As you can see, you still need some class/struct/enum that implements what the protocol/delegate CLLocationManagerDelegate
says, since (a protocol) it's just a blueprint.
(Technical note: Protocols can provide some implementation through protocol extensions, but it is for conforming types, you still can't instantiate a protocol).
Upvotes: 1