SlideShare a Scribd company logo
1 of 16
Download to read offline
Computer Science Large Practical:

                                     Arrays in Objective-C

                                               Stephen Gilmore

                                               School of Informatics


                                          Friday 2nd November, 2012




Stephen Gilmore (School of Informatics)      Computer Science Large Practical   Friday 2nd November, 2012   1 / 16
Experience report




         The Desktop Team, in the University’s Information Services
         department have asked for feedback from you regarding your
         experiences (good or bad!) with Xcode in the Main Library IS Open
         Access Lab.
         This will help inform their plans for future academic years.
         What has been your experience?




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   2 / 16
Programming with arrays in Objective-C




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   3 / 16
Inspecting an array




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   4 / 16
The XML representation




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   5 / 16
Sorting an array




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   6 / 16
The sorted array




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   7 / 16
The output in the console




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   8 / 16
Everything OK? What about “Build and Analyze”?




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   9 / 16
Handling arrays (1/2)
 #import <Foundation/Foundation.h>
 int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]
        init];

       NSLog(@"Hello, arrays!");

       NSArray * myArray = [[NSArray alloc]
          initWithObjects:@"foo",@"bar",@"baz",nil];

       for(NSString * myStr in myArray) {
          NSLog(@"-- %@", myStr);
       }

       [myArray writeToFile:@"myArray.plist" atomically:YES];

Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   10 / 16
Handling arrays (2/2)

       NSArray *sortedArray = [myArray
          sortedArrayUsingSelector:@selector(
           caseInsensitiveCompare:)];

       [sortedArray writeToFile:@"sortedArray.plist"
           atomically:YES];

       for(NSString * myStr in sortedArray) {
          NSLog(@"---- %@", myStr);
       }

       [pool drain];
       return 0;
 }

Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   11 / 16
Object allocation and arrays


         Arrays in Objective-C are not generic arrays: they can contain
         different kinds of objects.
         The nil value at the end is a terminator, signalling the end of the
         array.
         You can release an object as soon as you add it to an array because
         the array allocates its own memory for the object by calling retain on
         all objects which are added to it.
         arrayWithObjects auto releases from memory so you do not have
         to release the array yourself.
         If we replace [NSArray alloc] initWithObjects with
         arrayWithObjects then this should solve our memory leak.



Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   12 / 16
“Build and Analyze” finds no potential leaks now




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   13 / 16
Mutable arrays




         NSArray objects are not mutable so we cannot update their contents.
         To have an array which we can update we should use
         NSMutableArray.




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   14 / 16
Declaring and using mutable arrays




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   15 / 16
Output from the program




Stephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   16 / 16

More Related Content

What's hot

Domain Specific Language Design
Domain Specific Language DesignDomain Specific Language Design
Domain Specific Language DesignMarkus Voelter
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo designConfiz
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?Colin Riley
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeValerio Maggio
 
Object Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significanceObject Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significanceGajesh Bhat
 
Getting started with Xcode
Getting started with XcodeGetting started with Xcode
Getting started with XcodeStephen Gilmore
 
LectureNotes - RCOS Final Talk
LectureNotes - RCOS Final TalkLectureNotes - RCOS Final Talk
LectureNotes - RCOS Final TalkCalcAndCoffee
 
Design pattern - Software Engineering
Design pattern - Software EngineeringDesign pattern - Software Engineering
Design pattern - Software EngineeringNadimozzaman Pappo
 

What's hot (20)

Code Smell
Code SmellCode Smell
Code Smell
 
Code Smells
Code SmellsCode Smells
Code Smells
 
BDD Primer
BDD PrimerBDD Primer
BDD Primer
 
Domain Specific Language Design
Domain Specific Language DesignDomain Specific Language Design
Domain Specific Language Design
 
Ch06lect1 ud
Ch06lect1 udCh06lect1 ud
Ch06lect1 ud
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
 
Ch10lect1 ud
Ch10lect1 udCh10lect1 ud
Ch10lect1 ud
 
Clean code-v2.2
Clean code-v2.2Clean code-v2.2
Clean code-v2.2
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Ch11lect1 ud
Ch11lect1 udCh11lect1 ud
Ch11lect1 ud
 
