[iOS 7 Stanford 2013 Fall] Lecture 4 Foundation and Attributed String

More Objective-C

Creating Objects

1. alloc and int

2. Class Methods

3. Sometimes Both a Class Method and init Method Exist

4. Other Objects Creates New Objects

nil

  • Sending messages to nil is (mostly) okay. No code executed.
  • Be careful if the method returns a C struct. Return value is undefined.

id

  • id means pointer to an object of unknown/unspecified type.
  • Really all object pointers are treated like id at run time.
  • Figuring out the code to execute when a message is sent at runtime is called “dynamic binding“.
  • Program crashs when someone is sending messages to an object that it doesn’t understand.
  • In order to be safe, we mostly use static type (e.g. NSString *, point out the type explicitly) and the compiler is really smart.
  • Compiler will not complain about assignment between an id and a statically typed variable.
  • Never use id *, because id is alreay a pointer.

Introspection

  • Asking at runtime what class an object is or what messages can be sent to it.
  • isKindOfClass: inheritance included
  • inMemberOfClass: no inheritance
  • respondsToSelector: returns whether an object responds to a given method

Selector

SEL is the Objective-C “type” for a selector

Foundation

NSObject,

NSArray

  • Immutable
  • All objects in the array are held onto strongly
  • Shortcut to create: *@[]

NSMutableArray

  • Mutable version of NSArray
  • for-in

NSNumber

New syntax for creating an NSNumber in iOS 6: @()

NSValue

  • I don’t know too much about this class.
  • Generic object wrapper for some non-object, non-primitive data types (i.e. C structs, interesting)

NSData

  • Bag of bits.
  • Used to save/restore/transmit raw data throughout the iOS SDK.

NSDate

  • Used to find out the time right now or to store past or future times/dates.

NSSet/NSMutableSet

  • No ordering.
  • Unique object

NSOrderedSet/NSMutableOrderedSet

NSDictionary

  • Immutable collection of objects looked up by a key (simple hash table).
  • All keys and values are held onto strongly by an NSDictionary.

NSMutableDictionary

  • Mutable version of NSDictionary.

Property Lists and NSUserDefault

  • It means any graph of objects containing only: NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData.
  • SDK usually read property lists from somewhere or write them out to somewhere.

  • NSUserDefault: lightweight storage of property lists.

NSRange

  • C struct (not a class)
  • Important location value NSNotFound.

UIFont and UIColor

  • Fonts in iOS 7 are very important to get right.
  • It is best to get a UIFond by asking for the preferred font for a given text style.

  • UIFontDescriptor: attempts to categorize a font anyway.

NSAttributedString

  • A NSString where each character has an NSDictionary of “attributes”.

Attributed String in UITextView and UILabel

[iOS 7 Stanford 2013 Fall] Lecture 2 Xcode

Objective-C

Lazy Initialization

Inheritance

Getter & Setter

If implements both setter and getter, @synthesize is required.

Utility Method

A (usually) static method. Used when you need to do stuff that does not need an instance of a class.

Initialization

  • instancetype basically tells the compiler that this method returns an object which will be the same type as the object that this message was sent to.
  • Almost only for init.

[DATA STRUCTURE & Other Objects Using JAVA] Chapter 7 Queue

stack vs. queue

7.1 Introduction to Queues

  • A queue is a data structure of ordered items such that items can be inserted only at one end (called the rear) and removed at the other end (called the front). The item at the from end of the queue is called the first item.
  • Because items must be removed in exactly the same order as they were added to the queue, a queue is call a first in/first out data structure (FIFO).
  • The only difference between stack and queue is the rule that determines which item is removed first.

7.1.1 The Queue Class

  • The method for adding an item to the queue is often called an insert or enqueue operation.
  • The method for removing an item from the queue is often called a getFront of dequeue operation.
  • If a program attempts to remove an item from an empty queue, that is a kind of error called queue underflow.
  • Java’s queue also provides two alternative methods, offer() and poll(), that a queue can use instead of add and remove.

7.1.2 Uses for Queues

  • Because queues occur in real-life situations, they are frequently used in simulation programs.
  • Queues also appear in computer system software, such as the operating system that runs on Pc.
  • Buffering data in a queue is often used when one computer component is receiving data from another faster computer component.

7.2 Queue Applications

7.2.1 Java Queues

  • Java does not actually have a separate Queue class.
  • Java has a Queu interface.
  • But there are several other Java classes that implement the Queue interface.
  • If you want a simple Java queue, consider using the Queue interface and the LinkedList class.

7.2.2 Palindromes

  • A palindrome is a string that reads the same forward and backward.
  • We treat both the uppercase and lowercase versions of a letter as being the same character.
  • The program only accepts alphabetic letters.

7.2.3 Car Wash Simulation (Skipped)

