Objective-C

mewlingtincupSoftware and s/w Development

Nov 9, 2013 (4 years ago)

90 views

Objective
-
C

Quinton
Palet

Introduction


Objective
-
C was developed to bring large scale software development the
power of the object
-
oriented approach


Comprised of C and Smalltalk


Strict superset of C

History


Early 1980’s


Brad Cox and Tom Love develop Objective
-
C at
Stepstone


1985


Steve Jobs leaves Apple and starts NeXT


1986


Stepstone

releases Objective
-
C


1988


Steve Jobs licensed Objective
-
C for NeXT


1996


Apple buys NeXT


1999


Apple releases OS X


2007


Apple releases
iOS

and Objective
-
C 2.0

Growing Popularity


From 5
th

to 3
rd

on the TIOBE programming community index


Apple gaining more market share in PC market


iPhone,
iPad
, iPod Touch, and Apple TV


http://www.apple.com/ios/videos/#developers/

Environments


Windows


GCC
under Cygwin or
MinGW


Linux


GNU compiler


Mac


Xcode


Frameworks


Collection of classes


Provide methods beyond the core methods


Frameworks often contain other Frameworks


Cocoa &
Cocoatouch


Most popular frameworks


Cocoa


Foundation framework


CoreData

framework


AppKit

framework


Cocoatouch


Foundation framework


CoreData

framework


UIKit

framework


File Declarations


Declared by file type


.h Interface


.m Implementation


.nm Implementation with C++ code along with Objective
-
C and C code

Syntax


Strict superset of C


Can compile C code without any linkage


Commenting the same


// single line


/* block */

Data types and Variable


Objective
-
C by itself has


Int


Float


Double


Char


BOOL


With frameworks has access to much more


Objective
-
C 2.0 Added


Int

(signed/unsigned)


Short


Long


Long
Long


Id

Data types and Variables cont.


Using
Foundation.framework

you can define a string:


NSString

*
someString

=
@”
hello world
”;



* represents that it is a pointer


NSString

*
SecondString

= [[
NSString

alloc
]
initWithFormat
:@”hello, %@”, self.name];


Syntax
-

Functions


Declare method in interface file or .h

+ (
return_type
) classMethod1;

-
(
return_type
) classMethod2: (param1_type) param1_name;



+ similar to a static function in Java


-

similar function called on instance of class


-

(void)
insertObject
: (id)
someObject

atIndex
: (
int
) index;

Syntax


Functions cont.


Implementation similar to C++ using the interface declaration


Done in the .m or .nm file


+ (
return_type
)
classMethod
: (
paramType
)
paramName

{


Return (
return_type
)
returnName
;

}


Functions cont.


Objective
-
C does not allow for Function overloads


This makes it so Objective
-
C isn’t able to easily implement polymorphism


Where as Java can implement constructors like:

public
initPerson
(){


name = DEFAULTNAME;

}

Public
initPerson
(String
inName
){


name =
inName
;

}

Functions Cont.


In order to do this in Objective
-
C you would have to implement different
method names like:

-
(void)
initPerson

{


name = [[
NSString

alloc
]
initWithString:DEFAULTNAME
];

}

-
(void)
initPersonWithName
: (
NSString

*)
inName
{


name = [[
NSString

alloc
]
initWithString
:
inName
];

}


Functions Cont.


Objective
-
C does not allow for Operator overloads


In turn Objective
-
C does not allow ad
-
hoc polymorphism


Example in C++ or C#:


Time
Time
::operator+(const Time&
addTime
)


{



Time temp = *this;



temp.seconds

+=
addTime.seconds
;



temp.minutes

+=
temp.seconds

/ 60;



temp.seconds

%= 60;



temp.minutes

+=
addTime.minutes
;



temp.hours

+=
temp.minutes

/ 60;



temp.minutes

%= 60;



temp.hours

+=
addTime.hours
;



return temp;


}


Allows you to do:
sumTime

=
firstTime

+
secondTime
;


Functions Cont.


Simple Solution make a function that does what you would want the operator
overload to do.


-
(void)
addTime
: (Time)
inTime
{



self.seconds

+=
inTime.seconds
;



self.minutes

+=
self.seconds

/ 60;



self.seconds

%= 60;



self.minutes

+=
inTime.minutes
;



self.hours

+=
self.minutes

/ 60;



self.minutes

%= 60;



self.hours

+=
inTime.hours
;


}



So can be called by [
timeOne

addTime
:
timeTwo
];


Messages


Messages are similar to a method call in C++ or Java

Object *O = [Object
messagePassed
:
Param
];


Message passing allows dynamic typing


Allows postponing a messages destination until runtime


If class can’t handle the message it can forward it, hold onto it,
or silently ignore it

-

(void
)
setValue
: (id)
Obj
;


Obj

can be any class

Memory Management


No built in memory management


Must allocate or
alloc

memory for objects



NSString

str

= [[
NSString

alloc
] init];


Can retain memory to pass objects



NSString

str

= [[[
NSString

alloc
] init] retain];


Developer responsible for releasing objects



[
str

release];

Memory Management


Autoreleasepool

allows you to group objects and release them all at once or
“drain” the pool






