Tutorial by Examples: ces

One use of SharedPreferences is to implement a "Settings" screen in your app, where the user can set their preferences / options. Like this: A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things: An XML file to define the av...
Annotation @XmlAccessorType determines whether fields/properties will be automatically serialized to XML. Note, that field and method annotations @XmlElement, @XmlAttribute or @XmlTransient take precedence over the default settings. public class XmlAccessTypeExample { @XmlAccessorType(XmlAccessT...
Interfaces can be extremely helpful in many cases. For example, say you had a list of animals and you wanted to loop through the list, each printing the sound they make. {cat, dog, bird} One way to do this would be to use interfaces. This would allow for the same method to be called on all of th...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
Python lists are zero-indexed, and act like arrays in other languages. lst = [1, 2, 3, 4] lst[0] # 1 lst[1] # 2 Attempting to access an index outside the bounds of the list will raise an IndexError. lst[4] # IndexError: list index out of range Negative indices are interpreted as countin...
dictionary = {"Hello": 1234, "World": 5678} print(dictionary["Hello"]) The above code will print 1234. The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in square brackets. The number 1234 is ...
import random shuffle() You can use random.shuffle() to mix up/randomize the items in a mutable and indexable sequence. For example a list: laughs = ["Hi", "Ho", "He"] random.shuffle(laughs) # Shuffles in-place! Don't do: laughs = random.shuffle(laughs) p...
Using typedef It might be handy to use a typedef instead of declaring the function pointer each time by hand. The syntax for declaring a typedef for a function pointer is: typedef returnType (*name)(parameters); Example: Posit that we have a function, sort, that expects a function pointer to ...
class MyClass { func sayHi() { print("Hello") } deinit { print("Goodbye") } } When a closure captures a reference type (a class instance), it holds a strong reference by default: let closure: () -> Void do { let obj = MyClass() // Captures a strong re...
We have three methods: attr_reader: used to allow reading the variable outside the class. attr_writer: used to allow modifying the variable outside the class. attr_accessor: combines both methods. class Cat attr_reader :age # you can read the age but you can never change it attr_writer...
Ruby has three access levels. They are public, private and protected. Methods that follow the private or protected keywords are defined as such. Methods that come before these are implicitly public methods. Public Methods A public method should describe the behavior of the object being created. T...
git diff This will show the unstaged changes on the current branch from the commit before it. It will only show changes relative to the index, meaning it shows what you could add to the next commit, but haven't. To add (stage) these changes, you can use git add. If a file is staged, but was modi...
git diff --staged This will show the changes between the previous commit and the currently staged files. NOTE: You can also use the following commands to accomplish the same thing: git diff --cached Which is just a synonym for --staged or git status -v Which will trigger the verbose sett...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub. str.replace(old, new[, count]): str.replace takes two arguments old and new containing the old sub-string which is to be replace...
The following examples will use this array to demonstrate accessing values var exampleArray:[Int] = [1,2,3,4,5] //exampleArray = [1, 2, 3, 4, 5] To access a value at a known index use the following syntax: let exampleOne = exampleArray[2] //exampleOne = 3 Note: The value at index two is th...
Individual values of a hash are read and written using the [] and []= methods: my_hash = { length: 4, width: 5 } my_hash[:length] #=> => 4 my_hash[:height] = 9 my_hash #=> {:length => 4, :width => 5, :height => 9 } By default, accessing a key which has not been added t...
A value in a Dictionary can be accessed using its key: var books: [Int: String] = [1: "Book 1", 2: "Book 2"] let bookName = books[1] //bookName = "Book 1" The values of a dictionary can be iterated through using the values property: for book in books.values { ...
Syntax : history.replaceState(data, title [, url ]) This method modifies the current history entry instead of creating a new one. Mainly used when we want to update URL of the current history entry. window.history.replaceState("http://example.ca", "Sample Title", "/exam...
This example show how to subscribe, and once that is successful, publishing a message to that channel. It also demonstrates the full set of parameters that can be included in the subscribe's message callback function. pubnub = PUBNUB({ publish_key : 'your_pub_key', ...
Sorted sequences allow the use of faster searching algorithms: bisect.bisect_left()1: import bisect def index_sorted(sorted_seq, value): """Locate the leftmost value exactly equal to x or raise a ValueError""" i = bisect.bisect_left(sorted_seq, value) ...

Page 2 of 40