Object C - University of Wisconsin-Platteville

mewlingtincupΛογισμικό & κατασκευή λογ/κού

9 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

112 εμφανίσεις

Objective
-
C



Quinton Palet

Department of Computer Science

University of Wisconsin
-
Platteville

paletq@uwplatt.edu



Abstract


Objective
-
C

is a
n

object oriented
language as well as a

strict
superset of C and is
primarily

used
in OS X

and iOS development
.
Objective
-
C o
riginally started in
1982

by Brad Cox and Tom
Love in an attempt to make a language which maintained

the

backwards compatibility of C while
also making it more reusable in
implementation
and

software design
.

Objective
-
C

has
recently

been growing in popularity due to Apple’
s
OS X and iOS increasing in popularity
, it is currently
ranked third on TIOBE programming Community Index which

is

up from fifth last year

[6
]
.
Object
ive
-
C is a dynamic lan
guage deferring many responsibilities until runtime, allowing it to
be more flexible. Objective
-
C is less versatile tha
n some languages due to its primary
development environment Xcode
,

which can only be ran on OS X.

Without Objective
-
C OS X
and iOS as we
know it today would be much different.





Introduction


Initially
Objective
-
C

was developed to bring large scale software development the power of
the
object
-
oriented
approach
.
The language is comprised of ANSI C and a small set of Smalltalk
inspired
extensions
.
It inherits much of its syntax from C along with its primitive types such as
int and char. Objective
-
C gets its object oriented constructs from Smalltalk using its message
passing.



History


Objective
-
C was first developed in the early 1980s
by Brad Cox and Tom Love at Stepstone. The
original language was an object oriented superscript of C with message passing based off of
Smalltalk.

In 1986 Objective
-
C was released by Stepstone.



P a g e

|
2


In 1985 Steve Jobs left Apple founded NeXT.

In 1988 Steve Jobs
licensed
Objective C license
for Ne
XT

from Stepstone

[1]
. Once Ne
XT

licensed Objective
-
C they extended the GCC
compiler to support Objective
-
C and built the NeXTStep operating system on Objective
-
C. In
1996 Apple bought NeXT and Apple r
olled the NeXTStep operating system into Mac OS X
.



Apple then continued to build onto the Mac OS X thus making all the later versions of OS X
Objective
-
C based. When apple released the first iOS device in 2007 they based it off of OS X
making all of the
iOS operating systems based off of Objective
-
C too.

Along with releasing iOS
is 2007 Apple release Objective
-
C that automated memory management, allowed for fast
enumeration, added garbage collection, and several other features.



Growing Popularity


Recently Objective
-
C has been growing in popularity; this is shown with Objective
-
C moving
from
fifth on the TIOBE programming Community Index last year to third this year

[6
]
. This is
due to Apple gaining more market share in the PC market in both persona
l PCs and business
PCs. Besides gaining market share in the PC market
,

it’s also gained more market share in the
tablet market with the iPad and also in the phone market with the iPhone. With the increase in
popularity of devices that run Objective
-
C based

operating systems
,

it also increased the
popularity in the development of Objective
-
C based applications to run on them.




Environments


Objective
-
C can be used across nearly any operating system. The main issue with using
Objective
-
C on any
operating s
ystems is the compilers that are available for that operating
system and the frameworks that the compiler supports. For Windows you can use GCC under
Cygwin or MinGW, the downside to this is that you will not have access to the Cocoa or
CocoaTouch framewor
ks that is necessary to do development for iOS

or Mac OS X
. For Linux
you can use the GNU compiler that was originally written for the GNU operating system, just
like for Windows you will not have access to the Cocoa or CocoaTouch frameworks. For OS X
you

can use Xcode for a

compile
r

that will
support more

frameworks than what are offered on
Windows or Linux,

this is

due to OS X being written on Objective
-
C

and Apple developing more
frameworks
.



Frameworks


Frameworks are a large part of Objectiv
e
-
C due t
o standard Objective
-
C not having many core
methods or classes. Frameworks are a collection of classes that can be imported into any class
and used.
The two most common
ly used

