Cross Platform Objective C Development Using Gn Ustep

download Cross Platform Objective C Development Using Gn Ustep

If you can't read please download the document

Transcript of Cross Platform Objective C Development Using Gn Ustep

GNUstep Presentation

Cross-Platform Objective-C Development using GNUstepNicola Pero, Fosdem 2009

What is Objective-C ?

it is a programming language

it is a strict superset of C it is C with some new additional constructs

C code compiles as Objective-C code

C libraries can be linked and used natively from Objective-C

it adds Object-Oriented features to CDefining classes

Implementing methods

Instantiating classes and objects

Invoking class and object methods

Protocols, categories, selectors, forwarding

Example of Objective-C Code

Implementation of a classConstructor MethodC CodeInstance Method

Why is Objective-C so special ?

It is compatible with C libraries

Is is a simple extension of C

It is a hybrid between C and SmalltalkIt can be as fast as raw C if you avoid the object-oriented extensions

It can be as high-level and flexible as Smalltalk if you use the object-oriented extensions

It allows experienced programmers to mix different programming styles in the same program

It is extremely flexible

Objective-C Support

CompilersApple GCC

FSF GCC

Objective-C 2.0Introduced by Apple in Mac OS X 10.5

Garbage-collection

Threading/Exceptions

Fast Enumeration

Properties

Objective-C 1.0 code still compiles with a 2.0 compiler

Runtime LibrariesNeXT/Apple Runtime

GNU Runtime

Core Objective-C LibrariesApple Cocoa

GNUstep

Writing Portable Objective-C Code

Whenever possible, use only Objective-C 1.0 and avoid runtime-specific features.

On Apple Mac OS X, avoid Objective-C 2.0 syntax such as properties and fast enumerations.

On Apple Mac OS X, avoid garbage collection and use explicit the traditional OpenStep reference counting. This is likely to change GNUstep is getting garbage collection again.

On both Apple and GNUstep, If you need to access the runtime, use an intermediate layer such as the one provided by gnustep-base additions.

History of Objective-C Core Frameworks

NeXTstep 1.0 (1989)NeXTSTEP 3.0 (1992)OpenStep API (1994)GNUstep (1995)Apple Cocoa (2000)StepStone (early 1980s)Apple iPhone (2008)

OPENSTEP 4.0 (1996)

Apple Cocoa 10.5 (2008)GNUstep 1.18 (2008)

OpenStep API Specification (1994)

ApplicationFoundation KitApplication KitOperating SystemOS Graphical SystemObjective-C Runtime

OpenStep API Specification (1994)

Foundation KitNon-graphical classesRoot classes: NSObject, NSProxy

Basic data classes: NSString, NSNumber, NSData, NSNull

Collection classes: NSArray, NSDictionary, NSSet

Execution control classes: NSRunLoop, NSTimer, NSThread, NSLock

I/O classes: NSTask, NSFileHandle

Notification classes: NSNotification, NSDistributedNotification

Serialization classes: NSArchiver, NSCoder

Resource management classes: NSBundle, NSUserDefaults

Distributed Objects Classes: NSConnection, NSPort

And many more...

ApplicationFoundation KitApplication KitOperating SystemOS Graphical SystemObjective-C Runtime

OpenStep API Specification (1994)

Application KitGraphical classesWindow classes: NSWindow, NSPanel

Menu classes: NSMenu, NSMenuItem

Basic drawing classes: NSView, NSColor, NSFont, NSGraphicsContext

Basic interaction classes: NSEvent, NSResponder, NSControl, NSCell

Control classes: NSButton, NSTextField, NSBrowser, NSPopUpButton, NSMatrix

Table classes: NSTableView

Text classes: NSTextView, NSTextStorage, NSLayoutManager, NSInputManager

Scrolling classes: NSClipView, NSSplitView, NSScrollView,

Standard panel classes: NSFontPanel, NSSavePanel

And many more...

ApplicationFoundation KitApplication KitOperating SystemOS Graphical SystemObjective-C Runtime

Apple Cocoa

ApplicationCocoa Foundation KitCocoa Application KitOperating System (Apple Darwin/BSD)OS Graphical SystemApple Objective-C Runtime

GNUstep

ApplicationGNUstep BaseGNUstep GUIOperating System (Any!)Graphical System (Any!)GNU Objective-C Runtime

GNUstep Back

Portability

GNUstep:GNU/Linux

OpenBSD, FreeBSD, *BSD

OpenSolaris, HP UX, other Unix systems

Microsoft Windows

Apple Cocoa:Apple Mac OS X

Writing Portable Objective-C Code

Whenever possible, use only the OpenStep API (Foundation Kit and Application Kit).

Avoid as much as possible using platform-specific, non-portable frameworks.On Apple Mac OS X, avoid Apple-only frameworks such as CoreFoundation, CoreSound, CoreAnimation, KeyChain, Carbon, Quartz, Quicktime, WebKit.

On GNUstep, avoid GNUstep extensions that are not available as a separate library or framework that works on Apple as well.

Foundation Kit Example

#import

int main (void){ NSLog (@Hello world!); return 0;
}

A (Trivial) Portable Objective-C Program

File hello.m:

Building the Foundation Kit Example

On Apple Mac OS X, there is XCode

Only for Apple Mac OS X

Complete, total IDE

