Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Cocoa and Objective-C:Data Structures, Collections, and Control Structures TN Valley Apple Developers Saturday CodeJam December 4, 2010
  2. 2. Organizing InformationMost software applications work withcollections of information that arecomposed of individual components A data form is a collection of form fields
  3. 3. Data CollectionsCocoa has a variety of data structures thatcan be used to organize data: NSString NSNumber NSArray NSData NSDictionary NSURL NSSet User-defined Objects
  4. 4. 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.
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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.
  10. 10. 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
  11. 11. 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
  12. 12. NSURLNSURL objects are used to hold both fileURLs (paths to file names preceded with“file://”) and network URLs (such aswebsite addresses)
  13. 13. Operators and Control Structures
  14. 14. 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?
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. ++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
  19. 19. 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
  20. 20. For More InformationMore information on operators in C,Objective-C and C++ can be found at:
  21. 21. 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
  22. 22. “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 }
  23. 23. 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”.
  24. 24. “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 }
  25. 25. “switch”Used to branch to different code blocksdepending on the value of an integerexpression.
  26. 26. “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.
  27. 27. “while”Evaluates an expression and loopsthrough a code block as long as theevaluated expression is trueStructure:while (expression) { execute this code}
  28. 28. “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
  29. 29. “for”This is the most common looping structureCommonly called a “for loop”Structure:For (expression1; expression3; expression3) { execute code block;}
  30. 30. 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
  31. 31. A more concrete examplefor (int x = 0; x < 10; x++) { execute this code block 10 times}
  32. 32. “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
  33. 33. “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
  34. 34. Next CodeJamNSEnumerationProtocolsDelegatesCategoriesModel-View-ControllerEncapsulation