I am a beginner level programmer in iOS app development using Swift. Now I am facing the compile time issue "UInt8 is not convertible to CGFloat"
var numberOfAvatars:Int = 8
let count:Int = 1
let columns:Int = 3
let dimension:CGFloat = 84.0
var spacing:CGFloat = (avatarContentcroll.frame.size.width - columns * dimension)/(columns+1)
var scHeight:CGFloat = spacing + (numberOfAvatars/columns) * (dimension + spacing)
I've tried all the solutions out there and did many experiments. And I am not sure why still I am getting this error.
You have to do it explicitly as
var spacing:CGFloat = CGFloat((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = CGFloat(spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing))
or you can try converting every expression in CGFloat
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)
You must explicitly cast your Ints as CGFloats (via as), or construct them -- like so:
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)
Related
How do I implement dynamic data to my CorePlot? I'd like my graph to look like this:
How should I implement my JSON API and to decode the values to set it as X-axis?
JSON API: https://dcmicrogridiep.000webhostapp.com/datamainjson.php
This is my sample code for the CorePlot graph which uses hard coded values.
import Foundation
import SwiftUI
import CorePlot
class CalculatePlotData: ObservableObject {
var plotDataModel: PlotDataClass? = nil
func plotYEqualsX()
{
//set the Plot Parameters
plotDataModel!.changingPlotParameters.yMax = 10.0
plotDataModel!.changingPlotParameters.yMin = -5.0
plotDataModel!.changingPlotParameters.xMax = 10.0
plotDataModel!.changingPlotParameters.xMin = -5.0
plotDataModel!.changingPlotParameters.xLabel = "x"
plotDataModel!.changingPlotParameters.yLabel = "y"
plotDataModel!.changingPlotParameters.lineColor = .red()
plotDataModel!.changingPlotParameters.title = " y = x"
plotDataModel!.zeroData()
var plotData :[plotDataType] = []
for i in 0 ..< 120 {
//create x values here
let x = -2.0 + Double(i) * 0.2
//create y values here
let y = x * 8
let dataPoint: plotDataType = [.X: x, .Y: y]
plotData.append(contentsOf: [dataPoint])
}
plotDataModel!.appendData(dataPoint: plotData)
}
func ploteToTheMinusX()
{
//set the Plot Parameters
plotDataModel!.changingPlotParameters.yMax = 10
plotDataModel!.changingPlotParameters.yMin = -3.0
plotDataModel!.changingPlotParameters.xMax = 10.0
plotDataModel!.changingPlotParameters.xMin = -3.0
plotDataModel!.changingPlotParameters.xLabel = "x"
plotDataModel!.changingPlotParameters.yLabel = "y = exp(-x)"
plotDataModel!.changingPlotParameters.lineColor = .blue()
plotDataModel!.changingPlotParameters.title = "exp(-x)"
plotDataModel!.zeroData()
var plotData :[plotDataType] = []
for i in 0 ..< 60 {
//create x values here
let x = -8.0 + Double(i) * 5
//create y values here
let y = exp(-x)
let dataPoint: plotDataType = [.X: x, .Y: y]
plotData.append(contentsOf: [dataPoint])
}
plotDataModel!.appendData(dataPoint: plotData)
return
}
}
If you don't need the actual date values elsewhere in the app, you can just send the numbers in the "times" field provided by the API to the plot directly. You'll set the plot ranges based on those values and define a label formatter with the correct reference date to properly display the date values. Look at the DatePlot example app for a simple Swift example.
I am making a note writing app. In this app I am turning the lines into a bezier curve. At the moment I am getting an error when I try to put new values into a matrix that was defined outside of the while loop. I have tried the expression by itself when put into a dummy variable and it works in the loop. When I use the format for replacing a value of the two dimensions variable it works outside the loop, however, when I do both those actions together in the loop it does not work.
//: Playground - noun: a place where people can play
import UIKit
var x2 = 6.0
var y2 = 5.0
var x1 = 1.0
var y1 = 1.0
var p1x = 2.0
var p1y = 3.0
var B = 6.5
var Ba = 2.2
var percentall = Ba/B
var yall = 5.0
var xall = 1.0
var A = 4.0
var C = 5.0
var Ct = 0.0
var At = 0.0
var Aall = Int(A) * 10
var Call = Int(C) * 10
var thelistx = [[Double]](count: Call, repeatedValue: [Double](count: Call, repeatedValue: 0.0 ))
var thelisty = Array(count: Aall, repeatedValue: Array(count: Call, repeatedValue: 0.0 ))
var theAt = 0
var theCt = 0
while At <= A {
var Apercent = percentall * At
var Aend = y1 + At
var yslope = (A - At) * percentall + Aend
var lefty = (yslope - Apercent) * percentall + Apercent
var righty = ( y2 - yslope) * percentall + yslope
while Ct <= C {
var Cpercent = percentall * Ct
var Cend = x2 - Ct
var xslope = (C - Ct) * percentall + Cend
var leftx = (xslope - x1) * percentall + x1
var rightx = (x2 - xslope) * percentall + xslope
thelistx [theAt] [theCt] = (rightx - leftx) * percentall + left
*********************Execution was interrupted, reason exc_bad_instruction (code=exc_i386_invop, subcode=0x0) ***************
thelisty [theAt] [theCt] = (righty - lefty) * percentall + lefty
Ct += 0.1
theCt += 1
}
At = At + 0.1
theAt += 1
}
The console shows what your error is:
fatal error: Index out of range
You're going out of bounds with thelistx [theAt] [theCt]
Changing while Ct < C to Ct < C - 1 keeps your calls in the bounds of the array.
I am learning how to make a multiplication table in swift and used
override func viewDidLoad() {
let n = Int(str)!
while (i<=10) {
let st = "\(n) * \(i) = \(n * i)"
lbl.text = st
i += 1
}
this code. i have a label in which i want to show the table, but the problem is that only the last result which is say 2*10 = 20 is showing and not all the other value. i am confused what to do, please help what to do so that all the values are displayed.
Glad you've decided to learn Swift. You're on the right track, but as others have said, your final iteration of the loop is replacing the contents of lbl.text.
There are many ways to achieve what you want, but for problems like this I'd suggest starting off working in a playground rather than worrying about labels, viewDidLoad and suchlike.
Here's a nice Swift-y way to do what you want
let n = 12
let table = Array(0...10).map({"\(n) * \($0) = \(n * $0)"}).joinWithSeparator("\n")
print("\(table)")
Gives…
12 * 0 = 0
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120
To break that down…
// Take numbers 0 to 10 and make an array
Array(0...10).
// use the map function to convert each member of the array to a string
// $0 represents each value in turn.
// The result is an array of strings
map({"\(n) * \($0) = \(n * $0)"}).
// Join all the members of your `String` array with a newline character
joinWithSeparator("\n")
Try it for yourself. In Xcode, File -> New -> Playground, and just paste in that code. Good luck!
That's because every time the loop iterates, it overwrites the previous value in label.text. You need to append the new value to existing string value in label, as suggested by RichardG.
let n = Int(str)!
while (i<=10) {
let st = "\(n) * \(i) = \(n * i)"
lbl.text = lbl.text + " " +st //Append new value to already existing value in label.text
i += 1
}
There is also a possibility of UI issue. You have to provide number of lines to the label or it will mess up the display. It also needs to be of size enough to hold your data. A better option would be UITextView which is scrollable, if you are unwilling to handle cases for label height and width. But if you want to stick with UILabel, the following code will resize the label depending on text for you:
lbl.numberOfLines = 0; //You only need to call this once. Maybe in `viewDidLoad` or Storyboard itself.
lbl.text = #"Some long long long text"; //here you set the text to label
[lbl sizeToFit]; //You must call this method after setting text to label
You can also handle that by Autolayout constraints.
Easy way to do it with SWIFT 2.0
var tableOf = 2 //Change the table you want
for index in 1...10 {
print("\(tableOf) X \(index) = \(index * tableOf)")
}
OUTPUT
repeat-while loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does).
1) repeat...while Loop
var i = Int()
repeat {
print("\(i) * \(i) = \(i * 11)")
i += 1
} while i <= 11
2) While Loop
var i = Int()
while i <= 11
{
print("\(i) * \(i) = \(i * 11)")
i += 1
}
3) For Loop
for n in 1..<11
{
print("\(n) * \(n) = \(n * 10)")
}
I am writing an app, and I have a block of code that reads like this:
let DestViewController: ftrViewController = segue.destinationViewController as! ftrViewController
let weightInt: Int? = Int(weightInKilos.text!)
let dehydrationInt: Int? = Int(percentOfDehydration.text!)
let lossesInt: Int? = Int(ongoingLosses.text!)
let factorFloat: Float? = Float(Factor.text!)
let lrs24Int = (30 * weightInt! + 70) * factorFloat! + weightInt! * dehydrationInt! * 10 + lossesInt!
However, Xcode says that Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions.
My equation looks right to me, and I do not believe that my equation is too complex, because I had the same error when the problem was simply that I wasn't declaring the integers correctly (the deal with the ?s and the !s).
Does anybody see a problem in my code that is leading to this error, or is the expression truly too hard for the computer to solve in reasonable time? Thanks!
PS- I think the problem might be the float, because before I added the float, it was working fine.
Rewritten Code
let weightInt: Float? = Float(weightInKilos.text!)
let dehydrationInt: Float? = Float(percentOfDehydration.text!)
let lossesInt: Float? = Float(ongoingLosses.text!)
let factorFloat: Float? = Float(Factor.text!)
let step1 = (30 * weightInt! + 70) * factorFloat! + weigh
let lrs24Int = step1 * dehydrationInt! * 10 + lossesInt!
It was not Only Float problem. I used float to avoid type conversions as all are float. The Main Problem was the Complexity of expression.
I am trying to execute the following code and getting the error Could not find member 'subscript on Xcode6
var b:[[Float]] = [[1,2]]
var i2 = 0 // second to last control point
var pointIm1 = CGPoint(x: 0.0,y: 0.0)
var pointI = CGPoint(x: 0.0,y: 0.0)
var pointIp1 = CGPoint(x: 0.0,y: 0.0)
var px:Float = b[0][0] * Float(pointIm1.x) + b[0][0] * Float(pointI.x) + b[0][0] + b[0][0] * Float(pointIp1.x)
Anyone has idea why is giving error
Edit: Do anyone have better idea or i need to create different variables for each subscript as Nate suggested in his answer
//not looks good
var bj0 = b[j][0]
var bj1 = b[j][1]
var bj2 = b[j][2]
var bj3 = b[j][3]
var px:Float = bj0 * Float(pointIm1.x) + bj1 * Float(pointI.x) + bj2 + bj3 * Float(pointIp1.x)
It isn't a problem with the subscript - Swift gets into trouble when trying to parse longer multi-item expressions like your final line. In a playground I get this error:
expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
You'll need to refactor that line to get past this limitation - something like:
let b00 = b[0][0]
var px: Float = b00 * Float(pointIm1.x) + b00 * Float(pointI.x) + b00 + b00 * Float(pointIp1.x)
You could probably just break it up into two steps, like this:
var px:Float = bj0 * Float(pointIm1.x) + bj1 * Float(pointI.x)
px += bj2 + bj3 * Float(pointIp1.x)
It looks like a compiler bug. I tried this expression (obtained from yours, and removing all multiplications):
var px:Float = b[0][0] + b[0][0] + b[0][0] + b[0][0]
and it fails, whereas this:
var px:Float = b[0][0] + b[0][0] + b[0][0]
works.
So Nate Cook's answer is correct, it's the compiler getting into trouble.
More tests - using a unidimensional array, this works:
var c:[Float] = [1.0, 2.0]
var px2: Float = c[0] + c[0] + c[0] + c[0] + c[0]
but this doesn't
var px2: Float = c[0] + c[0] + c[0] + c[0] + c[0] + c[0] // Cannot invoke '+' with arguments of type '($T28, $T32)'
Note that your expression can be refactored as:
var px:Float = b[0][0] * (Float(pointIm1.x) + Float(pointI.x) + 1.0 + Float(pointIp1.x))