Open menu with table of contents Objective-C
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

Objective-C

  • Developed by Brad Cox and Tom Love in the early 80s (StepStone)
  • Object oriented extension to ANSI C
  • Strict superset of C
    • Can be mixed with C and C++
  • Dynamic runtime
  • Loosely typed (if you want it)
  • Inspired by Smalltalk
  • Goal: "As fast as C but as beautiful as Smalltalk"
center 40%

83%

Stuttgart Media University

1 History of Objective-C

  • Steve Jobs licensed Objective-C in 1988
  • AppKit and Foundation were developed
  • Development base for NeXTStep UI
  • Development Tools Projekt Builder and Interface Builder

2 Well known Objective-C Projects

  • ...?

3 Well known Objective-C Projects

  • Tim Berners Lee: First Web-Server & Web-Browser (1990)
  • John Carmack: Doom and predecessor Wolfenstein 3D
  • Project Builder and Interface Builder are the basis of Xcode
  • NeXTStep and OpenStep are the basis of MacOS X and iOS
  • iWork (Keynote, Numbers, Pages)

center 60%

[Source:Wikipedia](https://de.wikipedia.org/wiki/Tim_Berners-Lee)

4 Objective-C

  • Until 2012 Objective-C was constantly updated
  • Compiler Clang on basis of LLVM
  • Objective-C is and stays a modern programming language
  • TIOBE index: Objective-C was language of the year 2011 and 2012*
  • Q4 2014: Swift is introduced as new language for iOS and Mac OS X development by Apple
  • Q4 2015: Swift 2.0 was released changing some parts of the syntax and introducing error handling
    • with try / catch / throws. Swift is OpenSource
  • Q4 2016: Swift 3.0 released with major changes
  • Q3 2017: Swift 4.0 released
  • Q1 2019: Swift 5 released with ABI stability
  • The rest is history...

5 Objective-C Example

  • Short Objective-C application (using Foundation) coding demo using CLang compiler frontend:
    • clang -fobjc-arc testapp.m -o testapp

center

6 Objective-C Files

  • Separation of public declaration and implementation in header and source files (like in C)
Extension Source Type
.h Header files. Header files contain class, type, function, and constant declarations.
.m Source files. This is the typical extension used for source files and can contain both Objective-C and C code.
.mm Source files. A source file with this extension can contain C++ code in addition to Objective-C and C code. This extension should be used only if you actually refer to C++ classes or features from your Objective-C code.

7 Class Interface

  • Class declaration in *.h
@interface MyClass : NSObject
{
    int         count;
    id          data;
    NSString*   name;
}
- (id)initWithString:(NSString*)aName;
+ (MyClass*)createMyClassWithString:(NSString*)aName //Class method;
@end

8 Class Interface

  • Class implementation in *.m
@implementation MyClass

- (id)initWithString:(NSString *)aName
{
    self = [super init];
    if (self) {
        name = [aName copy];
    }
    return self;
}

+ (MyClass *)createMyClassWithString: (NSString *)aName
{
    return [[self alloc] initWithString:aName] ;
}
@end

9 NSObject

  • NSObject is root class (similar to Object class in Java)
  • Provides functionality for
    • Memory management (allocation, release)
    • Object equality
    • Introspection
  • Part of the Foundation framework

10 Using a class

  • Create an instance of a class in two steps:
    • Dynamically allocate memory
    • Initialize the allocated memory with values
//create an Object of SomeClass:
id anObject = [[SomeClass alloc] init];

//Example init method implementation in NSObject:
- (id)init {

    self = [super init];
    if (self) {
        //initialisation of Object
    }

    return self;
}

11 Using Objects

  • Object initialization example using NSString object
NSString* text1 = [[NSString alloc] initWithCString: "Hello World!" encoding: NSUTF8StringEncoding];

NSString* text2 = @"Hello World!"; //short for upper statement
NSLog(@"text 1 = %@", text1); //%@ placeholder for objects
NSLog(@"text 2 = %@", text2);

NSLog(@"text2.length = %ld", text2.length);

12 Methods

  • Method declaration
    • Instance method identifier: minus (-)
    • Class method type identifier: plus (+)

center 60%

Source:Stackoverflow

13 Methods

  • Calling a method is done by messaging an object
  • A message is the method signature plus parameter information
  • Messages are dispatched dynamically, facilitating the polymorphic behavior of Objective-C classes
  • Messages are enclosed by brackets and look like this:
 [myArray insertObject:anObject atIndex:0];

14 Messaging Terminology

  • Message expression
    • [receiver method:argument]
  • Message
    • [receiver method:argument]
  • Selector
    • [receiver method:argument]
  • Method
    • The code selected by a message

15 Object Messaging Examples

  • Messages can be nested to avoid declaring numerous local variables
  • The return value from each nested message is used as a parameter, or as the target, of another message
[[myAppObject theArray] insertObject: [myAppObject objectToInsert] atIndex: 0];

16 Object Messaging Examples

// message without argument
[receiver message];

// message with single argument
[receiver message: argument];

// message with multiple arguments
[receiver message: arg1 argument2: arg2];

// receiving a return value
int status = [receiver message];

// message with a variable number of arguments
[receiver makeGroup: group, memberOne, memberTwo, memberThree];

17 Object Messaging Examples

  • Sending messages to the class is the same as sending them to an instance, you only need to use the class name
// nil is essentially the same as NULL

NSMutableArray *myArray = nil;

// Create a new array and assign it to the myArray variable.

myArray = [NSMutableArray array];

18 Dynamic Typing

  • Supports strongly and weakly (dynamic) typed variable declarations
 Person* myPerson;  // Strong typing

 id myObject;  // Weak typing
  • Dynamic type id is completely unrestrictive
  • The type of an object is only determined at runtime
  • Allows for introspection and dynamic binding

19 Dynamic Binding

  • Supports (dynamic) binding of methods at runtime Allows high flexible programming paradigms
//Dynamic binding example
@interface ClassA : NSObject {…}
- (void) doSomething;
@end

@interface ClassB : NSObject { …}
- (void) doSomething;
@end

// anywhere
id object = … // An object of an arbitrary class
[object doSomething]; // polymorph between ClassA und ClassB

20 Hands On Objective-C

Please open the terminal and create your first Objective-C project.

  • Exercise: Create a new Objective-C project that prints "Hello World" to the console

Exercise

21 Swift vs Objective-C

Objective-C:

NSString *helloWorld = @"Hello World";
NSLog(@"%@", helloWorld);

Swift:

var str = "Hello World"
print(str)