collections.key
DESCRIPTION
Cocoa and Objective-C: Data Structures, Collections, and Control StructuresTRANSCRIPT
Cocoa and Objective-C:Data Structures, Collections,
and Control StructuresTN Valley Apple Developers
Saturday CodeJam
December 4, 2010
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
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
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.
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
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
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
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
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.
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
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
NSURL
NSURL objects are used to hold both file URLs (paths to file names preceded with “file://”) and network URLs (such as website addresses)
Operators and Control Structures
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?
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
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
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
++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
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
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++
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
“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
}
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”.
“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
}
“switch”
Used to branch to different code blocks depending on the value of an integer expression.
“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.
“while”Evaluates an expression and loops through a code block as long as the evaluated expression is true
Structure:
while (expression) {
execute this code
}
“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
“for”This is the most common looping structure
Commonly called a “for loop”
Structure:
For (expression1; expression3; expression3) {
execute code block;
}
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
A more concrete example
for (int x = 0; x < 10; x++) {
execute this code block 10 times
}
“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
“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
Next CodeJamNSEnumeration
Protocols
Delegates
Categories
Model-View-Controller
Encapsulation