在Swift中崩溃的地方应该不会崩溃(“if,let,where”声明)

时间:2016-09-17 11:10:25

标签: ios swift crash hockeyapp

我一直在努力应对一场根本不应该发生的非常奇怪的崩溃。我通过Hockeyapp收到崩溃报告,它不断报告应用程序不应该崩溃的行中的崩溃。我已经面对这个问题已经有一个星期。

这是崩溃报告

getY()

代码行:

public Bitmap addrizzone(Bitmap image, ArrayList<myHandle> sourcePoints){
        // sourcePoints are  expected  to be clockwise ordered
        // [top_left,top_right,bottom_right,bottom_left]
        // getting the size of the output image
        double dst_width = Math.max(sourcePoints.get(0).distanceFrom(sourcePoints.get(1)),sourcePoints.get(3).distanceFrom(sourcePoints.get(2)));
        double dst_height = Math.max(sourcePoints.get(0).distanceFrom(sourcePoints.get(3)),sourcePoints.get(1).distanceFrom(sourcePoints.get(2)));

        //determining point sets to get the transformation matrix
        List<org.opencv.core.Point> srcPts = new ArrayList<org.opencv.core.Point>();
        for (myHandle ball : sourcePoints) {
            srcPts.add(new org.opencv.core.Point((ball.getX()),ball.getY()));
        }

        List<org.opencv.core.Point> dstPoints= new ArrayList<org.opencv.core.Point>();
        dstPoints.add(new org.opencv.core.Point(0,0));
        dstPoints.add(new org.opencv.core.Point(dst_width-1,0));
        dstPoints.add(new org.opencv.core.Point(dst_width-1,dst_height-1));
        dstPoints.add(new org.opencv.core.Point(0,dst_height));

        Mat srcMat = Converters.vector_Point2f_to_Mat(srcPts);
        Mat dstMat = Converters.vector_Point2f_to_Mat(dstPoints);

        //getting the transformation matrix
        Mat perspectiveTransformation = Imgproc.getPerspectiveTransform(srcMat,dstMat);

        //getting the input matrix from the given bitmap
        Mat inputMat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);

        Utils.bitmapToMat(image,inputMat);

        Imgproc.cvtColor(inputMat,inputMat,Imgproc.COLOR_RGB2GRAY);

        //getting the output matrix with the previously determined sizes
        Mat outputMat = new Mat((int) dst_height,(int) dst_width,CvType.CV_8UC1);

        //applying the transformation
        Imgproc.warpPerspective(inputMat,outputMat,perspectiveTransformation,new Size(dst_width,dst_height));

        //creating the output bitmap
        Bitmap outputBitmap = Bitmap.createBitmap((int)dst_width,(int)dst_height, Bitmap.Config.RGB_565);

        //Mat to Bitmap
        Imgproc.cvtColor(outputMat,outputMat,Imgproc.COLOR_GRAY2RGB);
        Utils.matToBitmap(outputMat,outputBitmap);

        return outputBitmap;
    }

第755行是if语句。在swift中的“if,let,where”语句中,“let”检查是否存在rescheduleBooking,如果存在并被分配,则执行where语句....我是对的吗?...无论如何,我测试过在我的设备和模拟器本地,它不会崩溃,无论变量的值...它发生在我手上没有的其他设备..

如果您有任何建议,或者如果我没有正确理解“if,let where”条款,我将非常感谢您的帮助和评论。

谢谢..

1 个答案:

答案 0 :(得分:1)

您对“if-let where”条款的理解是正确的,但您必须记住if let booking = rescheduleBooking确保rescheduleBooking不是nil,然后将其分配给{ {1}}。现在即使booking不是booking也不能保证nil不会booking.confirmed所以如果它是nil并且您强行打开它会导致它崩溃。

您可以添加另一个,以确保nil不是booking.confirmed

nil