gpt4 book ai didi

swift - "Declaring a public let for an internal class"- swift

转载 作者:行者123 更新时间:2023-11-28 10:24:58 24 4
gpt4 key购买 nike

我正在尝试制作一个使用 GPS 的应用程序,下面是我的 GPS Controller 类。如果我尝试将其声明为 public,我的纬度和经度元组会发出编译器警告。

class CoreLocationController : NSObject, CLLocationManagerDelegate {
var locationManager:CLLocationManager = CLLocationManager()
public let ltuple: (latitude:Double, longitude:Double)?;
let location: CLLocation?
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
println("didChangeAuthorizationStatus")
switch status {

case .NotDetermined:
println(".NotDetermined")
self.locationManager.requestAlwaysAuthorization() //Will use information provided in info.plist
break

case .Authorized:
println(".Authorized")
self.locationManager.startUpdatingLocation()
break
...

};
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
//Important things
let location = locations.last as CLLocation;
let ltuple = (location.coordinate.latitude, location.coordinate.longitude)
let geocoder = CLGeocoder()
//Printing
let str = "didUpdateLocations: "+String(format:"%f", location.coordinate.latitude)+String(format:"%f", location.coordinate.longitude);
println(str)
println(ltuple)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error)
}
};

当我试图摆脱第二个 let 时,它给我“无法将 ltuple 分配给 self”。有什么想法吗?

最佳答案

你的类天生就有一个internal class隐式声明,你只是写class SomeClass但代码实际上是internal class SomeClass

如果你想在你的类中拥有public属性/函数/等,你必须首先将你的类声明为public

公共(public)类 SomeClass

public let someImmutableProperty

您可以在 Apple 文档中阅读有关访问控制的所有信息:The Swift Programming Language: Access Control

我个人比较喜欢这篇文章,非常简洁,简化了Access Control的概念.

此外,您不应该在 Swift 代码中使用 ;,请参阅此 Swift Style Guide供大家引用。

关于swift - "Declaring a public let for an internal class"- swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28651805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com