Principles of Programming Languages. Objective-C. Joris Kluivers

Similar documents
Object-Oriented Programming with Objective-C. Lecture 2

GNUstep: What is it? gnustep.org

Cocoa Programming. David Chisnall. March 18,

Xcode and Swift CS 4720 Mobile Application Development

Mobile Application Development

An Introduction to Smalltalk for Objective-C Programmers

Objective-C ICT/7421ICTNathan. René Hexel. School of Information and Communication Technology Griffith University.

IPHONE. Development Jump Start. phil nash levelofindirection.com

Objective-C and COCOA Applications

Cocoa. Last Week... Music 3SI: Introduction to Audio/Multimedia App. Programming. Today... Why Cocoa? Wikipedia - Cocoa

About MSDOSX. Lecture 0

Objective-C: An Introduction (pt 1) Tennessee Valley Apple Developers Saturday CodeJam July 24, 2010 August 7, 2010

D Programming Language

What are the characteristics of Object Oriented programming language?

Data Abstraction. Hwansoo Han

ITP 342 Advanced Mobile App Dev. Memory

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

Traditional Smalltalk Playing Well With Others Performance Etoile. Pragmatic Smalltalk. David Chisnall. August 25, 2011

CSE 303: Concepts and Tools for Software Development

BASIC CONCEPT OF OOP

Sample Copy. Not for Distribution.

Introduction to Cocoa Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Object-Oriented Programming in Objective-C

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?


2 rd class Department of Programming. OOP with Java Programming

The MVC Design Pattern

Short Notes of CS201

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

Introductory ios Development

CS201 - Introduction to Programming Glossary By

Jython. An introduction by Thinh Le

Outline Smalltalk Overview Pragmatic Smalltalk Closing. Pragmatic Smalltalk. David Chisnall. February 7,

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties


Object-Oriented Programming

Semantic Analysis. Lecture 9. February 7, 2018

These are exciting times for Macintosh users. When Apple unleashed Mac

Extensibility in GNUstep & Étoilé

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

MacOS X Cocoa Programming for Vision Scientist --- #1

Starting to Program in C++ (Basics & I/O)

CPS 506 Comparative Programming Languages. Programming Language

An Object Oriented Programming with C


Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

OBJECT ORİENTATİON ENCAPSULATİON

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS558 Programming Languages Winter 2013 Lecture 8

X Review. Mac OS X Roots: NeXT. BWS Available for virtually every OS

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

Data Structures (list, dictionary, tuples, sets, strings)

Inheritance and Polymorphism

Objective-C. Stanford CS193p Fall 2013

CS 231 Data Structures and Algorithms, Fall 2016

Interview Questions of C++

Inheritance and Polymorphism

STRUCTURING OF PROGRAM

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CS 47. Beginning iphone Application Development

Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

CS193P: HelloPoly Walkthrough

377 Student Guide to C++

Xcode. Chapter 1. Creating a Project

Lecture Topics. Administrivia

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

ios: Objective-C Primer

Announcement. Final Project Proposal Presentations and Updates

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Intro. Scheme Basics. scm> 5 5. scm>

CSE 341, Autumn 2015, Ruby Introduction Summary

Structure of Programming Languages Lecture 10

Programming in Visual Basic with Microsoft Visual Studio 2010

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

VALLIAMMAI ENGINEERING COLLEGE

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Implementing Interfaces. Marwan Burelle. July 20, 2012

EMBEDDED SYSTEMS PROGRAMMING OO Basics

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Computer Science, a Mac, and You. Kevin Cathey Junior, Computer Science

The Dynamic Typing Interlude

CS 251 Intermediate Programming Inheritance

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Certified Core Java Developer VS-1036

Transcription:

Principles of Programming Languages Objective-C Joris Kluivers joris.kluivers@gmail.com

History... 3 NeXT... 3 Language Syntax... 4 Defining a new class... 4 Object identifiers... 5 Sending messages... 5 Strings... 5 Protocols... 6 Memory Management... 6 Reference counting... 6 Dynamic Typing... 7 Error handling... 8 Compiling your program... 8 Like a scripting language... 9 Resources... 10 Objective-C - Page 2/10

