DOC PREVIEW
SDSU CS 696 - Objective C Basics

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Mobile Application DevelopmentFall Semester, 2010Doc 2 Objective C - BasicsAug 31, 2010Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.References2The Objective-C 2.0 Programming Language, http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163Objective-C Hello World3#import <Cocoa/Cocoa.h>int main(int argc, char *argv[]){ NSLog(@"Hello World!"); return 0; }History4Early 1980'sBrad Cox & Tom Love combine C and Smalltalk messagingGoal - software components1986 - Objective-C book published1988 - NeXT uses Objective-C to implement NeXTstep user interface1996 - Apple purchase NeXT, Objective-C becomes bases for Mac OS X 2007 - iPhone OS written in Objective-C2010 Aug - TIOBE index ranks Objective-C 9'th in popularityTIOBE programming popularity index: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.htmlObjective-C Overview5Strict superset of CC programs are legal Objective-C programsApple's Objective-C support C++Single InheritanceProtocols (java interfaces)Categories (Extending classes)PropertiesSmalltalk messaging syntaxException HandingDynamic runtimeObjects created on heapReflectionBlocksSyntax Additions to C6Anonymous objectClassesSelectorsMessage expressionsProtocol, Category syntaxObjective-C Message Syntax7 Rectangle * sample = [[Rectangle alloc] init]; [sample setWidth:4]; [sample setHeight:5]; [sample setHeight:5 width: 4]; int area = [sample area]; [sample release];Rectangle sample = new Rectangle();sample.setWidth(4);sample.setHeight(5);sample.setHeightWidth(4,5);int area = sample.area();Java Objective-CMessage Syntax8[receiver message][receiver message: argument][receiver message: arg1 and: arg2][receiver message: arg1 and: arg2 with: arg3]Base Date Types9intat least 16 bitsshort intsmaller than int; at least 16 bitslong intat least 32 bitslong long intat least 64 bitsunsigned intat least 16 bitsfloatat least 6 digits of precisiondoubleat least 10 digits of precisionlong doubleat least 10 digits of precisioncharSingle characterunsigned charsigned charBOOL0, 1, TRUE, FALSE, YES, NOfloat _ComplexComplex numberdouble _ComplexExtended accuracy complex numberlong double _ComplexExtra-extended accuracy complex numbervoidBOOL10 BOOL isHome = YES; isHome = NO; isHome = FALSE; isHome = 0; isHome = TRUE; isHome = 1; if (isHome) NSLog(@"home"); else { NSLog(@"away"); }How to Run Programs11Choose Command Line tool to write straight Objective-C code. When we start iPhone applications we will use a different template for projects.Xcode Editor12Strings13NSString C StringNSString* greeting = @"Hi mom"; char* greeting = "Hi Dad";Objective-C Classin Foundation frameworkUsed in iPhone developmentObjective-C array of charWhere are the docs for NSString?14Search for NSString in XcodeGoogle for NSStringor go to Apple Dev center and find NSString classRead String Programming GuideCreating Strings15 NSString * start = @"Start"; NSString * all = [start stringByAppendingString:@" and the rest"]; NSLog(all);Formatting Strings16NSString * formatted = [NSString stringWithFormat:@"Name: %@, Age: %f", @"Sam", 12.3];//Name: Sam, Age: 12.300000Some formats17%@Object%d, %isigned int%uunsigned int%ffloat/double%x, %Xhexadecimal int%ooctal int%zusize_t%ppointer%efloat/double (in scientific notation)%gfloat/double (as %f or %e, depending on value)%sC string (bytes)%SC string (unichar)%ccharacter%Cunichar%lldlong longSee "String Format Specifiers" in Apples "String Programming Guide"NSLog uses formatting18float fromString = [@" 123.45 " floatValue];NSLog(@"Result: %f", fromString);Output2010-08-21 16:42:38.129 examples[29557:a0b] Result: 123.449997Reading From File19 NSString *path = @"/Users/whitney/Desktop/names.txt"; NSError *error; NSString *stringFromFileAtPath = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error:&error]; if (stringFromFileAtPath == nil) { NSLog(@"Error reading file at %@\n%@", path, [error localizedFailureReason]); } NSLog(@"Contents:%@", stringFromFileAtPath);First: Roger,Last: WhitneyFirst: Sam,Last: Spadenames.txtScanning a String20 NSScanner *theScanner; NSString *firstName; NSString *lastName; theScanner = [NSScanner scannerWithString: stringFromFileAtPath]; while ([theScanner isAtEnd] == NO) { if ([theScanner scanString: @"First:" intoString: NULL] && [theScanner scanUpToString: @"," intoString: &firstName] && [theScanner scanString: @"," intoString: NULL] && [theScanner scanString: @"Last:" intoString: NULL] && [theScanner scanUpToString: @"\n" intoString: &lastName] ) { NSLog(@"First: %@: Last: %@", firstName, lastName ); } }First: Roger,Last: WhitneyFirst: Sam,Last: Spadenames.txtstringFromFileAtPath is from last slideDerived Data Types21int single[] = { 0, 1, 2 }int byOrder[] = {[2] = 4, [5] = 1, [0] = 8 }int matrix[3][2] = { {1, 2} , {4, 5}, {6, 7 }}struct point { x; y } corner = { 10, 20 };union overlap { int integer; float floater } example = { 10 }enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday} exam;exam = Monday;Standard C Control Structures22fordowhileifswitchnil23Java's null that responds to messagesRectangle* test = nil;int area = [test area];// runs without errorRulesmessage returns[nil message] returnsobjectnilpointer, int, long, double (numberic types)0struct in registerall values


View Full Document

SDSU CS 696 - Objective C Basics

Download Objective C Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objective C Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objective C Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?