Error changing [Xcode] Array array to Double.

Asked 2 years ago, Updated 2 years ago, 40 views

Hello.

After receiving data from the connected Bluetooth and processing it

I'm trying to draw a graph using charts.

Currently, the app consists of three Tabbar Controllers in structure

If each tab bar is A, B, and C, receive and save data from B.

C is going to use the data to show the graph.

B stores data in Array.

public var UserDis : Array<String> = []
UserDis.append(String(valDis))

In C,

var transferedData: Array<String> = SerialViewController().UserDis. 

After loading to

I'm trying to take out each value and convert it to Double and put it into a setChart that draws a chart.

for i in 0...transferedData.endIndex-1{
        let inputData = Double(transferedData[i])!
        setChart(values: [inputData])
    }

However, an error is appearing in this process.

In order to use all elements of transferred data, if you introduce a for statement and do endIndex-1 and execute it

Thread 1: Fatal error: Can't form Range with upperBound < lowerBound

The error appears.

So I was wondering if I was mistaken.

for i in 0...transferedData.endIndex{
        let inputData = Double(transferedData[i])!
        setChart(values: [inputData])
    }

In the process of converting to Double when you modify to run it,

Thread 1: Fatal error: Index out of range

You get this error.

ㅠ<

Even if I search on the Internet, I can't find the wrong part. ㅠ<

How can I fix it?

xcode

2022-09-21 21:51

1 Answers

If transferedData is empty, you must make an exception. transferedData.If endIndex is 0, if -1 is set to that value, The first error you wrote above occurs.

Even if you interpret the error phrase, lowerBound (0 in code) is upperBound (transferredData in code).The situation is that it is not possible to create a range larger than endIndex-1, i.e. -1. If you're not expecting empty data, check out the code on the receiving end of the data.

The second error is

Just in case, I think you imagined that you could do it like this, but the reason why it doesn't work as you expected is right. No way.

It is recommended that you write stride(to:by:) when creating a dynamic range. Safely so that you don't fail to create the scope itself, as you wrote.

If the above code is written only by a well-known for;;, the error is It'll be gone. The difference in performance from the range method is in general situations It's very trivial, so these days, it's mostly divided by the difference in coding taste... Above In situations, I think it could be better to use the for;; phrase That's right.

There is no for;; in switf 4. -_-;

If transferedData is a sliced array, you should be careful to use endIndex when creating a range. It'll be a little complicated to deal with this, but I don't think it's your case, so pass!

Finally, you're using a powerful development tool called Xcode, so if you're in a situation, try debugging it. You will be able to grasp the above problem easily.


2022-09-21 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.