History Until the 1980s most software development was based on structured programming, procedural programming using functions. The functions were used to break up larger programs into smaller parts to reuse code and make the program easier to work on. But as computers grew stronger and had more memory so did the software. Software became larger and more complicated, the use of functions just wasn t enough anymore to keep the code clean, organized and easy to maintain. A trend became visible where software engineers thought Object Oriented Programming (OOP) was the solution to keep software maintainable. OOP languages existed already, and new ones were developed. Smalltalk for example was a language that already addressed the software engineering problems using OOP principles. Smalltalk needed a virtual machine to run though. New languages were also developed. C++ added OOP programming features as an extension to C and quickly became the language of choice for native OOP development. C++ started with the addition of classes followed by among others, virtual functions, operator overloading, multiple inheritance and templates. At the same time another extension of C was created called Objective-C. Objective-C was designed by Brad Cox at this company Stepstone. Cox created the language with true reusability in software design and programming in mind. He modeled the language with as many features from Smalltalk as possible. Not only did he create an OOP language, he also wrote a basic set of libraries and allowed the code and resources to be bundled in a single cross-platform format called universal binaries. NeXT Steve Jobs, CEO and founder of Apple left the company in 1985 after an internal struggle. He analyzed the current the computer industry and formed a company called NeXT Inc. His product was what he thought would be the next big thing: an object-oriented toolkit, aimed at the academic market, using PostScript as the display technology. NeXT licensed Objective-C from StepStone. NeXT released its own compiler and libraries on which the NeXTStep OOP toolkit was based. In 1993 Next released NeXTStep for Intel and stopped producing hardware. NeXT changed it s name into NeXT Software Inc. The GNU project released their own version of the ObjectiveC compiler and cloned the NeXTStep framework into GNUStep. The first gnu-objc runtime for GCC was released in 1992. In 1996 Apple acquired NeXT and Steve Jobs once again became the CEO of the company he founded. NeXT executives mostly replaced their Apple counterparts. Apple ported NeXTStep to Apple PowerPC s and based it s new operating system Mac OS X on the NeXTStep framework. The NeXT step Objective-C - Page 3/10