Ch07lect1 ud
Ch07lect1 udCh07lect1 ud
Ch07lect1 ud
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Ch03lect1 ud
Ch03lect1 udCh03lect1 ud
Ch03lect1 ud
 
Object Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significanceObject Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significance
 
Cd2Alloy
Cd2AlloyCd2Alloy
Cd2Alloy
 
Getting started with Xcode
Getting started with XcodeGetting started with Xcode
Getting started with Xcode
 
LectureNotes - RCOS Final Talk
LectureNotes - RCOS Final TalkLectureNotes - RCOS Final Talk
LectureNotes - RCOS Final Talk
 
Ch05lect1 ud
Ch05lect1 udCh05lect1 ud
Ch05lect1 ud
 
Design pattern - Software Engineering
Design pattern - Software EngineeringDesign pattern - Software Engineering
Design pattern - Software Engineering
 

Viewers also liked

SELP: Debugging, AVDs and Manifests
SELP: Debugging, AVDs and ManifestsSELP: Debugging, AVDs and Manifests
SELP: Debugging, AVDs and ManifestsStephen Gilmore
 
Working with databases in Android
Working with databases in AndroidWorking with databases in Android
Working with databases in AndroidStephen Gilmore
 
Hacker Halted 2014 - Reverse Engineering the Android OS
Hacker Halted 2014 - Reverse Engineering the Android OSHacker Halted 2014 - Reverse Engineering the Android OS
Hacker Halted 2014 - Reverse Engineering the Android OSEC-Council
 
My Final year project on Android app development
My Final year project on Android app developmentMy Final year project on Android app development
My Final year project on Android app developmentrahulkumargiri
 
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsSteelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsTom Keetch
 
Toll app - Android project
Toll app - Android projectToll app - Android project
Toll app - Android projectArun prasath
 
UseCase is a DIALOG---NOT a PROCESS
UseCase is a DIALOG---NOT a PROCESSUseCase is a DIALOG---NOT a PROCESS
UseCase is a DIALOG---NOT a PROCESSPutcha Narasimham
 
Activity diagram railway reservation system
Activity diagram railway reservation systemActivity diagram railway reservation system
Activity diagram railway reservation systemmuthumeenakshim
 
Payroll and attendance system
Payroll and attendance system Payroll and attendance system
Payroll and attendance system Moses Nkrumah
 
Usecase diagram railway reservation system
Usecase diagram railway reservation systemUsecase diagram railway reservation system
Usecase diagram railway reservation systemmuthumeenakshim
 
Android seminar-presentation
Android seminar-presentationAndroid seminar-presentation
Android seminar-presentationconnectshilpa
 
Software Engineering ppt
Software Engineering pptSoftware Engineering ppt
Software Engineering pptshruths2890
 

Viewers also liked (14)

Android overview
Android overviewAndroid overview
Android overview
 
SELP: Debugging, AVDs and Manifests
SELP: Debugging, AVDs and ManifestsSELP: Debugging, AVDs and Manifests
SELP: Debugging, AVDs and Manifests
 
Working with databases in Android
Working with databases in AndroidWorking with databases in Android
Working with databases in Android
 
Hacker Halted 2014 - Reverse Engineering the Android OS
Hacker Halted 2014 - Reverse Engineering the Android OSHacker Halted 2014 - Reverse Engineering the Android OS
Hacker Halted 2014 - Reverse Engineering the Android OS
 
My Final year project on Android app development
My Final year project on Android app developmentMy Final year project on Android app development
My Final year project on Android app development
 
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsSteelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
 
Toll app - Android project
Toll app - Android projectToll app - Android project
Toll app - Android project
 
UseCase is a DIALOG---NOT a PROCESS
UseCase is a DIALOG---NOT a PROCESSUseCase is a DIALOG---NOT a PROCESS
UseCase is a DIALOG---NOT a PROCESS
 
Activity diagram railway reservation system
Activity diagram railway reservation systemActivity diagram railway reservation system
Activity diagram railway reservation system
 
