Swift Generic Unbound通配符类型

时间:2016-04-16 05:10:17

标签: swift generics

有没有办法在Swift中编写以下内容。我似乎无法为未绑定的通配符类型“?”找到一个好的替代方法。也许这种模式可以在Swift中以不同的方式处理。

class MyDefinition {}

class DefinitionBuilder<B: DefinitionBuilder<?, T>, T> {}

class ObjectDefinitionBuilder<B: ObjectDefinitionBuilder<?, T>, T>: DefinitionBuilder<B, T> {}

class MyDefinitionBuilder: ObjectDefinitionBuilder<MyDefinitionBuilder, MyDefinition> {}

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以将DefinitionBuilder定义为使用protocolassociatedtype的{​​{1}},如下所示:

Self

希望,这会有所帮助。

答案 1 :(得分:0)

我相信你可以使用

import UIKit
import GoogleMaps
import MapKit

class LocationItemViewController: UIViewController,GMSMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var lblInfo: UILabel!    
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var viewMap: UIView!
@IBOutlet weak var googleMapView: GMSMapView!


override func viewDidLoad() {

    super.viewDidLoad()     
    let latitude = (String(format:"%.02f", locationItem.itemLatitude ) as NSString).doubleValue
    let longgitude = (String(format:"%.02f", locationItem.itemLongitude) as NSString).doubleValue
    let camera = GMSCameraPosition.cameraWithLatitude(latitude,longitude: longgitude, zoom: 7)

    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    self.googleMapView = mapView
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake((locationItem.itemLatitude as NSString).doubleValue, (locationItem.itemLongitude as NSString).doubleValue)
    marker.title = "somelocation"
    marker.snippet = "anywhere"
    marker.map = mapView
    lblTitle.text = locationItem.itemName
    lblInfo.text = locationItem.itemAddress
}