Top Banner
Development for the iPhone from a Java Perspective The Tools, The Language, The Experience Ognen Ivanovski Netcetera 9140
47

iPhone development from a Java perspective (Jazoon '09)

May 28, 2015

Download

Technology

Netcetera

Based on experience gained in developing the popular Zurich train/tram/bus/ship timeplan transport application, wemlin, senior software engineer Ognen Ivanovski describes development for the iPhone from the perspective of an Enterprise Java developer - covering aspects about differences in the language, the architecture, the user experience, the tools, and the market.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: iPhone development from a Java perspective (Jazoon '09)

Development for the iPhone from a Java PerspectiveThe Tools, The Language, The Experience

Ognen Ivanovski

Netcetera

9140

Page 2: iPhone development from a Java perspective (Jazoon '09)

2

Page 3: iPhone development from a Java perspective (Jazoon '09)

3

Background

> Software Architect; @Netcetera since 2001

> 2000 - present: Java (enterprise, web, swing, RCP)

> 2008 - present

– Mobile Development: Android, iPhone OS

– Technical Lead for Wemlin

Page 4: iPhone development from a Java perspective (Jazoon '09)

4

> iPhone App

> Covers the Zürich greater area

> See departure times for public transport

> Completely offline app

> Database holds roughly 1.2M records

– but it’s fast

> http://mobile.netcetera.ch/wemlin

Page 5: iPhone development from a Java perspective (Jazoon '09)

5

Switching

> Team of four developers

> Published 1.0 of Wemlin (then Tramdroid) on October 3rd 2008

> 1 - 3 weeks adaptation time

> 1st internal build after 4 weeks

Page 6: iPhone development from a Java perspective (Jazoon '09)

6

New Priorities

> Performance

> Start-up time

> UI

> Simplicity

Page 7: iPhone development from a Java perspective (Jazoon '09)

7

F.A.Q.

> No, you can’t use Eclipse for development

> No, you cannot use Java either

> Yes, you do need a Mac (Intel based)

> iPhone SDK

– http://developer.apple.com/iphone

– xCode enhanced with iPhone specific frameworks and tools

– Registration is free, you can download, develop & test in the simulatora fee is required if you want to test on the device and to distribute

Page 8: iPhone development from a Java perspective (Jazoon '09)

8

The Stack

> OS X (FreeBSD, Mach 3)

> libc (POSIX)

> Core Services

> Objective-C

> CocoaTouch

Page 9: iPhone development from a Java perspective (Jazoon '09)

9

First Impressions: Toolbox

Page 10: iPhone development from a Java perspective (Jazoon '09)

10

First Impressions: xCode

> Yes, you are going to miss Eclipse

– Refactoring support in infancy but progressing

– SCM support is quite poor (NetBeans is comparable)

– Code browsing is poor

– No compile as you type

– Takes some will to switch

Page 11: iPhone development from a Java perspective (Jazoon '09)

11

xCode: What’s Interesting

> Multi-window support

> Great documentation browser

> Extensive documentation (1.5GB)

> Built on top of OpenSource technologies

– gcc

– gdb

– Unix scripts to enhance (stdin, stdout)

– in any scripting language

Page 12: iPhone development from a Java perspective (Jazoon '09)

12

Interface Builder

> Integral part of the SDK

> Probably the second visual editor I actually use(d)

> Integrates with xCode

– information about classes and their features

– Outlets, actions, connections

> NIB / XIB files

– very similar to Java .ser (serialized) files used in the early days of Swing by IDEs

Forte 4 Java

Page 13: iPhone development from a Java perspective (Jazoon '09)

13

First Impressions: Build system

> The same build system is used from both the IDE and command line

– xcodebuild

– one project file

– low build maintenance cost

> You don’t actually miss maven much

– Builds are easy to set up and maintain

– Advice: don’t reinvent the wheel (use the standard tool box)

> Frameworks

– no central repository

– no out of the box “materialization” of frameworksreliance on shared drives to achieve the goal

Page 14: iPhone development from a Java perspective (Jazoon '09)

14

First Impressions: Instruments

> It’s Just GREAT

> It’s based on DTrace

> DTrace is present on the devices too

> Almost real-time profiling & analysis both in Simulator and device

– Just “attach” on a running process

Page 15: iPhone development from a Java perspective (Jazoon '09)

15

Objective-C / Java connection?

> OpenStep API specification

– Developed jointly by NeXT and Sun Inc. @1993-1994

– Direct predecessor to Cocoa and CocoaTouch

> Java (probably) was strongly influenced by Objective-C

– http://www.virtualschool.edu/objectivec/influenceOnJava.html

> At least a common Smalltalk heritage

Page 16: iPhone development from a Java perspective (Jazoon '09)

16

One Big Difference

No Source Code

Page 17: iPhone development from a Java perspective (Jazoon '09)

17

Should I learn it?

> NeXT -> didn’t reach a wide audience

> Macs too (more or less) -> 8% world PC market

but

> Installed base of OS X including iPhones and iPod touches:

– grown from 25M to nearly 75M in the last 2 years

> The platform has reached an incredible number of developers and users in the last 2 years

> Customers asked for it :-)

Page 18: iPhone development from a Java perspective (Jazoon '09)

18

Diving Deeper: The Platform

Page 19: iPhone development from a Java perspective (Jazoon '09)

19

Devices

> iPhone

> iPhone 3G

> iPod Touch

> iPod Touch 2nd generation

> iPhone 3GS

> ARM11 ~ 400 MHz

> 128MB RAM (~50MB available to an app)

> Hardware Graphics Accelerator

> Slightly faster

> ARM Cortex V8 ~ 600 MHz

> 256MB RAM

Page 20: iPhone development from a Java perspective (Jazoon '09)

20

Objective-C

> Shared (Smalltalk) heritage with Java

– compiled

– late bound

– single inheritance

– one root class

– Pass-by-reference for objects, pass-by-value for “primitives”

– camelCase convention

Page 21: iPhone development from a Java perspective (Jazoon '09)

21

Syntax

> Strict superset of ANSI C

> Object syntax is borrowed from Smalltalk

– [object method] instead of

– object.method()

– [dictionary setValue:aValue forKey:key]

– [location setDistanceInMeters:3 from:zurich]

– [location setDistanceInKiloMeters:3 from:zurich]

– NSString *s = [NSString stringWithFormat:@"height %2i", height];

Page 22: iPhone development from a Java perspective (Jazoon '09)

22

Type System

> Dynamically typed, late bound

– Sending messages opposed to method invocation

– messages (selectors) have a type and can be passed around

> Type annotations are just “hints” to the compiler, can be added after the fact

– type id is like Object in Java (every object is of type id)

– you can send any message to an object of type id

> “object variable declaration”:

– NSString *variable = ...;

– id variable = ...; (id is like void *)

Page 23: iPhone development from a Java perspective (Jazoon '09)

23

Nil

> You can send any message to the nil object

– It will always succeed

> Return value is always with negative semantics– depends on the method signature / call site

– [nil isCreated] will return NO

– [nil numberOfCopies] will return 0

> Is this good?

Page 24: iPhone development from a Java perspective (Jazoon '09)

24

Namespace handling

> There isn’t any

> Prefixes are used instead (2 to 4 letters) on classes and global functions

– NSString (Foundation, historical, for NextStep)

– UIView (UI = UIKit)

– CLLocation (CL = CoreLocation)

Page 25: iPhone development from a Java perspective (Jazoon '09)

25

Classes: MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject {

int age;

NSString *name;

}

@end

Page 26: iPhone development from a Java perspective (Jazoon '09)

26

Classes: MyClass.m

#import "MyClass.h"

@implementation MyClass

@end

Page 27: iPhone development from a Java perspective (Jazoon '09)

27

Methods

#import <Foundation/Foundation.h>

@interface MyClass : NSObject {

int age;

NSString *name;

}

- (id)init;

- (id)initWithName:(NSString *)name;

+ (id)myClassWithName:(NSString *)name age:(int)age;

- (NSString *)name;

@end

Page 28: iPhone development from a Java perspective (Jazoon '09)

28

Methods: implementation

#import "MyClass.h"

@implementation MyClass

+ (id)myClassWithName:(NSString *)name age:(int)age

{

return [[MyClass new] autorelease];

}

- (NSString *)name

{

return name;

}

@end

Page 29: iPhone development from a Java perspective (Jazoon '09)

29

Selectors

> The message

> Dynamic invocation

SEL notification = @selector(jobDone:);id callback = // ...;[callback performSelector:notification withObject:result];

Page 30: iPhone development from a Java perspective (Jazoon '09)

30

Instantiation

MyClass *instance = [[MyClass alloc] init];

// same as

MyClass *instance = [MyClass new];

Page 31: iPhone development from a Java perspective (Jazoon '09)

31

Protocols

#import <Foundation/Foundation.h>

@protocol StateHandling

- (void)handleState:(id)state;

@optional

- (id)provideState;

@end

#import "StateHandling.h"

@interface MyClass <StateHandling> : NSObject {

// ...

}

// ...

@end

Page 32: iPhone development from a Java perspective (Jazoon '09)

32

Protocols

id <StateHandling> handler = // ...;

[handler handleState:aState];

if ([handler respondsToSelector:@selector(provideState)]) {

id newState = [handler provideState];

}

Page 33: iPhone development from a Java perspective (Jazoon '09)

33

Categories

> Every existing class can be extended with new methods using categories

– open classes

– limitation: no instance variables can be added

> Used quite often

Page 34: iPhone development from a Java perspective (Jazoon '09)

34

NSObject

> The main root class

> Methods:

– class

– superclass

– description (toString)

– hash

– isEqual:

– isKindOfClass:

Page 35: iPhone development from a Java perspective (Jazoon '09)

35

Reflection

id anObject;

[anObject respondsToSelector:@selector(handleTouch:)][anObject isKindOfClass:[UITableView class]][anObject isMemberOfClass:[UITableView class]][anObject conformsToProtocol:@protocol(Copying)]

> C based API (Objective-C runtime) for full blown introspection

Page 36: iPhone development from a Java perspective (Jazoon '09)

36

Memory Management

> Manual Reference Counting

> Autorelease pools

anObject = [argumentObject retain]

[anObject release]

return [anObject autorelease]

> THE biggest issue we had and still have

– dangling pointers

Page 37: iPhone development from a Java perspective (Jazoon '09)

37

Objective-C

> Dynamically Typed, late bound

> Semantically very similar to Java

– root class

– single inheritance

> Protocols are just like interfaces

– have optional methods

> Classes can be extended through categories

> Classes cannot be nested’

> Verbose

Page 38: iPhone development from a Java perspective (Jazoon '09)

38

Patterns

Page 39: iPhone development from a Java perspective (Jazoon '09)

39

Application Model

> SpringBoard

> Single Running Application

> Familiar (Swing)

> Main Thread w/ event loop

Page 40: iPhone development from a Java perspective (Jazoon '09)

40

Key-Value Coding (KVC)

> A convention similar to the JavaBean convention

– properties

– observing

> A bit more verbose and powerful

> You get a lot for free

– automatic observer notification

– very easy to use

> Accessor Methods

[anObject setName:@"name"];

[anObject name];

[anObject isHidden];

Page 41: iPhone development from a Java perspective (Jazoon '09)

41

Notification Center / Events

> Notification Center

– broadcast notifications

> Events

– user events

– responder chain

Page 42: iPhone development from a Java perspective (Jazoon '09)

42

Delegate Pattern

Page 43: iPhone development from a Java perspective (Jazoon '09)

43

Target / Action Pattern

> Interface Builder

– Outlets

– Actions

– Connections

Page 44: iPhone development from a Java perspective (Jazoon '09)

44

MVC

> extension through inheritance

> mixed behavior and view construction

Page 45: iPhone development from a Java perspective (Jazoon '09)

45

MVC (classic)

> views composed out of stock components in interface builder

> behavior in controllers

> connections in interface builder (outlets, actions)

Page 46: iPhone development from a Java perspective (Jazoon '09)

46

Take Away

> A Java developer is likely to feel at his/her uncle’s house on the iPhone

> Memory management will grow some gray hairs on you

> Love thy garbage collector

> Objective-C is a unique, intoxicating mix of high-level language and raw power

> Scroll speed is important

Page 47: iPhone development from a Java perspective (Jazoon '09)

Ognen Ivanovski http://mobile.netcetera.com

Netcetera [email protected]