Collections.key
Upcoming SlideShare
Loading in...5
×
 

Collections.key

on

  • 762 views

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

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

Statistics

Views

Total Views
762
Slideshare-icon Views on SlideShare
762
Embed Views
0

Actions

Likes
0
Downloads
9
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Collections.key Collections.key Presentation Transcript

    • Cocoa and Objective-C:Data Structures, Collections, and Control Structures TN Valley Apple Developers Saturday CodeJam December 4, 2010
    • Organizing InformationMost software applications work withcollections of information that arecomposed of individual components A data form is a collection of form fields
    • Data CollectionsCocoa has a variety of data structures thatcan be used to organize data: NSString NSNumber NSArray NSData NSDictionary NSURL NSSet User-defined Objects
    • NSStringNSString is the Cocoa class used tohandle strings of data.NSString has over 100 differentmethods to handle a variety of activities,such as comparing strings, workingwith file paths, interpreting URLs,manipulating text, etc.
    • NSArrayNSArray is used to manage an ordered collection ofobjects as an arrayWhile it is common practice to store objects of the sametype in an array, you can mix the type of objects storedin an NSArray objectNSArray has built-in behaviors to facilitateenumerating through the objects stored in themObjects added to an NSArray object receive a retainmessage; they also receive a release message when theyare removed from an array
    • NSDictionaryNSDictionary objects handle key/value pairs, or associative arraysObjects added to a dictionary are sent aretain message, and a correspondingrelease message is sent when it isremoved or the dictionary is released
    • NSSetNSSet holds a collection of objects as amathematical setThe set is not orderedA given object appears only onceObjects added to the set are sent a retainmessage, and they receive a correspondingrelease message when they are removed or theset is released
    • NSDataNSData objects are used to hold a blockof bytes and treat it as an Objective-CobjectNSData uses any bytes assigned to it asa buffer; if the bytes were originallyallocated with malloc (C) call, thenNSData takes ownership of those bytes,and frees them when it is deallocated
    • Mutable vs ImmutableAll of these objects (NSString, NSArray,NSDictionary, NSSet, NSData) areimmutable objects, which means they cannotbe changed once they are created.Each one of these objects has a correspondingobject that can be changed (is mutable):NSMutableString, NSMutableArray,NSMutableDictionary, NSMutableSet,NSMutableData.
    • When to use?Immutable objects are static, and as such take lessspace in memory and are faster to access, butcannot be changed once they are createdMutable objects are dynamically allocated and canbe changed (arrays, dictionaries and sets can growand shrink, and strings can be changed), but takemore space in memory and are a bit slower to accessUse Immutable objects when you know the contentsof that object won’t be changing before it is released
    • NSNumberNSNumber objects are used to hold numbersin 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
    • NSURLNSURL objects are used to hold both fileURLs (paths to file names preceded with“file://”) and network URLs (such aswebsite addresses)
    • Operators and Control Structures
    • Assignment OperatorTo assign a value to a variable, orcontent to an object, we typically use the“=”, which is also called the assignmentoperatorSo, if “=” sets the value of something,how do we test the value of something?
    • Comparison OperatorsTesting the value of something is donewith a Comparison Operator,specifically in this case, “==”This is common among a multitude ofprogramming languages, and is not a Cor 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/decrementthe value of a by one++a/--a changes the value of a beforeany other evaluation takes placea++/a-- changes the value of a afterany other evaluation takes place
    • Result of prefix vs. postfixint a = 5; int c = 9;int b; int d;b = a++; d = ++c;a = 6, b = 5 c = 10, d = 10
    • For More InformationMore information on operators in C,Objective-C and C++ can be found at:http://en.wikipedia.org/wiki/Operators_in_C_and_C++
    • Control StructuresControl structures are used to add logic(decision making processes) to yourcodeThe code that gets executed depends onthe outcome of the evaluation of certainexpressions
    • “if” and “else”An “if” statement is used to evaluate an expression which, iftrue, results in the execution of a block of codeThe optional “else” portion is executed if the expression evaluatedin the “if” branch is falseStructure:if (expression is true) { execute this code } else { execute this code }
    • Conditional ExpressionA “conditional expression” is shortcut codefor the “if-then-else” statementStructure:a = expression-1 ? expression-2 : expression-3This evaluates to “if expression-1 is true,a=expression-2, else a=expression-3”.
    • “else if”Introduces additional statements for evaluationStructure: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 blocksdepending on the value of an integerexpression.
    • “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 loopsthrough a code block as long as theevaluated expression is trueStructure:while (expression) { execute this code}
    • “do-while”Similar to a “while” structure; executes a code block,then evaluates an expression and executes the codeblock again as long as the expression is trueStructure: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 structureCommonly called a “for loop”Structure:For (expression1; expression3; expression3) { execute code block;}
    • What happens?expression1 is evaluated once before the loopbeginsexpression2 is evaluated for truthIf expression2 is true, the code block is executedexpression3 is evaluatedThese steps are repeated until expression2becomes false
    • A more concrete examplefor (int x = 0; x < 10; x++) { execute this code block 10 times}
    • “break”The “break” statement is used to breakout of a loop of switch statementExecution of the code block inside thelooping structure is terminated and codeexecution resumes after the code block
    • “continue”Used inside a looping structure toabandon execution of the code block forthe current iterationWhen the “continue” statement isencountered, control passes to the nextiteration of the loop
    • Next CodeJamNSEnumerationProtocolsDelegatesCategoriesModel-View-ControllerEncapsulation