I've been working on nutrition app for ios lately. I'm trying to write this algorithm
weight (lb) / [height (in)]2 x 703
in xcode with swift, but I'm beginner to use swift. I wrote this code
let calculate = ((textforweight.text) / ((textforheight.text)*(textforheight.text))*703)
and it gives me error like "Type of expression is ambiguous without more context" that. Can you help me for this. It might be so easy but I just don't know how to do it
UITextField .text property return a String value.So you need to convert this value to Int first
let weight = Int(textforweight.text!) ?? .zero
let height = Int(textforheight.text!) ?? .zero
Now You can Calculate you result,
let result = weight / height * height * 703
UITextField .text Property Documentation
Related
iOS 15, Swift 5.5
Sorry, stupid easy question??
I am working on an image processing app, and I have this.
let pixels = UnsafeMutableBufferPointer<UInt32>(start: rawData, count: width * height)
Which I want to break into two pieces, so I tried this.
let fullSet = width * height
let halfSet = fullSet / 2
let pix1 = UnsafeMutableBufferPointer<UInt32>(start: rawData, count: halfSet)
let pix2 = UnsafeMutableBufferPointer<UInt32>(start: (rawData + halfSet), count: halfSet)
I suspect my problem is that the UInt32 is in fact a UInt24 + UInt8, as in red+green+blue and alpha channel.
I count the colours in my pixel Set and I got 120,433... but if I convert to an array I end up with 57,142.
Ok,
I found the answer, thanks everyone for your comments. And yes, it is stupid obvious, you just use a subset.
let pix1 = pixels[(0..<pixels.count / 2)]
let pix2 = pixels[(pixels.count / 2)...]
I was trying too hard I think.
I'm a beginner programmer, and I'm making a game at the moment. I haven't run into many errors like this, but I know it's really easy to fix.
Heres the code:
func randInRange(range: Range<Int>) -> Int {
return Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex))) + range.startIndex }
Here is the constant I'm trying to work with:
let random = randInRange(self.frame.size.width * 0.3...self.frame.size.width * 0.6)
The error comes out as this: Binary operator '...' be applied to 2 CGFloat operands.
Your method randInRange is expecting a range of Integers, so you need to convert the result of your expression from CGFloat to Integer.
let random = randInRange(Int(self.frame.size.width * 0.3)...Int(self.frame.size.width * 0.6))
This question already has answers here:
Multiplying variables and doubles in swift
(2 answers)
So if string is not NilLiteralConvertible... what do some string functions return?
(1 answer)
Closed 7 years ago.
Hello brand new to Swift, and programming in general. Going through an exercise the code given is exactly:
//: Playground - noun: a place where people can play
import UIKit
let height = 12
let width = 10
let area = height * width
let areaInMeters = area / 10.762
But I get the error, "binary operator / cannot be applied to operands of type Int and Double".
After some digging around I found you can't operate on both an Integer and a Double. So I changed the last line to:
let areaInMeters = (Double)area / 10.762
Then I get the error, "Consecutive statements on a line must be separated by a ;" and it wants me to put the ; after area. None of this is making any sense to me.
Using El Capitan beta and Xcode 7 beta.
height and width will both be inferred as of type Int. Therefore area is also of type Int whilst 10.762 is a Double.
And in Swift safety is paramount so you'll need to have both operands of same type.
Solution is (as Eric D. suggested) is to convert area to a Double:
let areaInMeters = Double(area) / 10.762
Try instead adding a decimal point and a zero to the end of your height and width.
Like so:
let height = 12.0
let width = 10.0
And you won't have to worry about having to deal with an Integer.
Hope this helps. Happy Coding!
Current learning Swift, there are ways to find max and min value for different kind of Integer like Int.max and Int.min.
Is there a way to find max value for Double and Float? Moreover, which document should I refer for this kind of question? I am currently reading Apple's The Swift Programming Language.
As of Swift 3+, you should use:
CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude
While there’s no Double.max, it is defined in the C float.h header, which you can access in Swift via import Darwin.
import Darwin
let fmax = FLT_MAX
let dmax = DBL_MAX
These are roughly 3.4 * 10^38 and 1.79 * 10^308 respectively.
But bear in mind it’s not so simple with floating point numbers (it’s never simple with floating point numbers). When holding numbers this large, you lose precision in a similar way to losing precision with very small numbers, so:
let d = DBL_MAX
let e = d - 1.0
let diff = d - e
diff == 0.0 // true
let maxPlusOne = DBL_MAX + 1
maxPlusOne == d // true
let inf = DBL_MAX * 2
// perhaps infinity is the “maximum”
inf == Double.infinity // true
So before you get into some calculations that might possibly brush up against these limits, you should probably read up on floating point. Here and here are probably a good start.
AV's answer is fine, but I find those macros hard to remember and a bit non-obvious, so eventually I made Double.MIN and friends work:
extension Double {
static var MIN = -DBL_MAX
static var MAX_NEG = -DBL_MIN
static var MIN_POS = DBL_MIN
static var MAX = DBL_MAX
}
Don't use lowercase min and max -- those symbols are used in Swift 3.
Just write
let mxFloat = MAXFLOAT
You will get the maximum value of a float in Swift.
Also CGFloat.infinity, Double.infinity or just .infinity can be useful in such situations.
Works with swift 5
public extension Double {
/// Max double value.
static var max: Double {
return Double(greatestFiniteMagnitude)
}
/// Min double value.
static var min: Double {
return Double(-greatestFiniteMagnitude)
}
}
Im very new to Xcode and am trying to make a simple app that calculates Gross Profit.
I am trying to use the following code but it returns the value '0'.
any ideas why?
// Playground - noun: a place where people can play
import UIKit
var costPrice = 10
var salePrice = 100
var grossProfit = ((salePrice - costPrice) / salePrice) * 100
println(grossProfit)
This is all explained in the first few pages of the iBook "Introduction to Swift" that is free and published by Apple.
Swift is type safe and will infer type from context.
The line var costPrice = 10 is inferring that the variable costPrice is an int.
You then can't implicitly combine ints with other types of numbers (doubles for instance).
If you try this..
let costPrice = 10.0
let salePrice = 100.0
let grossProfit = ((salePrice - costPrice) / salePrice) * 100.0
You will find this works.
10 and 100 are integers, so costPrice and salePrice are integers. Integer division truncates as you're seeing. You wanted to use 10.0 and 100.0 here.