Phong Trịnh Quốc
0 followers · 254 views
on the atlas — 14
- Documentation1 savers
- Getting started with Scrumdinger — iOS App Dev Tutorials | Apple Developer Documentation2 savers
- Firebase Dynamic Links | Firebase Documentation1 savers
- Links | Isar Database1 savers
- Queries | Isar Database1 savers
- SOLID Design Principles Explained: The Liskov Substitution Principle with Code Examples1 savers
- Composition in Java - GeeksforGeeks1 savers
- Favoring Composition Over Inheritance In Java With Examples - GeeksforGeeks1 savers
- Clean Architecture: Component Cohesion Principles | by Rebeca Mora | Medium1 savers
- How can you solve problems using the scientific method?1 savers
- When Enough is Enough - by Mary Hutto Fruchter1 savers
- The Best Decision-Making Is Emotional - Every3 savers
- Every9 savers
- Curius / Onboarding2621 savers
highlights — 160
Use a precondition whenever a condition has the potential to be false, but must definitely be true for your code to continue execution.
DocumentationThe difference between assertions and preconditions is in when they’re checked: Assertions are checked only in debug builds, but preconditions are checked in both debug and production builds. In production builds, the condition inside an assertion isn’t evaluated. This means you can use as many assertions as you want during your development process, without impacting performance in production.
DocumentationBecause a failed assertion or precondition indicates an invalid program state, there’s no way to catch a failed assertion. Recovering from an invalid state is impossible. When an assertion fails, at least one piece of the program’s data is invalid — but you don’t know why it’s invalid or whether an additional state is also invalid.
Documentationassertions and preconditions also become a useful form of documentation within the code
Documentationverifying your expectations at runtime
DocumentationAssertions help you find mistakes and incorrect assumptions during development, and preconditions help you detect issues in production.
Documentationyour app is terminated.
DocumentationYou use them to make sure an essential condition is satisfied before executing any further code
DocumentationAssertions and preconditions are checks that happen at runtime
DocumentationA do statement creates a new containing scope, which allows errors to be propagated to one or more catch clauses.
DocumentationSwift automatically propagates errors out of their current scope until they’re handled by a catch clause.
DocumentationWhen you call a function that can throw an error, you prepend the try keyword to the expression.
DocumentationA function indicates that it can throw an error by including the throws keyword in its declaration.
DocumentationIf an implicitly unwrapped optional is nil and you try to access its wrapped value, you’ll trigger a runtime error. The result is exactly the same as if you write an exclamation point to force unwrap a normal optional that doesn’t contain a value. You can check whether an implicitly unwrapped optional is nil the same way you check a normal optional:
DocumentationYou can think of an implicitly unwrapped optional as giving permission for the optional to be force-unwrapped if needed. When you use an implicitly unwrapped optional value, Swift first tries to use it as an ordinary optional value; if it can’t be used as an optional, Swift force-unwraps the value. In the code above, the optional value assumedString is force-unwrapped before assigning its value to implicitString because implicitString has an explicit, non-optional type of String. In code below, optionalString doesn’t have an explicit type so it’s an ordinary optional.
DocumentationDon’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.
DocumentationImplicitly unwrapped optionals are useful when an optional’s value is confirmed to exist immediately after the optional is first defined and can definitely be assumed to exist at every point thereafter
Documentationou write an implicitly unwrapped optional by placing an exclamation point (String!) rather than a question mark (String?) after the type that you want to make optional. Rather than placing an exclamation point after the optional’s name when you use it, you place an exclamation point after the optional’s type when you declare it.
DocumentationThese kinds of optionals are defined as implicitly unwrapped optionals.
Documentationit’s useful to remove the need to check and unwrap the optional’s value every time it’s accessed, because it can be safely assumed to have a value all of the time.
DocumentationSometimes it’s clear from a program’s structure that an optional will always have a value
DocumentationOptionals can be checked with an if statement to see if a value exists, and can be conditionally unwrapped with optional binding to access the optional’s value if it does exist.
Documentationoptionals indicate that a constant or variable is allowed to have “no value”.
DocumentationConstants 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.
DocumentationIf any of the values in the optional bindings are nil or any Boolean condition evaluates to false, the whole if statement’s condition is considered to be false
DocumentationYou can include as many optional bindings and Boolean conditions in a single if statement as you need to, separated by commas.
DocumentationIf you don’t need to refer to the original, optional constant or variable after accessing the value it contains, you can use the same name for the new constant or variable:
DocumentationBecause this kind of code is so common, you can use a shorter spelling to unwrap an optional value: Write just the name of the constant or variable that you’re unwrapping. The new, unwrapped constant or variable implicitly uses the same name as the optional value.
DocumentationIf the conversion is successful, the actualNumber constant becomes available for use within the first branch of the if statement. It has already been initialized with the value contained within the optional, and has the corresponding non-optional type. In this case, the type of possibleNumber is Int?, so the type of actualNumber is Int.
Documentation“If the optional Int returned by Int(possibleNumber) contains a value, set a new constant called actualNumber to the value contained in the optional.”
DocumentationOptional binding can be used with if, guard, and while statements to check for a value inside an optional, and to extract that value into a constant or variable, as part of a single action
DocumentationYou use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.
DocumentationIn Objective-C, nil is a pointer to a nonexistent object. In Swift, nil isn’t a pointer — it’s the absence of a value of a certain type. Optionals of any type can be set to nil, not just object types.
DocumentationWhen you access an optional value, your code always handles both the nil and non-nil case. There are several things you can do when a value is missing, as described in the following sections: Skip the code that operates on the value when it’s nil. Propagate the nil value, by returning nil or using the ?. operator described in Optional Chaining. Provide a fallback value, using the ?? operator. Stop program execution, using the ! operator.
DocumentationThis separation of optional and non-optional values lets you explicitly mark what information can be missing, and makes it easier to write code that handle missing values
DocumentationYou can’t use nil with non-optional constants or variables. If a constant or variable in your code needs to work with the absence of a value under certain conditions, declare it as an optional value of the appropriate type.
DocumentationYou can use an if statement to find out whether an optional contains a value by comparing the optional against nil. You perform this comparison with the “equal to” operator (==) or the “not equal to” operator (!=).
DocumentationIf you define an optional variable without providing a default value, the variable is automatically set to nil:
DocumentationYou set an optional variable to a valueless state by assigning it the special value nil:
DocumentationYou use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value of a specified type, and you can unwrap the optional to access that value, or there isn’t a value at all.
DocumentationTuples are useful for simple groups of related values. They’re not suited to the creation of complex data structures. If your data structure is likely to be more complex, model it as a class or structure, rather than as a tuple
DocumentationTuples are particularly useful as the return values of functions. A function that tries to retrieve a web page might return the (Int, String) tuple type to describe the success or failure of the page retrieval. By returning a tuple with two distinct values, each of a different type, the function provides more useful information about its outcome than if it could only return a single value of a single type.
DocumentationIf you name the elements in a tuple, you can use the element names to access the values of those elements:
DocumentationYou can name the individual elements in a tuple when the tuple is defined:
DocumentationAlternatively, access the individual element values in a tuple using index numbers starting at zero:
DocumentationIf you only need some of the tuple’s values, ignore parts of the tuple with an underscore (_) when you decompose the tuple:
DocumentationYou can decompose a tuple’s contents into separate constants or variables, which you then access as usual:
DocumentationTo convert one specific number type to another, you initialize a new number of the desired type with the existing value
DocumentationType inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it.
DocumentationIf you don’t specify the type of value you need, Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.
Documentation