1
我試圖在iOS應用程序中爲不同的選項卡實現相同的邏輯,但獲取線程1:EXC_BAD_INSTRUCTION(code = EXC_1386_INVOP,子代碼0x0)。這是一個簡單的應用程序,應該允許用戶在地圖上標記一些點(現在使用註釋),並在它們之間繪製線。試圖使用委託時Swift EXC_BAD_INSTRUCTION
類,它包含的邏輯:
class MapController : NSObject, MKMapViewDelegate{
var Map: MKMapView!
var points : [CGPoint]
init(_Map : MKMapView!, delClass : String)//, coder aDecoder: NSCoder)
{
self.Map = _Map
points = [CGPoint]()
self.Map.mapType = MKMapType.Satellite
let centre = CLLocationCoordinate2D(latitude: 40.0000000,
longitude: 49.0000000)
let span = MKCoordinateSpan(latitudeDelta: 10.01,
longitudeDelta: 10.01)
let region = MKCoordinateRegion(center: centre, span: span)
self.Map.setRegion(region, animated: false)
self.Map.regionThatFits(region)
let urlTemplate = "http://someip/mapcache/tms/1.0.0/[email protected]/{z}/{x}/{y}.png"
let carte_indice = MKTileOverlay(URLTemplate: urlTemplate)
carte_indice.geometryFlipped = true
carte_indice.canReplaceMapContent = false
print("Map")
self.Map.addOverlay(carte_indice)
}
func longPressGesture()
{
let lpg = UILongPressGestureRecognizer(target: self.Map, action: "longPressAction:")
lpg.minimumPressDuration = 1;
Map.addGestureRecognizer(lpg)
}
func longPressAction(myRecognizer : UILongPressGestureRecognizer)
{
let currPoint = myRecognizer.locationInView(Map)
let point = Map.convertPoint(currPoint, toCoordinateFromView: Map)
points.append(currPoint);
if(points.count>1)
{
let startPoint = Map.convertPoint(points[points.count-2], toCoordinateFromView: Map)
let endPoint = Map.convertPoint(currPoint, toCoordinateFromView: Map)
var lineCoords = [startPoint,endPoint]
var line = MKPolyline(coordinates: &lineCoords, count: 2)
var test = MKPolylineRenderer(polyline: line)
test.lineWidth = 10;
test.strokeColor = UIColor.redColor()
Map.addOverlay(line)
}
let myAnnotation = MKPointAnnotation();
myAnnotation.coordinate = point
myAnnotation.title = "Test"
myAnnotation.subtitle = "Test subtitle"
Map.addAnnotation(myAnnotation);
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay);
circle.strokeColor = UIColor.redColor();
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1);
circle.lineWidth = 1;
return circle;
}else if overlay is MKTileOverlay {
var carte_Renderer = MKTileOverlayRenderer(overlay: overlay)
carte_Renderer.alpha = 0.9
return carte_Renderer
}else if overlay is MKPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: overlay);
polylineRenderer.strokeColor = UIColor.blueColor();
polylineRenderer.lineWidth = 5;
return polylineRenderer;
}else {
return MKPolylineRenderer();
}
}
}
視圖控制器類是這樣的:
class BuildTrack: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
@IBOutlet var testRef: MKMapView!
var mapController : MapController!
required init?(coder aDecoder : NSCoder)
{
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
mapController = MapController(_Map: testRef, delClass: "BuildTrack")
mapController.longPressGesture();
testRef.delegate = mapController
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我想這是因爲我不聲明一個代理權。我試圖做這在我MapController類是這樣的:
self.Map = _Map
Map.delegate = BuildTrack.self()
,但得到了同樣的異常,當我點擊地圖(現在我甚至不看地圖,它崩潰的MapController的初始化),看起來像有些東西會在時間之前處理
問題真的出現在代表中,這種方法可行嗎?當我有一個ViewController和所有的邏輯在裏面,它工作正常,當我試圖從接口分離邏輯時發生問題。
我搬到初始化viewDidLoad,不是它給我錯誤「在聲明之前使用局部變量mapController」 – Jamil
@Jamil刪除var關鍵字製作mapController在viewDidLoad –
新來swift,總是認爲編譯器消息我,如果我嘗試聲明變量具有相同的名稱 是的,它的工作,但現在我得到同樣的錯誤在AppDelegate時,我點擊地圖 – Jamil