Upload
netcetera
View
514
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
Development for the iPhone from a Java PerspectiveThe Tools, The Language, The Experience
Ognen Ivanovski
Netcetera
9140
2
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
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
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
6
New Priorities
> Performance
> Start-up time
> UI
> Simplicity
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
8
The Stack
> OS X (FreeBSD, Mach 3)
> libc (POSIX)
> Core Services
> Objective-C
> CocoaTouch
9
First Impressions: Toolbox
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
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
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
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
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
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
16
One Big Difference
No Source Code
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 :-)
18
Diving Deeper: The Platform
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
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
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];
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 *)
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?
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)
25
Classes: MyClass.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject {
int age;
NSString *name;
}
@end
26
Classes: MyClass.m
#import "MyClass.h"
@implementation MyClass
@end
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
28
Methods: implementation
#import "MyClass.h"
@implementation MyClass
+ (id)myClassWithName:(NSString *)name age:(int)age
{
return [[MyClass new] autorelease];
}
- (NSString *)name
{
return name;
}
@end
29
Selectors
> The message
> Dynamic invocation
SEL notification = @selector(jobDone:);id callback = // ...;[callback performSelector:notification withObject:result];
30
Instantiation
MyClass *instance = [[MyClass alloc] init];
// same as
MyClass *instance = [MyClass new];
31
Protocols
#import <Foundation/Foundation.h>
@protocol StateHandling
- (void)handleState:(id)state;
@optional
- (id)provideState;
@end
#import "StateHandling.h"
@interface MyClass <StateHandling> : NSObject {
// ...
}
// ...
@end
32
Protocols
id <StateHandling> handler = // ...;
[handler handleState:aState];
if ([handler respondsToSelector:@selector(provideState)]) {
id newState = [handler provideState];
}
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
34
NSObject
> The main root class
> Methods:
– class
– superclass
– description (toString)
– hash
– isEqual:
– isKindOfClass:
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
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
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
38
Patterns
39
Application Model
> SpringBoard
> Single Running Application
> Familiar (Swing)
> Main Thread w/ event loop
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];
41
Notification Center / Events
> Notification Center
– broadcast notifications
> Events
– user events
– responder chain
42
Delegate Pattern
43
Target / Action Pattern
> Interface Builder
– Outlets
– Actions
– Connections
44
MVC
> extension through inheritance
> mixed behavior and view construction
45
MVC (classic)
> views composed out of stock components in interface builder
> behavior in controllers
> connections in interface builder (outlets, actions)
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
Ognen Ivanovski http://mobile.netcetera.com
Netcetera [email protected]