My Preparation for Agoda Test
I have 3 days to do the challenge. This is going to be my strategy.
- Read through and make notes on Swift Language Guide and Swift Language Reference
- Read through my Objc.io PDF on the MVVM, MVC pattern etc.
- Read up on Core Data, NSNotification, UITableView, UICollectionView.
- Read through how NSURLSession, NSURL and iOS Networking works
- Practice some Hackerrank challenges.
If this is possible or not I’m not sure. But I will sure try to achieve it.
Swift Basics
- Fundamental Types: Bool, Double, Float, Int, sString
- Collection Types: Array, Set, Dictionary
- Tuples
- Optionals
Swift is a type-safe language
- Constants vs Variables.
- Type Annotations
- Type Safety and Type Inference
- Naming Constants and Variables
- Printing Constants and Variables
- Default Parameter Values
- String Interpolation
- Comments, Multiline Comments, Nested Multiline Comments
- Semicolons
Integers
Integers are whole numbers with no fractional component, such as 42 and -23. Integers are either signed (positive, zero, or negative) or unsigned (positive or zero).
- Integer Bounds
Swift provides an additional integer type, Int, which has the same size as the current platform’s native word size:
On a 32-bit platform, Int is the same size as Int32. On a 64-bit platform, Int is the same size as Int64.
- UInt
Floating-Point Numbers
Floating-point numbers are numbers with a fractional component, such as 3.14159, 0.1, and -273.15.
Floating-point types can represent a much wider range of values than integer types, and can store numbers that are much larger or smaller than can be stored in an Int |
- Double
- Float
Type Safety and Type Inference
Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code requires a String, you can’t pass it an Int by mistake.
- Literal Value
Numeric Literals
- A decimal number, with no prefix
- A binary number, with a 0b prefix
- An octal number, with a 0a prefix
- A hexadecimal number, with a 0x prefix
- An exponential number, with an e suffix
-
An exponential hexadecimal number, with a p suffix
- Number padding, 100_000_000
Numeric Type Conversion
Use other integer types only when they’re specifically needed for the task at hand, because of explicitly sized data from an external source, or for performance, memory usage, or other necessary optimization.
Integer Conversion
You must opt in to numeric type conversion on a case-by-case basis.
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
You need to use a type for which UInt16 has an initializer.
Integer and Floating-Point Conversion
Conversions between integer and floating-point numeric types must be made explicit.
Floating-point values are always truncated when used to initialize a new integer value |
Type Aliases
typealias SpecificType = Array<String>
Booleans
Tuples
Tuples group multiple values into a single compound value. The values within a tuple can be of any type and don’t have to be of the same type as each other.
Tuple Decomposition:
- Tuple Pattern Matching with an _
- Access the individual element values in a tuple using index numbers
- Name the individual elements in a tuple
- Using Tuples and Functions with Multiple Return Values
Optionals
- Unwrapping an optional value.
- Swift Optionals let you indicate the absence of a value for any type at all.
- Assigning nil to an Optional.
- Nil cannot be assigned to non-Optional types.
- Optionals without a default value are automatically set to nil.
- In Swift nil isn’t a pointer - it’s the absence of a value of a certain type. Unlike in ObjC where nil is a pointer to a nonexistent object.
If Statements and Forced Unwrapping
- Force Unwrapping. !
- “Not equal” to nil
Optional Binding
- if let
- while let
- Multiple optional bindings within an if statement separated by commas
Constants and variables created with optional binding in an if statement are available only within the body of the if statement. In contrast, the constants and variables created with a guard statement are available in the lines of code that follow the guard statement, as described in Early Exit . |
Implicitly Unwrapped Optionals
- Unowned References and Implicitly Unwrapped Optional Properties.
If an implicitly unwrapped optional is nil and you try to access its wrapped value, you’ll trigger a runtime error.
Don’t use an implicitly unwrapped optional when there’s a possibility of a variable becoming nil at a later point. Always use a normal optional type if you need to check for a nil value during the lifetime of a variable.
Error Handling
func canThrowAnError() throws {
// this function may or may not throw an error
}
- A do statement creates a new containing scope, which allows errors to be propagated to one or more catch clauses.
Assertions and Preconditions
Assertions and preconditions are checks that happen at runtime. You use them to make sure an essential condition is satisfied before executing any further code.
You use assertions and preconditions to express the assumptions you make and the expectations you have while coding, so you can include them as part of your code. Assertions help you find mistakes and incorrect assumptions during development, and preconditions help you detect issues in production.
Debugging with Assertions
assert(_:_:file:line:)
assertionFailure(_:file:line:)
Enforcing Preconditions
precondition(_:_:file:line:)
preconditionFailure(_:file:line:)
fatalError(_:file:line:)
Basic Operators
Arithmetic operators (+, -, *, /, % and so forth) detect and disallow value overflow, to avoid unexpected results when working with numbers that become larger or smaller than the allowed value range of the type that stores them.
- Unary
- Binary
- Ternary
Assignment Operator
let a = 10
var b = 20
let (x, y) = (1, 2)
Arithmetic Operators
- Addition (+)
- Subtraction (-)
- Multiplication (*)
-
Division (/)
-
Overflow Operators
- The addition operator is also supported for
String
concatenation.
Remainder Operator
a = (b x some multiplier) + remainder
The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer.
Unary Minus Operator
Unary Plus Operator
Compound Assignment Operators
a += 2
- Operator Declarations
Comparison Operators
- Equal to (a == b)
- Not Equal to (a != b)
- Greater than (a > b)
- Less than (a <> b)
- Greater than or equal to (a >= b)
-
Less than or equal to (a <= b)
-
Identity Operators
- Tuples are compared from left to right, one value at a time, until the comparison finds two values that aren’t equal.
("blue", -1) < ("purple", 1) // OK, evaluates to true
("blue", false) < ("purple", true) // Error because < can't compare Boolean values
Ternary Conditional Operator
Nil-Coalescing Operator
Range Operators
Closed Range Operator
for index in 1...5 {}
Half-Open Range Operator
for i in 0..<count {}
One-Sided Ranges
for name in names[2...] {}
for name in names[...2] {}
for name in names[..<2] {}
Logical Operators
- Logical NOT (!a)
- Logical AND (a && b)
-
Logical OR (a || b)
- Short-circuit evaluation
Combining Logical Operators
The Swift logical operators && and || are left-associative, meaning that compound expressions with multiple logical operators evaluate the leftmost subexpression first.
Explicit Parentheses
Strings and Characters
Swift’s String type is bridged with Foundation’s NSString class. Foundation also extends String to expose methods defined by NSString. This means, if you import Foundation, you can access those NSString methods on String without casting.
String Literals
- Multiline String Literals
let quote = """
Hello \
Hello
"""
Special Characters in String Literals
-
The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), " (double quotation mark) and ' (single quotation mark)
-
An arbitrary Unicode scalar value, written as \u{n}, where n is a 1–8 digit hexadecimal number (Unicode is discussed in Unicode below)
Extended String Delimiters
let stringDelimiters = #"""
Here are three more double quotes: """
"""#