我试图制作一款使用GPS的应用程序,以下是我的GPS控制器类。如果我尝试将其声明为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
"。任何想法?
答案 0 :(得分:4)
您的课程天生就有internal class
声明,您只需撰写class SomeClass
但代码真的是internal class SomeClass
。
如果您希望班级中有public
个属性/函数/等,则必须先将班级声明为public
。
public class SomeClass
public let someImmutableProperty
您可以在Apple的文档中阅读有关访问控制的所有信息:The Swift Programming Language: Access Control
我个人更喜欢这篇文章,它非常简洁并简化了Access Control的概念。
此外,您不应在Swift代码中使用;
,请参阅此Swift Style Guide以供参考。