Payroll and attendance system
Payroll and attendance system Payroll and attendance system
Payroll and attendance system
 
Usecase diagram railway reservation system
Usecase diagram railway reservation systemUsecase diagram railway reservation system
Usecase diagram railway reservation system
 
Ziilion E245 final presentation
Ziilion E245 final presentationZiilion E245 final presentation
Ziilion E245 final presentation
 
Android seminar-presentation
Android seminar-presentationAndroid seminar-presentation
Android seminar-presentation
 
Software Engineering ppt
Software Engineering pptSoftware Engineering ppt
Software Engineering ppt
 

More from Stephen Gilmore

More Stochastic Simulation Examples
More Stochastic Simulation ExamplesMore Stochastic Simulation Examples
More Stochastic Simulation ExamplesStephen Gilmore
 
Testing Android apps with Robotium
Testing Android apps with RobotiumTesting Android apps with Robotium
Testing Android apps with RobotiumStephen Gilmore
 
Common Java problems when developing with Android
Common Java problems when developing with AndroidCommon Java problems when developing with Android
Common Java problems when developing with AndroidStephen Gilmore
 
Quick quiz on Objective-C
Quick quiz on Objective-CQuick quiz on Objective-C
Quick quiz on Objective-CStephen Gilmore
 
Crash Course in Objective-C
Crash Course in Objective-CCrash Course in Objective-C
Crash Course in Objective-CStephen Gilmore
 
The Stochastic Simulation Algorithm
The Stochastic Simulation AlgorithmThe Stochastic Simulation Algorithm
The Stochastic Simulation AlgorithmStephen Gilmore
 
Beginning Android Development
Beginning Android DevelopmentBeginning Android Development
Beginning Android DevelopmentStephen Gilmore
 
Computer Science Large Practical coursework
Computer Science Large Practical courseworkComputer Science Large Practical coursework
Computer Science Large Practical courseworkStephen Gilmore
 
Software Engineering Large Practical coursework
Software Engineering Large Practical courseworkSoftware Engineering Large Practical coursework
Software Engineering Large Practical courseworkStephen Gilmore
 
Introduction to the CSLP and the SELP
Introduction to the CSLP and the SELPIntroduction to the CSLP and the SELP
Introduction to the CSLP and the SELPStephen Gilmore
 
Fixing errors in Android Java applications
Fixing errors in Android Java applicationsFixing errors in Android Java applications
Fixing errors in Android Java applicationsStephen Gilmore
 
Feedback on Part 1 of the Individual Practical
Feedback on Part 1 of the Individual PracticalFeedback on Part 1 of the Individual Practical
Feedback on Part 1 of the Individual PracticalStephen Gilmore
 
Creating and working with databases in Android
Creating and working with databases in AndroidCreating and working with databases in Android
Creating and working with databases in AndroidStephen Gilmore
 
Continuing Android development
Continuing Android developmentContinuing Android development
Continuing Android developmentStephen Gilmore
 
Project management for the individual practical
Project management for the individual practicalProject management for the individual practical
Project management for the individual practicalStephen Gilmore
 
Beginning Android development
Beginning Android developmentBeginning Android development
Beginning Android developmentStephen Gilmore
 
CS/SE Individual practical - DDMS and AVD
CS/SE Individual practical - DDMS and AVDCS/SE Individual practical - DDMS and AVD
CS/SE Individual practical - DDMS and AVDStephen Gilmore
 

More from Stephen Gilmore (17)

More Stochastic Simulation Examples
More Stochastic Simulation ExamplesMore Stochastic Simulation Examples
More Stochastic Simulation Examples
 
Testing Android apps with Robotium
Testing Android apps with RobotiumTesting Android apps with Robotium
Testing Android apps with Robotium
 
Common Java problems when developing with Android
Common Java problems when developing with AndroidCommon Java problems when developing with Android
Common Java problems when developing with Android
 
Quick quiz on Objective-C
Quick quiz on Objective-CQuick quiz on Objective-C
Quick quiz on Objective-C
 
