Please Help Me Intepret This Code Swift

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"
}

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

Willset does not retain value after initializing

Since its an optional the regular set will not work

Not correct. Since it's not a computed property set will not work.

You need to reread the section about willSet and didSet. willSet is called before any value is written to the variable. I.e. whatever you write to the variable inside willSet will be overwritten immediately by the assignment that invokes your code.

If you assign nil you will overwrite your string array with nil.

Simplify by just doing

var arrayOfString: [String] = []

Convert code from Objective-C to Swift

__FLT_EPSILON__ is a predefined macro of the compiler, apparently not available
in Swift. But <float.h> defines

#define FLT_EPSILON __FLT_EPSILON__

and this is available in Swift as well:

let hasBlur = blurRadius > FLT_EPSILON

Update: Starting with Swift 4, FLT_EPSILON is deprecated. See Hiren Panchal's answer for an up-to-date solution.



Related Topics



Leave a reply



Submit