480 likes | 822 Views
Overview of Objective-C. CSE 391 Fall 2012 Tony Scarlatos. What is Objective-C?. Objective-C is an object-oriented superset of the C programming language C is a procedural programming language, where data and logic are separated in code
E N D
Overview of Objective-C CSE 391 Fall 2012 Tony Scarlatos
What is Objective-C? • Objective-C is an object-oriented superset of the C programming language • C is a procedural programming language, where data and logic are separated in code • In Objective-C data and logic are combined into a class (encapsulation) • Straight C code will compile just fine in the Xcode IDE • OS X is built on Objective-C
History of Objective-C • 1972 - C programming language introduced • 1980 – SmallTalk development leads to Objective-C in 1983 • 1988 – NeXTStep acquires license to Objective-C • 1996 – Apple buys NeXT • 2002 – OS X is built on Objective-C • 2007/2008 – iPhone OS introduced with iPhone SDK
Differences with other C descendants • C#, Java, and C++ were developed to be stand-alone languages with their own rules, and so they look different than C, and from each other • Objective-C looks like C with lots of stuff added • So there are 2 ways to do things in Objective-C, the “old C way” and the “new Obj C way” • Square brackets are used in the code to indicate to the compiler that part of the code is Obj C • Often the classes begin with “NS”, as in NSString, which is a holdover from the NeXTStep days
Cocoa Touch • A set of pre-written classes for Obj C such as utility functions that make up the Foundation Framework • User Interface elements are contained in the UIKit Framework, as well as classes to deal with media, and other services such as maps • Hundreds of classes, thousands of methods make up Cocoa Touch
OOP Refresher - Classes • Class is a definition or description – what exists and what it can do • Can be used to describe data tables, UI elements, and services, etc. • Classes are attributes and behaviors, also called properties and methods • Ex. A person class (attributes are name, sex, height, etc.) (behaviors are jump, run, walk, etc.) • An instance of the person class might have properties like Alice, Female, 5’ 4” • 1 class can create multiple objects (also called instances), like many houses can be built from 1 set of blueprints
OOP Refresher - Objects • Objects are created from classes • An object can be a UI element (like a button) or an instance of a data table • An object is an instance of a class (instantiation) • Apple provides many classes for developers (ie. Date, String) that developers make instances of
OOP Process • Write the class (unless Apple has written it for you) • Create the object based on that class • Use the object – use the properties and methods of that object • Dispose of the object (clean up) • Not since late fall 2011, when Automatic Reference Counting (ARC) was introduced!
Language • APIE – Abstraction, Polymorphism, Inheritance, Encapsulation • Abstraction – reducing things to their core essence (ie. Thinking about a group of individuals as defined by their attributes such as weight, age, occupation, etc. and their behaviors, such as sleep, speak, etc.) • Encapsulation puts properties and methods into a class, and hides much of the information from the rest of the system (sometimes called black-boxing)
Language - continued • Inheritance is about specialization. It allows you to, say, inherit the properties of the person class (name, address) into a more specialized class, like a employee class, which adds the properties of title and salary • In this example the person class is the super class (also sometimes called the parent or base class) • In Obj C delegation is much more commonly used than inheritance
Delegation in Obj C • The App Delegate is where you can put code that responds to application-level events. • For example the app delegate creates a UI Window object that defines the boundary of the display. • The app delegate loads in view controllers that populate the display. • The delegate protocol contains the method names • Classes are marked by angle brackets < > • Methods are implemented from the delegate protocol • The delegate is connected to the delegating object
Model View Controller (MVC) • Is the core architecture of Obj C. • Model is the data in the application • View is the UI (the .xib file) • Controller is the entity that provides communication between the Model and the View (the .h and .m files) • Views send actions to the controller, the controller sends outlets to the view • The ViewControlleris the most common structure used in developing apps in Obj C.
Xcode file types • When you create an application in Xcode, it creates a project folder with lots of files in it. • .h files are header files. Header files contain class, type, function, and constant declarations. • .m files are source files. Can contain both C and Obj C code. • .mm files are also source files, but contain C++ code. • .xibfiles are interface builder files • When you want to include header files in your source code, you use a #import directive. It’s like #include (from C) but it makes sure the same file is never included more than once.
Some Obj C conventions • Objective-C is case sensitive, but somewhat not white space sensitive • Pointers are denoted by an * before the name • // some comment… is the format for comments • A multiline comment is /* everything that follows is a comment until you come to */ • Code blocks are indicated with curly braces { } • Lines of code end with a semicolon
More Obj C conventions • C style format strings are denoted by an % sign • Placeholder examples %iis an integer %fis a floating point value %cis a single character %@ is an object • An @ sign followed by quotes denotes a literal string
Variable Examples BOOL isThisCool = YES (or NO) int minutes = 60; int hours = 24; int days = 365; intminutesInYear = minutes * hours * days; NSLog (@”There are %i minutes in a year”, minutesInYear); Pointer example: NSString *message = @”hello”;
Object and Method Declarations • Dot syntax way: myObject.someMethod • Objective C way: [myObjectsomeMethod]; • Receiving variable, dot syntax: result = myObject.someMethod(); • Receiving variable, Obj C: result = [myObjectsomeMethod];
Methods with arguments • Dot syntax way: myObject.someMethod(arg); • Obj C way: [myObjectsomeMethod: arg]; • Multiple arguments, dot syntax: myObject.insert(“Hello”, 11); • Multiple arguments, Obj C: [myObjectinsertString: @”Hello” atIndex: 11]; • Methods with multiple arguments in Obj C can take up multiple lines of code. But the code is very explicit, unlike dot syntax arguments.
Nested Method Calls • Dot syntax way: myObject.someMethod (getValue()); • Obj C way: [myObjectsomeMethod: [anotherObjectanotherMethod]];
Obj C class declaration This example declares only methods – classes can also declare properties.
Obj C method declaration The minus sign indicates that this is an instance method. A plus sign would designate a class method. Class methods do not require you to create an instance first.
Allocation and Initialization NSDate *myDate = [NSDatealloc]; myDate = [myDate init]; OR NSDate *myDate = [[NSDatealloc] init]; Custom Initializers: NSString *message =[[NSStringalloc] initWithContentsOfFile: ]; NSString *message =[[NSStringalloc] initWithContentsOfURL: ];
Automatic Reference Counting • In other words, garbage collection! • In the iOS SDK you used to have to retain and then release the objects you created in memory. Releasing them too soon would result in a dangling pointer. Never releasing them would cause a memory leak. • The iOS SDK did not used to have automatic memory management like Java, but now it does. The compiler actually optimizes the code. • It is still possible, though, to do explicit memory management in code for those who wish to…
Steps to create your 1stiPhone project • Launch Xcode • Choose Single View Application from the iPhone palette • Name the file Hello391 and save it • From the Resources directory, select the file main.storyboard, which will launch Interface Builder • From the Object palette, drag and drop a Label onto the View • Select the label so you can edit the text • Click Run, which will launch the iPhone Simulator