Tutorial by Examples: at

The JSONSerialization class is built into Apple's Foundation framework. 2.2 Read JSON The JSONObjectWithData function takes NSData, and returns AnyObject. You can use as? to convert the result to your expected type. do { guard let jsonData = "[\"Hello\", \"JSON\"]&q...
An enum provides a set of related values: enum Direction { case up case down case left case right } enum Direction { case up, down, left, right } Enum values can be used by their fully-qualified name, but you can omit the type name when it can be inferred: let dir = Dire...
Enum cases can contain one or more payloads (associated values): enum Action { case jump case kick case move(distance: Float) // The "move" case has an associated distance } The payload must be provided when instantiating the enum value: performAction(.jump) performA...
This example assumes Ruby and Ruby on Rails have already been installed properly. If not, you can find how to do it here. Open up a command line or terminal. To generate a new rails application, use rails new command followed by the name of your application: $ rails new my_app If you want to c...
Logical OR (||), reading left to right, will evaluate to the first truthy value. If no truthy value is found, the last value is returned. var a = 'hello' || ''; // a = 'hello' var b = '' || []; // b = [] var c = '' || undefined; // c = undefined var d = 1 |...
Decorators augment the behavior of other functions or methods. Any function that takes a function as a parameter and returns an augmented function can be used as a decorator. # This simplest decorator does nothing to the function being decorated. Such # minimal decorators can occasionally be used ...
As mentioned in the introduction, a decorator is a function that can be applied to another function to augment its behavior. The syntactic sugar is equivalent to the following: my_func = decorator(my_func). But what if the decorator was instead a class? The syntax would still work, except that now m...
Decorators normally strip function metadata as they aren't the same. This can cause problems when using meta-programming to dynamically access function metadata. Metadata also includes function's docstrings and its name. functools.wraps makes the decorated function look like the original function b...
A decorator takes just one argument: the function to be decorated. There is no way to pass other arguments. But additional arguments are often desired. The trick is then to make a function which takes arbitrary arguments and returns a decorator. Decorator functions def decoratorfactory(message): ...
The math module contains the math.sqrt()-function that can compute the square root of any number (that can be converted to a float) and the result will always be a float: import math math.sqrt(9) # 3.0 math.sqrt(11.11) # 3.3331666624997918 math.sqrt(Decimal('6.25')) ...
Sometimes you don't want to have your function accessible/stored as a variable. You can create an Immediately Invoked Function Expression (IIFE for short). These are essentially self-executing anonymous functions. They have access to the surrounding scope, but the function itself and any internal va...
A traditional for-loop A traditional for loop has three components: The initialization: executed before the look block is executed the first time The condition: checks a condition every time before the loop block is executed, and quits the loop if false The afterthought: performed every time a...
import random randint() Returns a random integer between x and y (inclusive): random.randint(x, y) For example getting a random number between 1 and 8: random.randint(1, 8) # Out: 8 randrange() random.randrange has the same syntax as range and unlike random.randint, the last value is no...
Protocols may define associated type requirements using the associatedtype keyword: protocol Container { associatedtype Element var count: Int { get } subscript(index: Int) -> Element { get set } } Protocols with associated type requirements can only be used as generic constraints:...
Standard Creation It is recommended to use this form only when creating regex from dynamic variables. Use when the expression may change or the expression is user generated. var re = new RegExp(".*"); With flags: var re = new RegExp(".*", "gmi"); With a backsl...
Match Using .exec() RegExp.prototype.exec(string) returns an array of captures, or null if there was no match. var re = /([0-9]+)[a-z]+/; var match = re.exec("foo123bar"); match.index is 3, the (zero-based) location of the match. match[0] is the full match string. match[1] is the t...
var re = /[a-z]+/; if (re.test("foo")) { console.log("Match exists."); } The test method performs a search to see if a regular expression matches a string. The regular expression [a-z]+ will search for one or more lowercase letters. Since the pattern matches the string,...
Assuming you set the upstream (as in the "setting an upstream repository") git fetch remote-name git merge remote-name/branch-name The pull command combines a fetch and a merge. git pull The pull with --rebase flag command combines a fetch and a rebase instead of merge. git pull ...
Setting a specific Seed will create a fixed random-number series: random.seed(5) # Create a fixed state print(random.randrange(0, 10)) # Get a random integer between 0 and 9 # Out: 9 print(random.randrange(0, 10)) # Out: 4 Resetting the seed will create the same &qu...
x > y x < y These operators compare two types of values, they're the less than and greater than operators. For numbers this simply compares the numerical values to see which is larger: 12 > 4 # True 12 < 4 # False 1 < 4 # True For strings they will compare lexicographical...

Page 8 of 442