7.3 Implementations of the Queue Class

7.3.1 Array Implementation of a Queue

  • When a class requires some small computation that is likely to be used several times, consider implementing the computation with a helper method (that is, a private method).

Invariant of the ArrayQueue Class

  1. The number of items in the queue is stored in the instance variable manyItems.
  2. For a non-empty queue, the items are stored in a circular array beginning at data[front] and continuing through data[rear].
  3. For an empty queue, manyItems is zero and data is a reference to an array, but we are not using any of the array, and we don’t care about front and rear.

Specification

Implementation

7.3.2 Linked List Implementation of a Queue

Invariant of the LinkedQueue Class

  1. The number of items in the queue is stored in the instance variable manyNodes.
  2. The items in the queue are stored in a linked list, with the front of the queue stored at the head node and the rear of the queue stored at the final node.
  3. For a non-empty queue, the instance variable front is the head reference of the linked list of items, and the instance variable rear is the tail reference of the linked list.

Implementation

7.4 Deques and Priority Queues

7.4.1 Double-Ended Queues

  • Entries can be quickly inserted and removed at both ends.
  • The ends of the deque are called front and back, but these designations are arbitrary since the same operations can occur at both ends.
  • In Java, deques are an interface, java.util.Deque, that includes all of the queue interface plus additional methods such as addFirst and removeLast.

7.4.2 Priority Queues

  1. A priority queue is a data structure that stores items along with a priority for each item.
  2. Items are removed in order of priority.
  3. The highest-priority item is removed first.
  4. If several items have equally high priorities, then the one that was placed in the priority queue first is the one removed first.

7.4.3 Priority Queue ADT — Specification

7.4.4 Priority Queue Class — An Implementation That Uses an Ordinary Queue

[DATA STRUCTURE & Other Objects Using JAVA] Chapter 6 Stacks

6.1 Introduction to Stacks

  • Stack is sometimes called a push down store.
  • A stack is a data structure of ordered items such that items can be inserted and removed only at one end.
  • Stack items must be removed in the reverse order of that in which they are placed on the stack.
  • LIFO: Last-in/first-out
  • Only the top item is accessible.
  • Adding an item to a stack is called a push operation and removing an item form a stack is called a pop operation.

6.1.1 The Stack Class — Specification

  • Ge the top item of this stack without removing the item.

  • Stack underflow: If a program attempts to pop an item off an empty stack, it is asking for the impossible; this error is called stack underflow, which will throw an EmptyStackException.

6.1.2 Programming Example: Reversing a Word

  • Stack is surprisingly useful:

    • Most compilers use stacks to analyze the syntax of a program.
    • Stacks are used to keep track of local variables when a program is run.
    • Stacks can be used to search a maze or a family tree or other types of breaching structures.

6.2 Stack Application

6.2.1 Programming Example: Balanced Parenthesis

  • Every time a left parenthesis occurs, it is pushed onto the stack.
  • Every time a right parenthesis occurs, a matching left parenthesis is popped off the stack.
  • If all goes smoothly and the stack is empty at the end of the expression, then the parenthesis match.

6.2.2 Evaluation Arithmetic Expression

Evaluation Arithmetic Expression — Design

(((6 + 9) / 3) * (6 – 4))

  • Evaluate the leftmost of the innermost expression repeatedly
  • The end of the expression to be evaluated is always a right parenthesis ‘)’, and moreover it is always the first right parenthesis.
  • The next right parenthesis will indicate the end of the next expression to be evaluated.
  • Whenever we reach a right parenthesis, we combine the top two numbers (on the number stack) using the topmost operation (on the character stack).

Something About Java Scanner

  • The expression input.hasNext(UNSIGNED_DOUBLE) is true if the next part of the input expresso is a double number.
  • The expression next = input.findInLine(UNSIGNED_DOUBLE) sets the stringnext equal to the next part of the input expression, which must a double number.
  • The expression next = input.findInLine(CHARACTER) sets next equal to the next single character (skipping spaces) in the input expression.

Evaluation Arithmetic Expression — Implementation

Evaluation Arithmetic Expression — Testing and Analysis

The algorithm for the program is O(n).

Evaluation Arithmetic Expression — Enhancements

  • Permit expression that are not fully parenthesized
  • Use the Java precedence rules to decide the order of operations when parentheses are missing

6.3 Implementations of the Stack ADT

6.3.1 Array Implementation of a Stack

Invariant of the ArrayStack Class

  1. The number of items in the stack is stored in the instance variable manyItems.
  2. The items in the stack are stored in a partially filled array called data, with the bottom of the stack at data[0], the next item at data[1], and so on, to the top of the stack at data[manyItems-1].

Specification

6.3.2 Linked List Implementation of a Stack

Invariant

  1. The items in the stack are stored in a linked list, who the top of the stack stored at the head node, down to the bottom of the stack at the final node.
  2. The instance variable top is the head reference of the linked list of items.
  3. Because we are using a linked list, there are no capacity worries.

Specification

Since we are using linked list here, we do not need method to maintain correct size.

6.4 More Complex Stack Applications

6.4.1 Evaluating Postfix Expressions

  • Infix Notation: 2 + 3
  • Prefix Notation (Polish Prefix Notation): * + 2 3 7 = (2 + 3) * 7
  • Postfix Notation (Polish Postfix Notation) (Reverse Polish Notation): Write the operations after the two numbers being combines. 2 3 + 7 *
  • In Reverse Polish Notation, an operation is applied to the two numbers that are immediately before it. 7 3 5 * + 4 = 7 + (3 * 5)
  • Do not intermix prefix and postfix notation.
  • Postfix notation is handy because it does not require parentheses and because it is particularly easy to evaluate.
  • Each operation is used as soon as it is read.
  • Each time an operation appears in the input, the operands for the operation are the two most recently seen numbers.
  • Sometimes the “most recently seen number” is not actually an input number; instead, it is a number that we computed and pushed back onto the stack.
  • When the input is exhausted, the number remaining in the stack is the value of the entire expression.

6.4.2 Translating Infix to Postfix Notation

  • One strategy for evaluating an ordinary infix expression is to first convert it to postfix notation and then evaluate the postfix expression.
  • If the infix expression is fully parenthesized, all that’s needed to convert from infix to postfix is to move each operation symbol to the location of the right parenthesis corresponding to that operation and then remove all parenthesis.
  • The operands in the equivalent postfix expression are in the same order as the operands in the corresponding infix expression we start out with.

6.4.3 Using Precedence Rules in the Infix Expression

  • In practice, infix expressions are usually not fully parenthesized, and the computer must rely on precedence rules to determine the order of operations for the missing parentheses.
  • Operations of equal precedence are performed in left-to-right order.

[iOS 7 Stanford 2013 Fall] Lecture 1 Class Logistic, Overview of iOS, MVC, Objective-C

I started to go through the latest iOS 7 Development course by Stanford in 2013 Fall. This is the first lecture.

iOS Overview

Core OS (Miniatured OS X)

  • OSX Kernel
  • Mach 3.0
  • BSD
  • Sockets
  • Security
  • Power Management
  • Keychain Access
  • Certificates
  • File System
  • Bonjour

Core Services

  • Collections: NSArray, NSDictionary, NSSet
  • Address Book
  • Networking
  • File Access
  • SQLite
  • Core Location
  • Net Services
  • Threading
  • Preferences
  • URL Utilities

Media

  • Core Audio
  • OpenAL
  • Audio Mixing
  • Audio Recording
  • Video Playback
  • JPEG, PNG, TIFF
  • PDF
  • Quartz (2D)
  • Core Animation
  • OpenGL ES

Cocoa Touch (Touch Version of Cocoa)

  • Multi-Touch
  • Core Motion
  • View Hierarchy
  • Localization
  • Controls
  • Alerts
  • Web View
  • Map Kit
  • Image Picker
  • Camera

MVC

  • Three camps: Model, View, Controller
  • Model: What your application is
  • Controller: How your Model is presented to the user
  • View: Your Controller’s minions
  • It’s all about managing communication between camps
  • Controllers can always talk directly to their Model.
  • Controllers can always talk directly to their View.
  • The Model and View should never speak to each other.
  • Views talk to Controllers through “target-action”, which is “blind” and structured.
  • The Controller sets itself as the View’s delegate.
  • The delegate is set via a protocol.
  • Views do not own the data they display. So, if needed, they have a protocol to acquire it. The protocol this time is called data source.
  • Controllers are almost always the data source (not Model).
  • Controllers interpret/format Model information for the View.
  • Models uses a “radio station” like broadcast mechanism to inform Controllers update or something about Models.
  • Controllers (or other Model) “tune in” to interesting stuff.

Objective-C

  • A @porperty is just the combination of a getter method and a setter method in a class.
  • The getter (usually) has the same name of the property.
  • The setter’s name is “set” plus capitalized property name.
  • New iOS 7 syntax for importing framework

  • Using dot notation only for @property‘s setters and getters.

  • strong means “keep the object that this property points to in memory until I set this property to nil (zero) (and it will stay in memory until everyone who has a strong pointer to it sets their property to nil)”

  • weak would mean: “if no one else has a strong pointer to this object, then you can throw it out of memory and set this property to nil (this can happen at any time)”

  • nonatomic means: “access to this property is not thread-safe”.

  • Fast enumeration: for-in syntax. It works on arrays, dictionaries, etc.