260 likes | 585 Vues
Introduction to Objective-C. Spring 2013. Goals. An introduction to Objective-C As implemented by the Apple LLVM Compiler 4.0 (a.k.a. Clang) Only the basics… See the class web page for references: Reference->Objective-C Most of the references are WRONG about memory management
E N D
Introduction to Objective-C Spring 2013
Goals • An introduction to Objective-C • As implemented by the Apple LLVM Compiler 4.0 (a.k.a. Clang) • Only the basics… • See the class web page for references: • Reference->Objective-C • Most of the references are WRONG about memory management • Now ARC takes care of memory management for you!
History • Brad Cox created Objective-C in the early 1980s • Added object-oriented programming concepts to the C programming language • NeXT Computer licensed the language in 1988 • Used it to develop the NeXTSTEP operating system, programming libraries and applications for NeXT • In 1993, NeXT worked with Sun to create OpenStep, an open specification of NeXTSTEP on Sun hardware
History • In 1997, Apple purchased NeXT and transformed NeXTSTEP into MacOS X which was first released in the summer of 2000 • Objective-C has been one of the primary ways to develop applications for OS X ever since • In 2008, it became the primary way to develop applications for iOS
Objective-C • Objective-C adds classes to C through a small set of extensions • It is used with two object-oriented frameworks: • The Foundation framework contains classes for basic concepts such as strings, arrays and other data structures and provides classes to interact with the underlying operating system • The AppKitcontains classes for developing applications and for creating windows, buttons and other widgets • Together the Foundation and AppKit frameworks are called Cocoa
Objective-C • On iOS, AppKit is replaced by UIKit • Foundation and UIKit together are called Cocoa touch • In this lecture, we focus on the Objective-C language, • we’ll see a few examples of the Foundation framework • we’ll see examples of UIKit later
Objective-C • Objective C is C • All of C code will work. • Memory management not necessary with ARC • Classes are based on Smalltalk syntax • It’s a bit, well, weird.
XCode • Apple’s development tool • Only tool you can use to develop native apps for iOS • Makes use of Apple’s Clang compiler • Based on LLVM • Used to use the gnu compilers, but Apple wanted to extend the compiler without open-sourcing the code • LLVM is a highly-optimized compiler • Xcode also contains Interface Builder, a tool for creating iOS GUI interfaces via drag ‘n drop.
Example • Create a new project as you did with the C++ program. • In the first dialog box choose “OS X” and “Application” • Choose “Command Line Tool” • hit “next” • Enter a name. • Make sure the “Type” is Foundation • Make sure to check the checkbox next to “Create local git repository for this project” when you choose where to locate the project. • A project will be created and you’ll get the file “main.m” • The “.m” stands for “messages” • Insert the code on the next slide
Example Instead of include import will ensure that the file is only included once. #import <Foundation/Foundation.h> intmain(intargc, const char *argv[]) { @autoreleasepool { NSLog (@”Hello, Objective-C!”); } return(0); } // main NSLog is the printf of the Foundation frameworks. Also prints date & time and a newline The @ signifies a NSString. NS = NextStep This is a string class like that found in Java Most Foundation framework classes expect a NSStringnot a C style string!
Interlude • notice that an “M” appears to the right of main.m after you edit it. • The M is an aspect of XCode’sgit integration. • It’s telling you that the file was modified and • that (eventually) you can check these changes into the git repository • XCode automatically created the git repository for you.
Example 2 #import <Foundation/Foundation.h> intmain(intargc, const char * argv[]) { @autoreleasepool { char getName[32]; printf("Enter a name: "); scanf("%s", getName); printf(“Hello %s\n”, getName); } return 0; }
Example 2.1 #import <Foundation/Foundation.h> intmain(intargc, const char * argv[]) { @autoreleasepool { char getName[32]; printf("Enter a name: "); scanf("%s", getName); // Note the use of a “C” string NSLog(@"Hello, %s!", theName); } return 0; }
Example 2.2 #import <Foundation/Foundation.h> intmain(intargc, const char * argv[]) { @autoreleasepool { NSString *theName; char getName[32]; printf("Enter a name: "); scanf("%s", getName); theName = [NSStringstringWithCString:getNameencoding:NSASCIIStringEncoding]; NSLog(@”Hello, %@”, theName); } return 0; } NSString is defined in the Foundation framework It’s complicated to convert a C string to a NSString! Note the format symbol: @ for NSString
Example 3 BOOL is a Foundation type Different from a C language “bool” In Foundation library have constants for BOOL type: YES = 1 NO = 0 int main(intargc, const char * argv[]) { @autoreleasepool{ BOOL areTheyDifferent; areTheyDifferent= areIntsDifferent (5, 5); NSLog(@"are %d and %d different? %@”,5, 5, [NSStringstringWithFormat:@”%d”,areIntsDifferent]); areTheyDifferent = areIntsDifferent (23, 42); NSLog (@"are %d and %d different? %@”,23, 42, [NSStringstringWithFormat:@”%d”,areIntsDifferent]); } return 0; }
Example 3 // returns NO if the two integers have the same // value, YES otherwise BOOL areIntsDifferent (int thing1, int thing2) { if (thing1 == thing2) { return (NO); } else { return (YES); } } // areIntsDifferent
Example 3 // given a YES value, return the human-readable // string "YES". Otherwise return "NO" NSString *boolString (BOOL yesNo) { if (yesNo == NO) { return (@"NO"); } else { return (@"YES"); } } // boolString