1 / 17

Introduction to Objective-C

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

kaden
Download Presentation

Introduction to Objective-C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Objective-C Spring 2013

  2. 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!

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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.

  8. 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.

  9. 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

  10. 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!

  11. 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.

  12. 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; }

  13. 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; }

  14. 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

  15. 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; }

  16. 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

  17. 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

More Related