NSAutoreleasePool

* pool = [[
NSAutoreleasePool

alloc
] init];


// create
autoreleased

objects


NSString

str

= [[[
NSString

alloc
] init]
autorelease
];


[pool release];


Objective
-
C 2.0


Added in


Garbage collection


Modern runtime


Fast enumeration


Replaced:



NSEnumerator

*enumerator = [
someCollection

objectEnumerator
];



someObject

*O



While ((O = [enumerator
nextObject
]) != nil) {




NSLog
(@”%@”, [O name];



}


With:



for(
someObject

*O in
someCollection
) {




NSLog
(@”%@”,[O name];



}


dot syntax


Without dot syntax:

[
someClass

objectAt
: 0
setName
: @”John”];


With dot syntax:

someClass
[0].name = @”John”


Objective
-
C
2.0 cont.


Added in Property
variables


Can
be implemented with @synthesize or @
dynamic


@property (
nonatomic
)
propertyType

*
propertyName
;

@property (
nonatomic
,
readonly
, getter = timestamp)
propertyType

* propertyName2;


@synthesize
propertyName
;

@synthesize
propertyName2;

Objective
-
C 2.0 cont.

So this:

@property (
nonatomic
)
propertyType

*
propertyName
;

@property (
nonatomic
,
readonly
, getter =
propertyName
)
propertyType

* propertyName2;


@synthesize
propertyName
;

@synthesize propertyName2
;


Turns into this:

@interface Event{


propertyType

*
propertyName
;


propertyType

* propertyName2; }

@implementation Event{


-
(
propertyType
)
propertyName
{



return
propertyName
; }


-
(void)
setPropertyName
: (
propertyType
) in {



_
propertyName

= in; }


-
(
propertyType
)propertyName2{



return [self
propertyName
]; }

}@end

Objective
-
C 2.0 cont.


You can choose to dynamically do your properties getter and setter methods
also, for example:

@interface Event{


@property (
nonatomic
)
propertyType

propertyName
;

}

@implementation Event{


@dynamic
propertyName
; // need this because it lets the compiler know you will implement





// your own getter and setter methods for the property


-
(
propertyType
)
propertyName

{



return
propertyName
;


}


-
(void)
setPropertyName
: (
propertyType
)
inValue

{



propertyName

+=
inValue
;


}

}@end



Automatic Reference Counting


Garbage collection was deprecated


Developer no longer needs to manually manage retain and release


Retain and release added in by compiler at compile time


Removed the overhead of a separate process managing retain counts

Objective
-
C or Java


Both have benefits to learning them, and are the two of the most popular
languages for mobile development.


Both are in the top 4 of the TIOBE index


Java has a written standard


Objective
-
C has no written standard but is heavily documented by Apple


Java is supported on more devices where as Objective
-
C is primarily Apple
devices.


Java has IDEs for all major operating systems where as Objective
-
C is only on
Mac OS X

Objective
-
C or
Java cont.


Objective
-
C can be more readable than Java


Objective
-
C is a more dynamic language


Allows you to easily add onto programs due to dynamic
typing


Objective
-
C programs tend to be larger due to compiler not being able to
strip methods or make them inline because of the dynamic typing you wont
know what methods are needed until runtime.


Objective
-
C does not allow operator overloading, function overloading, and
multiple inheritance.


Objective
-
C doesn’t waste resources on a garbage collector


In order to add an Objective
-
C app to their marketplace it must go through a
screening and you need a developers license, Java has open marketplaces
and also requires no developers license

Objective
-
C or Java cont.


Overall I would say Java is a more useful language to learn rather than
Objective
-
C


Doesn’t mean there’s no benefit to learning Objective
-
C


Apple
has a large market share in PC, mobile phones, and tablets
.


You can use other languages to develop for OS X and
iOs


Such as C++ and Ruby


Many times still need to use the Cocoa framework to add the necessary functionality


Makes it so you’re still developing in Objective
-
C and only adding another layer in below
the application making it less efficient


http://www.youtube.com/watch?v=OpNBbdA3jq0

For more Information on Objective
-
C


https://developer.apple.com/

Questions ?

References


[1]
Kaul
,
Vivek
. (2009, May 11).

What Jobs did when he was fired from Apple
. Retrieved from

http://www.dnaindia.com/money/report_what
-
steve
-
jobs
-
did
-
when
-
he
-
was
-
fired
-
from
-

apple_1254757


[2]
Biancuzzi
, F., & Warden, S. (2009).

Masterminds of programming: Conversations with the

creators of major programming languages

.
Sepastopol
, CA: O'Reilly Media.



[3] Apple. (2010, December 13).

Cocoa fundamentals guide
. Retrieved from

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFunda

mentals
/Introduction/Introduction.html


[4] Apple. (2011, October 12).

The objective
-
c programming language
. Retrieved from

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/I

ntroduction
/introObjectiveC.html


[5] LLVM. (2012).

Automatic Reference Counting
. Retrieved from

http://clang.llvm.org/docs/AutomaticReferenceCounting.html


[6] TIOBE Software. (2012, November).

TIOBE programming community index for November

2012
. Retrieved from http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html