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

标签 swift gps

我正在尝试制作一个使用 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/

相关文章:

ios - GKLeaderboard.localPlayerScore 在 iOS 8.1.3 上返回 nil?

ios - 为什么建议的 Swift 单例实现使用结构?

ios - Swift:移动 UIView

swift - 如何更改 NSButton 的位置?

java - Android GPS 应用程序有时会崩溃

swift - Realm 方法 addOrUpdateObject() 是否可以从 Swift 调用?

iphone - CLLocationManager Simulator 来模拟汽车移动?

安卓 : LocationManager vs Google Play Services

python - 扭转多个端口

Android:并非所有运营商都提供 CellID?