热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

尝试使用委托时SwiftEXC_BAD_INSTRUCTION-SwiftEXC_BAD_INSTRUCTIONwhentryingtousedelegate

ImtryingtoimplementsamelogicfordifferenttabsiniOSapplication,butgetThread1:EXC_BAD_

I'm trying to implement same logic for different tabs in iOS application, but get Thread 1: EXC_BAD_INSTRUCTION (code = EXC_1386_INVOP, subcode 0x0). It's a simple application that should allow user to mark some points on the map(right now with annotations), and draws lines between them in process.

我正在尝试为iOS应用程序中的不同选项卡实现相同的逻辑,但获取线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码0x0)。这是一个简单的应用程序,应该允许用户在地图上标记一些点(现在带有注释),并在进程中绘制它们之间的线。

Class that contains logic:

包含逻辑的类:

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/test@GoogleMapsCompatible/{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();
        }
    }
}

ViewController classes look like this:

ViewController类如下所示:

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()
}
}

I guess it's because i don't declare a delegate right. I tried to do this in my MapController class like this:

我想这是因为我没有宣布代表权利。我尝试在我的MapController类中执行此操作,如下所示:

self.Map = _Map
Map.delegate = BuildTrack.self()

, but got same exception when i clicked map(now i don't even see the map, it crashes in the init of MapController), looks like something gets disposed before time.

,但是当我点击地图(现在我甚至没有看到地图,它在MapController的init中崩溃)时得到了相同的异常,看起来像是在时间之前被处理掉的东西。

Is the problem really in delegates, and is this approach ok? When i had one ViewController and all logic was inside it, it worked fine, problem occurred when i tried to separate logic from interface.

问题是否真的在代表中,这种方法是否正常?当我有一个ViewController并且所有逻辑都在其中时,它工作正常,当我试图将逻辑与接口分开时出现问题。

1 个解决方案

#1


1  

I see problem in using outlet testRef before it have value

我看到在有价值之前使用outlet testRef的问题

Try to add ! in mapController declaration

尝试添加!在mapController声明中

var mapController: MapController!

and remove mapController initialization from init:

并从init中删除mapController初始化:

required init?(coder aDecoder : NSCoder) {
    super.init(coder: aDecoder)
}

推荐阅读
author-avatar
李亞男大宝贝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有