It uses its own files

Hard to work with developers who use other platforms where Xcode is not available

Building the Foundation Kit Example

On GNUstep, there is Project Center

Works everywhere

Similar to XCode

Lagging in features (this might change)

Uses its own files, then generatesgnustep-make GNUmakefiles to build

Building the Foundation Kit Example

On GNUstep, there is also gnustep-make

Works everywhere,even without a GUI

Makefile library designed to make building complex projects easy

Official building system for GNUstep

Very complete

Editable, portable text files

Requires editing text files - not a point-and-click GUI IDE

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = hellohello_OBJC_FILES = hello.m

include $(GNUSTEP_MAKEFILES)/tool.make

Building the Foundation Kit Example

Not Portable

Portable

GUI IDE

Text files

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = hellohello_OBJC_FILES = hello.m

include $(GNUSTEP_MAKEFILES)/tool.make

XCode

Project Center

gnustep-make

Pbxbuild can generate GNUmakefiles from Xcode projects

More Foundation Kit Portability Issues

Loading and locating resources and filesWindows paths

Unix filesystem layout

=> Always use the OpenStep API (NSBundle, NSPathUtilities)Tools with Resources (GNUstep) are not available on Apple

Frameworks vs Shared librariesFrameworks are shared libraries with resources

Apple has native linker implementation, GNUstep emulates them

Emulation can be inefficient/inappropriate on some platforms

Even More Foundation Kit Portability Issues

#ifdef GNUSTEP /* GNUstep-specific code */#else /* Apple-specific code */#endif

#ifndef GNUSTEP# include GNUstep.h#endif

Platform-dependent CodeUser GNUSTEP preprocessor define, which is defined on GNUstep but not on Apple

GNUstep macrosASSIGN, RELEASE, etc

Use GNUstep.h (eg, from Renaissance)

Application Kit Programs

If you create your GUI using the OpenStep API, it's pretty much the same ...

...

buttons[0] = [[NSButton alloc] initWithFrame: NSMakeRect (77, 3, 34,24)]; [buttons[0] setButtonType: NSToggleButton]; [buttons[0] setTitle: @"0"]; [buttons[0] setTag: 0]; [buttons[0] setState: NO]; [buttons[0] setAction: @selector(digit:)]; [buttons[0] setKeyEquivalent: @"0"];

...

PS: most developers don't use the OpenStep API directly but use Xcode or Gorm or Renaissance. We'll look at these tools later.

Menu Portability (hard)

Whenever possible, use only the OpenStep API (Foundation Kit and Application Kit).

Avoid as much as possible using platform-specific, non-portable frameworks.It is impossible to create the application menu on Appleusing only the OpenStep API.

We can only hope that this will change in the future.On Apple you must use the Apple-only method NSApplication -setAppleMenu:which is not part of OpenStep and not even declared in a public header (!!).

Menu Portability (hard)

Whenever possible, use only the OpenStep API (Foundation Kit and Application Kit).

Avoid as much as possible using platform-specific, non-portable frameworks.The best solution is to write two different menu codes:one for Apple Cocoa, one for GNUstep.

Luckily there is only one application menu in a program. :-) On Apple you need to use a goofy private Apple-only setup.

On GNUstep and Apple the standard organization of menus (location of menu items, etc) are very different.

Example: Gomoku.app

http://www.gnustep.it/nicola/Applications/Gomoku

A small game, originally for GNUstep only

It uses only the OpenStep API

It creates all the GUI in code

It has separate code to create the menu on Apple Cocoa and GNUstep

It has a GNUmakefile and is built using gnustep-make

You can use it as an example of how to build a portable GUI in code.

Example: Gomoku.app

http://www.gnustep.it/nicola/Applications/Gomoku

Example: Gomoku.app

http://www.gnustep.it/nicola/Applications/Gomoku

Example: Gomoku.app

http://www.gnustep.it/nicola/Applications/Gomoku

Creating GUIs without code

On Apple Mac OS X, there is Interface Builder

Only for Apple Mac OS X

Brilliant tool, extremely usableand fast

It uses its own files (Nibs)

NIB files serialize GUI objects, including platform-specific attributes such as fonts, sizes and colors

Static layout

Need to manually create a different NIB file for each language

Creating GUIs without code

On GNUstep, there is Gorm

Works on GNUstep

Very similar to Interface Builder

Uses its own files (Gorm files)

Same pros/cons as Interface Builder

Can import NIBs from Interface Builder (they might need manual adjustment after importing)

Creating GUIs without code

On GNUstep, there is also Renaissance https://www.gnustep.it/Renaissance

Works everywhere

GNU LGPL framework

Open XML file format

Not that complete

Lacking a GUI builder

Editable, portable text files

Autolayout using boxes and grids

Can load up translations at runtime: a single file can support alllanguages

Creating GUIs without code

Not Portable

Portable

GUI IDE

Text files

Interface Builder

Gorm

Renaissance

Renaissance GUI Builder (TODO)

The future of free, cross-platform Objective-C

Free, cross-platform Objective-C needs your help!Try out the GNUstep frameworks and tools

Give constructive feedback, suggest improvements, provide patches

Create examples, tutorials

Write applications

Port existing applications back and forth between GNUstep and Apple Cocoa

Help improving GNUstep ports to Windows andother platforms