frameworks
are Cocoa and Cocoatouch
,

this is due to
Apples heavy involvement in the development of the frameworks and them being a large basis of
P a g e

|
3


the Mac OS X operating system
. Frameworks
will
also

usually

import other frameworks,

and
some frameworks are just a collection of frameworks maki
ng it easy to import all of the
necessary classes needed to for

a program.




Cocoa and Cocoatouch


The framework that gives OS X and iOS the distinct look and feel often associated with them is
Cocoa and Cocoatouch. Cocoa provides
most of the look and fee
l most commonly associated
with OS X and Cocoatouch provides the look and feel most commonly associated with iOS.


The Cocoa and Cocoatouch frameworks consist of

three main frameworks. In Cocoa the three
main frameworks are the
F
oundation framework,
A
pp
K
it framework, and CoreData framework.

The Foundation framework consists of many classes used for simple Objective
-
C programs that
are from NeXTStep such as NSString, which is Cocoa’s string class since Objective
-
C doesn’t
have a string class

[4
]
. The AppKi
t framework consists of functions that are used for creating an
OS X application and include classes that are used to handle windows, buttons, and other GUI
elements. The last framework in the Cocoa framework is the CoreData framework; this is used to
hand
le data within an application and the linking of an application to a database.


The Cocoatouch framework
,

like the Cocoa framework
,

has the Foundation framework an
d the
CoreData framework in it. However

instead of the AppKit framework it has
UIKit framewo
rk;
this is because the Cocoatouch framework used for iOS application development. Like the
AppKit framework the UIKit framework adds

classes that are used to handle GUI elements but
also includes classes to handle elements unique to iOS devices like maps,

gps, gestures, and
other elements.



Syntax


The syntax of
Objective
-
C

is
a superset of ANSI C
extended by providing syntax for defining
classes and methods.

What sets Objective
-
C apart from C++, C#, and Ja
va is that you can put
C
code
straight
into any Objective
-
C program and it will compile with no problem.
This is because
Objective
-
C is C with just more added on
.



Declarations


Objective
-
C programs are declared by their file extension. There are three different types of file
extensions for Objective
-
C, two of the most popular ones are .h and .m. The .h file extension
similar to C++ is used to declare it as the header file that is

used to define the interface of the
class. The .m file extension is used to declare the methods of the class which are based off of the
methods or functions declared in the interface file. The last type of file extension is .nm which is
P a g e

|
4


used to similar to

the .m file extension but can contain C++ code along with Objective
-
C and C
code.



Commenting


Objective
-
C

contains support for comments

just like any other C based

language.
In order to
comment out a single line you simply start the line with //, and in

order to comment out a block
of lines you start the block with /* and end it with */.

Figure 1 demonstrates both single line
commenting and block style commenting.




Figure 1: Example of Single and Block Commenting



Functions


Function declarations and calls are very similar between C, Java and
Objective
-
C
. Functions can
be either value returning or non
-
value returning. Also, functions can either have parameters or no
parameters.
Objective
-
C

at its core doesn’t have many functio
ns built in but you can add
frameworks into your Objective
-
C programs that will give you access to thousands of functions
and classes
.
Just like in any other language you can write your own functions
.

In order to write
your own function you must declare the method in
the interface

of the class which is in the
header file

which similar to C++ is suffixed by .h
.



Figure 2: Example of declarations


The plus sign at the beginning of the classMethod1
declaration

in Figure 2

means that the
function can be called on the class itself like a static function would in C++. Functions also
declared with the plus sign also have no access to instance variables. The minus sign at the
beginning of the classMethod2

declaration
in Figure 2
means that the function can only be called
on a particular instance of the class. You can also see that the classMethod2 has

parameters
passed to the function; the colo
n after the function name is what

separates the function name
f
rom the parameters passed to
it.


P a g e

|
5


The implementation of functions or methods is done in the implementation file that normally has
the file extension .m which originally stood for “messages”. Just like in C++ the implementation
is done using the interface
declaration.




Figure 3: Example of method implementation


Figure 3 demonstrates the implementation of a method that would be in the .m file using
declaration from the interface file or .h file.

Objective C also allows you to split the name of your
metho
d with parameters.



Figure 4: Example of method with split method name


Figure 4 demonstrates how to implement a method call with a split method name. This allows
you to make your code in Objective
-
C more readable than you can in other languages.



Unlike
in
other language
s
, Objective
-
C does not allow for operator overloads.
In other languages
like java you could have three constructor methods, all with the same name but have different
parameter types or number of parameters passed

in to initialize t
he object accordingly. In order
to do this in Objective
-
C you must make different names for the methods, this follows around
with maintain the readability of Objective
-
C code. Many Objective
-
C developers will include the
parameter type that they are initia
lizing with in the name of the
method
,
for example a
constructor initialized with a string may have the name constructorWithString. Another
downside to not being able to do operator overloads is that it doesn’t allow for polymorphism.
This can be a major d
ownside to Objective
-
C if the desired program to develop requires
polymorphism.



Data types and Variables


In Objective
-
C you have access to all of the primitive data types that are found in C with the
added in of BOOL. This may not seem like a lot of data types compared to C++ or Java but the
advantage Objective
-
C has is that you can use other frameworks that
will give you access to a
variety of different classes. One of the most popular frameworks would be Cocoa; the Cocoa
framework was developed by Apple
various

classes from the NeXTStep

operating
system
.


NSString *someString = @”hello world”;

Figure
5
:
Example of defining a variable

P a g e

|
6



This is shown with many of the commonly used variables beginning with NS, Figure 4
demonstrates how you would define a string in Objective
-
C with the Foundation framework.
The
reason you start someString with an * is because

NSString is an Object and someString is only a
pointer to that object.

You can also see in Figure 4 that the literal has an @ prefix before the
quotes, this is used to show the difference between a
string in C and Objective
-
C.



Objects


Objects are the
reason Objective
-
C was developed. Classes in Objective
-
C are done very similar
to C++ and Java. One of the big differences between Objective
-
C and other languages is the way
objects handle method calls or message

passing
. In order to call a method within a

class you pass
a class a message, this i
s done using bracket syntax similar to Smalltalk.


Object *O = [Object messagePassed: Param];

Figure 6
: Passing a message


In Figure 5 you c
an see that the message that is passed to Object is messagePassed with the

parameter of Param. Message passing is essential for Objective
-
C to allow objects dynamic
typing
. Dynamic typing allows postponing a message’s destination until runtime where as C++
and Java

do this at compile time. Message passing also allows the same me
ssage to be sent to a
variety of classes and those that cannot handle the message silently ignore it.



-

(void) setValue: (id) Obj;

Figure 7
: Example of dynamic typing


In
Figure 6

the method setValue the parameter passed in Obj

may be of any class, if the method
is able t
o handle the parameter pass in the message

it will but if it doesn’t it will silently ignore
it.



Memory Management


Objective
-
C doesn’t have memory management built in, so in order to make an object you would
have to allocate or alloc memory for the object.
You can then retain the object so that you are
able to pass the object from one class to another, but with retaini
ng an object you are responsible
to release the object once able to in order to free up resources. Releasing an object frees up
resources and was necessary early on when you didn’t have as much memory
.
An alternative to
release is autorelease. Autorelease
adds the object to an autorelease
pool that allows you to
release all of the objects in the pool with one call.

This differs from the majority of other higher
level language
s

that have built in garbage collection or have
a library that can be added for
garb
age collection.

P a g e

|
7





Objective
-
C 2.0


In 2007 Apple released Objective
-
C 2.0 that added in a collection of new features. One of the
most notable new features was garbage collection. The garbage collector ran on a background
thread and removed the need to ret
ain and release objects.

This feature was not available for iOS
development though, and was soon deprecated with the release of OS X v10.8 which
implemented ARC or Automatic Reference Counting

[5]
. Unlike the garbage collection ARC did
not
require an additional thread to manage the resources. ARC is a compile time feature that just
like the garbage collector removes the need for the developer to release and retain objects. What
ARC does is at compile time it will go through the program and d
etermine when objects are out
of scope and will then add in the release statements
[5]
. As you can see this will add time to
compile the program but will in turn make the program much more efficient than if a garbage
collector would be running.



Along wit
h ARC another new feature added in with Objective
-
C 2.0

was properties. Properties
introduced a new syntax for declaring instance variables. Property variables have optional
attributes to configure the generation of access methods such as getter and setter
s by declaring
the property with @synthesize. You can also opt to not use @synthesize if you wish to have the
properties set by other means in which case you use @dynamic to indicate the accessor methods
will be created by other means. Properties default t
o atomic which means by default they can
only be accessed by one thread at a time. In order to make a property nonatomic you must
declare it as such.



Figure 8: Example of property declaration and implementation


As you can see in Figure 8 this would be

an example of a property be declared and implemented.
The top line would go in the header file or interface file, and is an example of a property being
declared as nonatomic, it also sets the property to read only, and sets the getter method to
timestamp.

The second line in Figure 8 is the synthesize line that would go in the main file or
implementation file. This line tells the compiler at compile time to create the getter and setter
methods

for the property.


Lastly one of the more interesting features
implemented with Objective
-
C 2.0 would be dot
notation syntax. This allows you to call methods similar

to the way you would in Java or C++
with the object name followed by a period and then the parentheses with the parameters passed
in. The dot syntax does
n’t work for all methods though; specifically it will not work for methods
with split method names. This can be somewhat of a dilemma; you can opt to keep your codes
readability with the split method name, or you could opt to make it easier for developers
not
familiar with Objective
-
C to read/use.


P a g e

|
8



Objective
-
C or Java


Many early on developers run into the question of which language to learn first, and due to the
increase popularity in mobile devices many narrow their choices down to Java and Objective
-
C.
Both languages have benefits to learning them, but for general development purposes, and early
on learning it seems like Java would be the better decision. The reason for this is that Java has a
writ
ten standard unlike Objective
-
C.

T
his will make it easier

for developers to have access to
standardized libraries and documentation on the language easily. Objective
-
C documentation
isn’t consistent depending on the compiler
or operating systems
you use, which is why most
developers use Xcode. This is due to App
les documentation for the Xcode compiler and the
frameworks used with the Xcode compiler. Another reason Java is a better language for early on
developers to start with is that Java is ran on many more devices than Objective
-
C, the main
devices that use Ob
jective
-
C are all Apple devices, where as for Java it is used
on a great deal
more, from phones to majority of PCs to even ATMs. Objective
-
C does have the ability to be
more readable than Java, but this seems to be overcome by using proper naming and
docum
entation.


Another thing to consider when deciding between Objective
-
C and Java is the amount of
resources available for the program to run. Objective
-
C programs tend to take up more room than
Java programs. This is due to Objective
-
C programs having a dyn
amic runtime, the compiler
isn’t able to remove un used methods at compile time because there is no way of knowing
whether or not that method will be used at runtime. So if resource availability is an issue it is
usually a better option to go with Java ove
r Objective
-
C.


While Objective
-
C may not be a good language for early on development or general
development doesn’t mean that it isn’t a good language to learn at all. Apple’s Mac OS

X and
iOS has been growing in

popularity and with that will come the dem
and for programs to be ran
on them. While you can use other languages to develop for
Mac OSs

such as C++ and Ruby,
the
programs tend to need the Cocoa framework in order to have the functionality needed for them

to
run on Mac OS X
. This makes it so you
would still be developing in Objective
-
C and just adding
another layer between the application and the kernel which can reduce efficiency

with the
program
.





Conclusion


In conclusion,
Objective
-
C

has

come a long way since its first design

in the early 80’s

of being
only used
on a few operating systems
.
It has become a cross platform, multipurpose
object
oriented

language that can be
developed

on
many

operating system.
With Apple continuing to
grow and having all of their operating systems
based off of Objective
-
C it is most likely that
Objective
-
C will continue to grow in popularity and continue to grow in functionality. Without
Objective
-
C Apple today would be a completely different company with completely different
operating systems.

P a g e

|
9




Re
ferences


[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/#documen
tation/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