framework was renamed Cocoa and is now the primary development framework for Apple software. GNUStep for linux and windows also still exists but is maintained by a few volunteers and is not widely supported. GNUStep is kept in sync with the Cocoa framework but always runs behind the Apple development and never truly mirrors all Cocoa classes. But at the moment it s the only real solution to develop Objective-C on non Mac OS X systems. Language Syntax Objective-C is a pure extension of C. The means all C code can be compiled using an Objective-C compiler. This can not be said of C++ which has some inconsistencies with C. Objective-C adds one data type, the object and one operation, the message expression to the C language. The message expression syntax is borrowed from the Smalltalk language. Some new keywords are also introduced. Defining a new class The description of an object in Objective-C is given using an interface declaration: @interface MyObject : ParentObject { // object variables + classmethod + classmethod - instancemethod - instancemethod MyObject is the name of the object defined here. Objective-C only allows single inheritance, in the example above, MyObject inherits from ParentObject. After the object variables block a list of methods is defined. In your application a factory object is created for each class you defined. This is done at compile time and they are assembled in memory by the loader. One of the purposes of a factory object is to create initialized instances of their class. Since factory methods are real objects they can do anything the programmer defined. As a convention Factory object names always start with a uppercase letter and represent the class of the object they produce (like MyObject in the above example). Messages send to factory objects are defined using a + sign. Messages send to the actual object instance are defined using a - sign. Objective-C - Page 4/10

The implementation for a defined interface is given using the following syntax: @implementation + classmethod { // implementation - instancemethod { // implementation Object identifiers In Objective-C objects can be identified in two separate ways, using a pointer to the object or using an object identifier. int studentnumber; // declare an integer Student *student; // pointer to an object id student; // declare an object identifier The student object pointer can only point to Student objects or instances of a subclass of Student. The object identifier can point to any object in Objective-C. Sending messages Messages can be seen as requests for operations on objects. As the programmer you send a message to the object, syntactically comparable with executing a method on an object in Java. The name of the message send to the object is called the selector. Say you have an object identifier called student for a Student object, you send messages to the student as follows: [student studentnumber]; // selector: studentnumber [student setname:@ Joris Kluivers ]; // selector: setname: [student setgrade:8 forclass:@ Data Structures ]; // selector: setgrade:forclass: As you can see, Objective-C messages use labeled parameters. Internally this might be translated to the c function objc_setgrade_forclass(int i, NSString *name). The method parameters can t be rearranged like in python. The only function of the labeled parameters is self documentation. Strings Like Java supports a shorthand creation for string objects, so does Objective-C. When using the Objective-C string syntax @ This is a string the compiler will create a NSString object. You can also supply your own class to be used at compile time by passing a special compiler flag. Objective-C - Page 5/10

Protocols Objective-C doesn t support multiple inheritence. At NeXT multiple inheritence by specification, not by implementation was added to Objective-C in the form of protocols. Protocols are much like Java interfaces, they define abstract methods to be implemented by the object that implements the protocol. @protocol Locking - (void) lock; - (void) unlock; @interface MyClass : ParentObject <Locking>... Memory Management To actually get an object identifier you need to instantiate an object. This can be done using one of the factory methods. The factory methods for a Student class could be id student = [Student new]; id student2 = [Student studentwithname:@ Joris Kluivers ]; Factory methods are responsible for allocating the object. In plain Objective-C this should be done using + new { id newobject = (* _alloc)(self, 0); return newobject; statement. This looks quite complicated, fortunately the Cocoa framework defines two factory methods on the root object (called NSObject). The new method allocates a new instance and initializes all instance variables. A second method called alloc allocates the new instance but doesn t initialize the object. Usually a method called init is used to initialize the object. id myobject = [MyObject new]; id myobject = [[MyObject alloc] init]; Reference counting Objective-C employs reference counting as memory management system. Each object keeps an internal count of the number of references to the object. A new object always starts with a reference count of 1. When you re done using the object you call the release method. The release method decreases the internal Objective-C - Page 6/10

reference count. When an object s reference count reaches 0 the dealloc method will be called on the object. The dealloc method should cleanup internal references and release objects it has references to. When you use an object for a longer period of time you don t wan t it to be released by some other part of the program which leaves you with an invalidated object. To prevent an object from being released you send it the retain message. When an object receives a retain message it increases the internal reference count. This ensures the object won t be released until you send it a release message again. Garbage collection is currently not a standard feature of Objective-C. Third party extensions are available and Apple is rumored to be working on a garbage collector for a future version of the Cocoa framework. Dynamic Typing Like Smalltalk, Objective-C is a dynamically typed language. Messages can be send to objects that weren't specified in the interface. This is where selectors come into play. A selector represents the name of the method, and only one selector exists for each method with the same name and argument labels. This way you can lookup the method using a string value and bind it at runtime. When an object receives a message it looks if it can find an implementation for the selector on the object itself other wise it will forward the selector to one of it s super classes. The programmer can also decide to capture the message manually and send it off to a totally different object. In case the message can not be forwarded to another object an error handler will be used to throw a runtime exception. Sending the wrong message to the wrong object will cause run-time errors. To prevent these errors at runtime you could specify the class of an object, so the compiler will apply static-tying and will complain at compile time about messages an object will not respond to. - (void) setmyvalue:(id)someobject; - (void) setmyvaluetoastring:(nsstring *)astring; Internally functions and messages differ in several ways. The most important difference is the parameter count. Although this is not visible in the source code, messages always have one parameter that identifies the object the message is sent to. This object can be used in a method using the name self. Because of it s dynamic nature Objective-C has some really nice features usually only to be found in scripting languages. My favorite is the category. To keep your code modular Objective-C enables you to extend objects at runtime. For example to extend a NSString object to include a method that encodes it for url usage you could create a category called WebExtensions: Objective-C - Page 7/10

@interface NSString (WebExtensions) - (NSString *) urlencoded; @implementation NSString (WebExtensions) - (NSString *) urlencoded { // implementation The advantage of using this over class extensions it you don t have to alter your code to use your extended class. The urlencoded method will be added to all NSString s. Even if these NSStrings are created in a third party library and returned to you. The only languages I know of that support a feature like this are scripting languages like Ruby. Error handling Objective-C uses different way s of handling errors. When using C libraries like the Carbon (OS X c libraries) framework most of the error handling is done using return codes. Methods will return special codes for success or failure. Another method of handling errors is using the NSError object. A pointer to NSError is passed as an argument in a message. The NSError will be nil if the message succeeded or contain information about the error when one occurred. A third way of handling errors is using exceptions. Exception handling is very similar to Java exception handling. By default exception handling is done using the macro s NS_DURING and NS_HANDLER. A special compiler option enables the use of the compiler directives @try, @catch and @finally for a more Java like experience. @try { // some code that might throw an exception @catch (AnObjCClass *exc) { // handle exception @finally { // cleanup Throwing an exception can be done using the raise:format: method on a NSException object, or the shorthand @throw ExceptionObject. Compiling your program Good practice is to define object definitions in header files (.h) and the object implementations in implementation files (.m) like the C (.h,.c) files. But if you like you could have all code in one file. Objective-C - Page 8/10

A beginning developer wouldn t know how to compile objective-c code by hand. All compiling is handled by the developer tools (usually XCode). Under the hood XCode is using GCC to compile the code. Compiling Objective-C code using GCC requires a special compiler flag and requires you to link to the objc runtime. This runtime is very small and won t add much to the size of the application. And by using the runtime you save the space of an entire virtual machine. Like a scripting language Objective-C offers everything C has to offer, but adds features which makes Objective-C very dynamic. Because of this Objective-C behaves very much like a scripting language with the speed of a native C language. Writing code in Objective-C tends to be very clean and although the messaging syntax is something you might need to get used to it feels very natural after a while. One disadvantage of the Objective-C language is its lack of support for modules or packages. You can t just name your classes and a common practice is to prefix your class names using one or two characters. The NS prefix is preserved to Apple, inherited from the NeXTStep era. Apple makes use of these dynamic features (some of which it added to Objective-C itself) a lot in it s development tools to move the load from the developer to the development tools resulting in writing less code. This make writing applications for OS X very easy and fast. Objective-C - Page 9/10

Resources Books Brad J. Cox and Andrew J. Novobilski: Object-Oriented Programming: An evolutionary approach, Second Edition, 1991 Online Documents http://en.wikipedia.org/wiki/objective-c http://en.wikipedia.org/wiki/next http://developer.apple.com/ Objective-C - Page 10/10