Crash Course in Objective-C
Crash Course in Objective-CCrash Course in Objective-C
Crash Course in Objective-C
 
The Stochastic Simulation Algorithm
The Stochastic Simulation AlgorithmThe Stochastic Simulation Algorithm
The Stochastic Simulation Algorithm
 
Beginning Android Development
Beginning Android DevelopmentBeginning Android Development
Beginning Android Development
 
Computer Science Large Practical coursework
Computer Science Large Practical courseworkComputer Science Large Practical coursework
Computer Science Large Practical coursework
 
Software Engineering Large Practical coursework
Software Engineering Large Practical courseworkSoftware Engineering Large Practical coursework
Software Engineering Large Practical coursework
 
Introduction to the CSLP and the SELP
Introduction to the CSLP and the SELPIntroduction to the CSLP and the SELP
Introduction to the CSLP and the SELP
 
Fixing errors in Android Java applications
Fixing errors in Android Java applicationsFixing errors in Android Java applications
Fixing errors in Android Java applications
 
Feedback on Part 1 of the Individual Practical
Feedback on Part 1 of the Individual PracticalFeedback on Part 1 of the Individual Practical
Feedback on Part 1 of the Individual Practical
 
Creating and working with databases in Android
Creating and working with databases in AndroidCreating and working with databases in Android
Creating and working with databases in Android
 
Continuing Android development
Continuing Android developmentContinuing Android development
Continuing Android development
 
Project management for the individual practical
Project management for the individual practicalProject management for the individual practical
Project management for the individual practical
 
Beginning Android development
Beginning Android developmentBeginning Android development
Beginning Android development
 
CS/SE Individual practical - DDMS and AVD
CS/SE Individual practical - DDMS and AVDCS/SE Individual practical - DDMS and AVD
CS/SE Individual practical - DDMS and AVD
 

Recently uploaded

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Arrays in Objective-C

  • 1. Computer Science Large Practical: Arrays in Objective-C Stephen Gilmore School of Informatics Friday 2nd November, 2012 Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 1 / 16
  • 2. Experience report The Desktop Team, in the University’s Information Services department have asked for feedback from you regarding your experiences (good or bad!) with Xcode in the Main Library IS Open Access Lab. This will help inform their plans for future academic years. What has been your experience? Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 2 / 16
  • 3. Programming with arrays in Objective-C Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 3 / 16
  • 4. Inspecting an array Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 4 / 16
  • 5. The XML representation Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 5 / 16
  • 6. Sorting an array Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 6 / 16
  • 7. The sorted array Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 7 / 16
  • 8. The output in the console Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 8 / 16
  • 9. Everything OK? What about “Build and Analyze”? Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 9 / 16
  • 10. Handling arrays (1/2) #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello, arrays!"); NSArray * myArray = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil]; for(NSString * myStr in myArray) { NSLog(@"-- %@", myStr); } [myArray writeToFile:@"myArray.plist" atomically:YES]; Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 10 / 16
  • 11. Handling arrays (2/2) NSArray *sortedArray = [myArray sortedArrayUsingSelector:@selector( caseInsensitiveCompare:)]; [sortedArray writeToFile:@"sortedArray.plist" atomically:YES]; for(NSString * myStr in sortedArray) { NSLog(@"---- %@", myStr); } [pool drain]; return 0; } Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 11 / 16
  • 12. Object allocation and arrays Arrays in Objective-C are not generic arrays: they can contain different kinds of objects. The nil value at the end is a terminator, signalling the end of the array. You can release an object as soon as you add it to an array because the array allocates its own memory for the object by calling retain on all objects which are added to it. arrayWithObjects auto releases from memory so you do not have to release the array yourself. If we replace [NSArray alloc] initWithObjects with arrayWithObjects then this should solve our memory leak. Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 12 / 16
  • 13. “Build and Analyze” finds no potential leaks now Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 13 / 16
  • 14. Mutable arrays NSArray objects are not mutable so we cannot update their contents. To have an array which we can update we should use NSMutableArray. Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 14 / 16
  • 15. Declaring and using mutable arrays Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 15 / 16
  • 16. Output from the program Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 16 / 16