简体   繁体   中英

swift : Cannot convert value of type '()' to expected argument type '[Double]'

I am Using Charts on iOS App.

It will get data from bluetooth and save as Array = []

It has to convert to [Double] when It draws charts

func setChart(values: [Double]) {
        // mChart.noDataText = "No data available!"
        for i in 0..<values.count {
            print("chart point : \(values[i])")
            let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
            dataEntries.append(dataEntry)
        }

These are the codes that draw graph.

for i in 1...transferedData.endIndex-1{
        setChart(values : transferedData[i]) // Cannot convert value of type 'String' to expected argument type '[Double]'
       setChart(values: transferedData.map{ Double($0)}) //Cannot convert value of type 'Double?' to closure result type 'Double'
    }

These are the codes that I have tried to add data to setChart from transferedData.

I have found the internet but I couldn't solve this.

What should I do for this problem?

************************** Edited ********************************

I have two Swift file Now.

One is for data transfer and the other is for draw graph.

this is for saving and processing data. (SerialViewController.swift)

public var UserDis : Array<String> = []

func getRealDataProcess(){        
        splitUserData = receivedDataForReal.split(separator: ";") 
        for j in 0...splitUserData.endIndex-1 { 
            if splitUserData[j].count == 14{ 
                checkedUser14Data.append(String(splitUserData[j]))
            }
        }

        for k in 0...checkedUser14Data.endIndex-1{
            var strForCal = checkedUser14Data[k]
            var valDis = strForCal[strForCal.startIndex...strForCal.index(strForCal.startIndex, offsetBy: 4)] 
            UserDis.append(String(valDis))
        }

    }

And this is for drawing graph. (GraphViewController.swift)

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

Thread 1: Fatal error: Can't form Range with upperBound < lowerBound is occurring now at the transferredData.endIndex-1

Have you tried the code below? I am using the if statement to convert the data from being optional.

var transferedData: [String] = ["12.2","24.3","23"]

for data in transferedData {
if let convertedData = Double(data){
   setChart(values : cobvertedData)
  }
}

I tried using the for loop you used

for i in 1...transferedData.endIndex-1 

but it would leave out one array making the data set incomplete.

As the error message clearly states you can not return an optional double so either give a default value or use compactMap to ignore all values that can not be convert

Default value

setChart(values: transferedData.map{ Double($0) ?? 0.0}) 

compactMap

setChart(values: transferedData.compactMap{ Double($0) }) 

And you don't need a loop when using map or compactMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM