collections.key

34
Cocoa and Objective-C: Data Structures, Collections, and Control Structures TN Valley Apple Developers Saturday CodeJam December 4, 2010

Upload: lisa-ridley

Post on 22-Apr-2015

804 views

Category:

Technology


0 download

DESCRIPTION

Cocoa and Objective-C: Data Structures, Collections, and Control Structures

TRANSCRIPT

Page 1: Collections.key

Cocoa and Objective-C:Data Structures, Collections,

and Control StructuresTN Valley Apple Developers

Saturday CodeJam

December 4, 2010

Page 2: Collections.key

Organizing Information

Most software applications work with collections of information that are composed of individual components

A data form is a collection of form fields

Page 3: Collections.key

Data CollectionsCocoa has a variety of data structures that can be used to organize data:

NSString

NSArray

NSDictionary

NSSet

NSNumber

NSData

NSURL

User-defined Objects

Page 4: Collections.key

NSStringNSString is the Cocoa class used to handle strings of data.

NSString has over 100 different methods to handle a variety of activities, such as comparing strings, working with file paths, interpreting URLs, manipulating text, etc.

Page 5: Collections.key

NSArrayNSArray is used to manage an ordered collection of objects as an array

While it is common practice to store objects of the same type in an array, you can mix the type of objects stored in an NSArray object

NSArray has built-in behaviors to facilitate enumerating through the objects stored in them

Objects added to an NSArray object receive a retain message; they also receive a release message when they are removed from an array

Page 6: Collections.key

NSDictionary

NSDictionary objects handle key/value pairs, or associative arrays

Objects added to a dictionary are sent a retain message, and a corresponding release message is sent when it is removed or the dictionary is released

Page 7: Collections.key

NSSetNSSet holds a collection of objects as a mathematical set

The set is not ordered

A given object appears only once

Objects added to the set are sent a retain message, and they receive a corresponding release message when they are removed or the set is released

Page 8: Collections.key

NSDataNSData objects are used to hold a block of bytes and treat it as an Objective-C object

NSData uses any bytes assigned to it as a buffer; if the bytes were originally allocated with malloc (C) call, then NSData takes ownership of those bytes, and frees them when it is deallocated

Page 9: Collections.key

Mutable vs ImmutableAll of these objects (NSString, NSArray, NSDictionary, NSSet, NSData) are immutable objects, which means they cannot be changed once they are created.

Each one of these objects has a corresponding object that can be changed (is mutable): NSMutableString, NSMutableArray, NSMutableDictionary, NSMutableSet, NSMutableData.

Page 10: Collections.key

When to use?Immutable objects are static, and as such take less space in memory and are faster to access, but cannot be changed once they are created

Mutable objects are dynamically allocated and can be changed (arrays, dictionaries and sets can grow and shrink, and strings can be changed), but take more space in memory and are a bit slower to access

Use Immutable objects when you know the contents of that object won’t be changing before it is released

Page 11: Collections.key

NSNumberNSNumber objects are used to hold numbers in Objective-C

Objective-C collection classes (NSArray, NSDictionary, NSSet) cannot hold C numeric types or structures, only Objective-C objects

NSNumber has methods to convert C types to Objective-C objects, and visa versa

Page 12: Collections.key

NSURL

NSURL objects are used to hold both file URLs (paths to file names preceded with “file://”) and network URLs (such as website addresses)

Page 13: Collections.key

Operators and Control Structures

Page 14: Collections.key

Assignment Operator

To assign a value to a variable, or content to an object, we typically use the “=”, which is also called the assignment operator

So, if “=” sets the value of something, how do we test the value of something?

Page 15: Collections.key

Comparison Operators

Testing the value of something is done with a Comparison Operator, specifically in this case, “==”

This is common among a multitude of programming languages, and is not a C or Objective-C specific thing

Page 16: Collections.key

Comparison OperatorsThe common comparison operators are:

== is equal to> is greater than< Is less than

>= Is greater than or equal to<= Is less than or equal to!= Is not equal to (preferred)

<> is not equal to=== Is Identical to

Page 17: Collections.key

Arithmetic Operatorsa = b Assignmenta + b Additiona - b Subtractiona++ Increments by 1++a Increments by 1 (prefix)a-- Decrements by 1--a Decrements by 1 (prefix)

a * b Multiplicationa / b Divisiona % b Modulus

Page 18: Collections.key

++a vs. a++ / --a vs. a--

Both expressions increment/decrement the value of a by one

++a/--a changes the value of a before any other evaluation takes place

a++/a-- changes the value of a after any other evaluation takes place

Page 19: Collections.key

Result of prefix vs. postfix

int a = 5;int b;b = a++;

a = 6, b = 5

int c = 9;int d;d = ++c;

c = 10, d = 10

Page 20: Collections.key

For More Information

More information on operators in C, Objective-C and C++ can be found at:

http://en.wikipedia.org/wiki/Operators_in_C_and_C++

Page 21: Collections.key

Control Structures

Control structures are used to add logic (decision making processes) to your code

The code that gets executed depends on the outcome of the evaluation of certain expressions

Page 22: Collections.key

“if” and “else”An “if” statement is used to evaluate an expression which, if true, results in the execution of a block of code

The optional “else” portion is executed if the expression evaluated in the “if” branch is false

Structure:

if (expression is true) {

execute this code

} else {

execute this code

}

Page 23: Collections.key

Conditional Expression

A “conditional expression” is shortcut code for the “if-then-else” statement

Structure:

a = expression-1 ? expression-2 : expression-3

This evaluates to “if expression-1 is true, a=expression-2, else a=expression-3”.

Page 24: Collections.key

“else if”Introduces additional statements for evaluation

Structure:

If (statement evaluates true) {

execute this block

} else if (statement evaluates true) {

execute this block if the first statement is false

} else {

execute this block if both are false

}

Page 25: Collections.key

“switch”

Used to branch to different code blocks depending on the value of an integer expression.

Page 26: Collections.key

“switch” Structureswitch (expression) {

case value1:

execute this code;

break;

case value2:

execute this code;

break;

default:

statement;

}

Without the “break” statement, code execution will fall through each branch until the code block ends, or a break statement is encountered.

Page 27: Collections.key

“while”Evaluates an expression and loops through a code block as long as the evaluated expression is true

Structure:

while (expression) {

execute this code

}

Page 28: Collections.key

“do-while”Similar to a “while” structure; executes a code block, then evaluates an expression and executes the code block again as long as the expression is true

Structure:

do {

execute code block;

} while (expression is true);

Unlike the “while” structure, in a “do-while” statement the code block is always executed once

Page 29: Collections.key

“for”This is the most common looping structure

Commonly called a “for loop”

Structure:

For (expression1; expression3; expression3) {

execute code block;

}

Page 30: Collections.key

What happens?expression1 is evaluated once before the loop begins

expression2 is evaluated for truth

If expression2 is true, the code block is executed

expression3 is evaluated

These steps are repeated until expression2 becomes false

Page 31: Collections.key

A more concrete example

for (int x = 0; x < 10; x++) {

execute this code block 10 times

}

Page 32: Collections.key

“break”

The “break” statement is used to break out of a loop of switch statement

Execution of the code block inside the looping structure is terminated and code execution resumes after the code block

Page 33: Collections.key

“continue”

Used inside a looping structure to abandon execution of the code block for the current iteration

When the “continue” statement is encountered, control passes to the next iteration of the loop

Page 34: Collections.key

Next CodeJamNSEnumeration

Protocols

Delegates

Categories

Model-View-Controller

Encapsulation