Swift 没有参数的方法正在调用参数
我有一个名为Swift 没有参数的方法正在调用参数,swift,class,parameters,arguments,Swift,Class,Parameters,Arguments,我有一个名为Location的类,其中有几个没有任何参数的方法 但是,当我尝试用方法的结果创建变量时,它需要一个参数。为什么呢 位置类别: let locationManager = CLLocationManager() public class Location { public func coordinate() -> (latitude: Float?, longitude: Float?) { let latitude = Float((locatio
Location
的类,其中有几个没有任何参数的方法
但是,当我尝试用方法的结果创建变量时,它需要一个参数。为什么呢
位置
类别:
let locationManager = CLLocationManager()
public class Location {
public func coordinate() -> (latitude: Float?, longitude: Float?) {
let latitude = Float((locationManager.location?.coordinate.latitude)!)
let longitude = Float((locationManager.location?.coordinate.longitude)!)
return (latitude: latitude, longitude: longitude)
}
public func getCity() -> String {
var returnCity: String = "N/A"
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// City
if let city = placeMark.addressDictionary!["City"] as? String {
returnCity = city
}
})
return returnCity
}
public func getCountry() -> String {
var returnCountry: String = "N/A"
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// City
if let country = placeMark.addressDictionary!["Country"] as? String {
returnCountry = country
}
})
return returnCountry
}
public func getZip() -> Int {
var returnZip: Int = 0
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// City
if let zip = placeMark.addressDictionary!["ZIP"] as? Int {
returnZip = zip
}
})
return returnZip
}
public func getLocationName() -> String {
var returnName: String = "N/A"
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// City
if let locationName = placeMark.addressDictionary!["Name"] as? String {
returnName = locationName
}
})
return returnName
}
public func getStreetAddress() -> String {
var returnAddress: String = "N/A"
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// City
if let street = placeMark.addressDictionary!["Thoroughfare"] as? String {
returnAddress = street
}
})
return returnAddress
}
}
正在尝试创建变量:
let city = Location.getCity()
以下是我得到的一些屏幕截图:
因为您试图将其作为类函数调用。您应该创建
Location
的实例,并调用该实例上的函数。还请注意,它返回String
,其中您的代码告诉编译器您希望它返回位置
,因为您试图将其作为类函数调用。您应该创建Location
的实例,并调用该实例上的函数。还请注意,它返回String
,其中代码告诉编译器您希望它返回位置
这些方法不是类方法,而是实例方法。您必须在位置
类的实例上调用它们,而不是在类本身上调用它们。显然,Swift可以调用类似于Python的实例方法:该方法是该类拥有的函数,其参数是该类的实例。但不应该以这种方式调用实例方法
解决此问题的最佳方法是构造位置
对象,然后对其调用方法:
let city: Location = Location().getCity()
这些方法不是类方法,而是实例方法。您必须在
位置
类的实例上调用它们,而不是在类本身上调用它们。显然,Swift可以调用类似于Python的实例方法:该方法是该类拥有的函数,其参数是该类的实例。但不应该以这种方式调用实例方法
解决此问题的最佳方法是构造位置
对象,然后对其调用方法:
let city: Location = Location().getCity()