What Is the '' Is or the ':' at the Equation When Coding in Swift

What is the `?` is or the `:` at the equation when coding in Swift

Operators can be unary, binary, or ternary:

This is Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).

From Apple Documents Basic Operators

Ternary Conditional Operator

The ternary conditional operator is a special operator with three
parts, which takes the form question ? answer1 : answer2. It is a
shortcut for evaluating one of two expressions based on whether
question is true or false. If question is true, it evaluates answer1
and returns its value; otherwise, it evaluates answer2 and returns its
value.

As per your question if isRound is true then corner radios is frame.size.height else it's 2.

As like if condition :

if(self.isRounded){
backgroundView.layer.cornerRadius = frame.size.height * 0.4
}
else{
backgroundView.layer.cornerRadius = 2.0
}

Swift: Multiplication and brackets calculation doesn't work

This should work:

let numerator: Double = (5-2) * (10-5)
let denumerator: Double = (4-2) * (10-5)

Fist you calculate the numerator and denumerator. And finally the result:

print(result)
let result: Double = numerator/denumerator
//1.5

Ternary operation in swift what is this ? symbol

Is called ternary operator, and is not a feature only of Swift programming language, ternary operator is supported for almost all of major programming languages as you can read here Ternary Operator Wikipedia Reference

basically you are doing an if else statement in one line

this let title: String = isPaused ? "Start" : "Pause" is equivalent to this

 let title: String
if isPaused {
title = "Start"
} else {
title = "Pause"
}

The equation will not solve in swift cause of this error

you forgot "let" before root1 and root2:

let a: Double  = 9.1
let b: Double = -1.1
let c: Double = 1.3
let root1: Double = (b + sqrt((b * b) - 4 * a * c)/(2 * a)
let root2: Double = (b + sqrt((b * b) - 4 * a * c)/(2 * a)
print(root1)
print(root2)

There are further issues that I didn't notice, as Duncan pointed out.

You can find out if a quadratic function has zero, one or two distinct real roots by looking at the sign of the discriminant. Since we'll be calculating multiple values (the discriminant, and up to two roots) from a set of related values, that's a perfect chance to use a struct.

Here I model the three Doubles that represent the quadratic function using a struct called... QuadraticFunction. You can then ask that function for the value of its discriminant, and for a solution. I also made a helper function which builds a nice English description of the solution.

import Foundation

struct QuadraticFunction {
let a, b, c: Double

var discriminant: Double { b*b - 4*a*c }

enum Roots {
case two(Double, Double)
case one(Double)
case none
}

func solve() -> Roots {
switch self.discriminant {
case let d where d < 0: return Roots.none // No real roots

case let d where d == 0: return .one((b + sqrt(d)) / (2 * a))

case let d where d > 0: return .two(
(b - sqrt(d)) / (2 * a),
(b + sqrt(d)) / (2 * a)
)

default: fatalError("Unreachable")
}
}
}

extension QuadraticFunction: CustomStringConvertible {
var description: String {
"\(a)x² + \(b)x + \(c)"
}
}

func prettyPrintSolution(of function: QuadraticFunction) {
let solution = function.solve()

switch solution {
case .none: print("The equation \(function) has no real roots.")
case .one(let r): print("The equation \(function) has one distinct root \(r).")
case .two(let r1, let r2): print("The equation \(function) has two distinct roots \(r1) and \(r2).")
}
}

let function1 = QuadraticFunction(a: 9.1, b: -1.1, c: 1.3)
prettyPrintSolution(of: function1)

let function2 = QuadraticFunction(a: 1, b: 0, c: 0)
prettyPrintSolution(of: function2)

let function3 = QuadraticFunction(a: 1, b: 0, c: -1)
prettyPrintSolution(of: function3)

Result:

The equation 9.1x² + -1.1x + 1.3 has no real roots.
The equation 1.0x² + 0.0x + 0.0 has one distinct root 0.0.
The equation 1.0x² + 0.0x + -1.0 has two distinct roots -1.0 and 1.0.

What calculation is this line making?

if splitH is true, it picks height otherwise it picks width, and then subtracts the value Int(min) from the picked value.

Its usual syntax is conditional ? statement if conditional is true : statement if conditional is false



Related Topics



Leave a reply



Submit