1 Reverse a String iPhone/iPad, iOS Development Tutorial.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Why not just use Arrays? Java ArrayLists.
View-Based Application Development Lecture 1 1. Flows of Lecture 1 Before Lab Introduction to the Game to be developed in this workshop Comparison between.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Create a calculator– 2nd iPhone programming exercise CSE 391 Fall 2012.
Chapter 10 Introduction to Arrays
Introduction to Objective-C and Xcode (Part 1) FA 175 Intro to Mobile App Development.
Instructions 1. Open xCode 2. File -> New Project 3. Start a new View based iPhone Project. The type of project you create really.
Chapter 10.
View Controllers (second part) Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
The Rhapsody in C++ Tool Training "Essential" © I-Logix v2.3 25/9/2000 Adv-1 Essential Rhapsody in C++ Section 4 Advanced Level Modified.
Guide To UNIX Using Linux Third Edition
C++ fundamentals.
Storyboards Managing multiple views. Overview Create a single view application Give the project a name and click “Use Storyboards” and “Use Automatic.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Xcode testing Using XCTest.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
PHP meets MySQL.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
Martin Dodge Practical 2, 24th March 2004, pm Social Science Research Methodologies.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Objective C Basics. It’s C with Some Extras!  Cross Platform language  Mac  Linux/UNIX  Windows  Layer above C (Superset)  Adds Object-Oriented.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
1 Designing with a UIToolBar iPhone/iPad, iOS Development Tutorial.
Tabbed Views UITabBarController. Controller Architecture UITabBarController Controls the first view that the user sees The view controller class (and.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
IOS with Swift Hello world app.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
View Controllers Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 CSC241: Object Oriented Programming Lecture No 25.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
OOP with Objective-C Categories, Protocols and Declared Properties.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
1 UI Alert View iPhone/iPad, iOS Development Tutorial.
Chapter 15 Strings as Character Arrays
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Computer Programming for Engineers
Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.
Introduction to Objective-C and Xcode (Part 4) FA 175 Intro to Mobile App Development.
VB.NET and Databases. ADO.NET VB.Net allows you many ways to connect to a database. The technology used to interact with a database or data source is.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Designing with Introspection
Engineering Innovation Center
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Making Procedural Methods
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
EEC-492/693/793 iPhone Application Development
Presentation transcript:

1 Reverse a String iPhone/iPad, iOS Development Tutorial

2 Why Convert? Sometimes we want to perform some task on an object that is not included in the methods of that class. An example of this might be reversing the order of characters in an NSString object. While we could certainly do this in code, it would be better if we could add a class method to NSString itself that would reverse any string. We will use a category to add the string reversal method to NSString.

3 What’s a Category? A category is an Objective – C construct that allows us to add methods to any class, regardless of whether we have the source code for that class. Once we add a category to a class, all the methods in the category are added to all instances of the class in our code. Since categories are maintained in separate files, it is a simple matter to include them in any project where the extended features of the class are desired.

4 Open a new “Single View” app Start Xcode, choose “Create a new Xcode project,” select the Single View Application template, and click Next. Select “Use Auto Reference Counting” Don’t select the storyboard. Don’t really need it for this application. Add a UITextField and a UILabel.

5 Create a UI that looks like this

6 In the Attribute Inspector Set the UITextField’s return button type to “Done.” Attribute Inspector is 4 th button to the right. “Return Key” is towards the bottom of settings selections.

7 Select New File Objective-C Category

8 Name the new category “AddedMethods”. Make sure that the category is on NSString

9 New Category Created Click Next, then Create. Two new files will be generated with the names NSString+AddedMethods.h NSString+AddedMethods.m This file naming convention makes it easy to see categories that we have added.

10 NSString+AddedMethods.h Open the NSString+AddedMethods.h file and add the declaration for our new class method as shown below: + (NSString *)stringByReversingString:(NSString *)str;NSString Save the file.

11 Add to: NSString+AddedMethods.m + (NSString *)stringByReversingString:(NSString *)str { char cString[50]; char revString[50]; NSString *retval; [str getCString:cString maxLength:[str lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1 encoding:NSUTF8StringEncoding]; printf("%s", cString); for (int i = 0; i < strlen(cString); i++) { revString[i] = cString[strlen(cString) - (i + 1)]; } revString[strlen(cString)] = ‘\0‘; retval = [NSString stringWithCString:revString encoding:NSUTF8StringEncoding]; return retval; }NSString printfstrlen NSString

12 NSString+AddedMethods.m It is much easier to index into C strings and build them up character by character than it would be for NSString objects. In this method, we set up two buffers (char arrays). Then we get a C string representation of the str argument by using the getCString:maxLength:encoding method. In this method, the maxLength parameter must be one more than the number of bytes in the NSString to account for the NULL terminator on C strings. We then copy each character from the cString to the revString in reverse order (in the for loop). After doing this, we put a NULL terminator ( ‘\0’) at the end of the reversed string, create a new NSString (retval) from the contents of the reversed string, and return it.

13 Add to: ViewController.h Import the category in order to use it. #import "NSString+AddedMethods.h" Wire the (nonatomic, strong)IBOutlet UILabel *output; -(IBAction)inputDone:(UITextField *)sender; USE: UITextField’s Did End On Exit event.

14 Add to: ViewController.m - (IBAction)inputDone:(UITextField *)sender { self.output.text = [NSString stringByReversingString:sender.text]; self.output.text); [sender resignFirstResponder]; }NSString

15 What Happens? The action method sets the output label’s text property to the reversed contents of the input text field’s text property using the stringByReversingString method we just added to the NSString class. After doing this, the method calls resignFirstResponder on the sender, which dismisses the keyboard.

16 Program Output

17 Summary Categories are a very powerful tool for adding functionality to existing classes. When we add a category to a class, all child classes of that class will inherit the added methods as well. As a further exercise, try to add another method to NSString category we defined that replaces all spaces in a string with underscores or something else!