42
Objective-C for C# and Java Developers JaxCodeCamp 2012

Objective-C for C# and Java Developers

  • Upload
    ponce

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

Objective-C for C# and Java Developers. JaxCodeCamp 2012. Who am I. David Fekke .NET Developer by day, iOS by night Presenter JaxMUG, JaxDug, JaxJug, JSSUG Mac User 1986. JaxMUG. Meet once a month at the Southside Library. jaxmug.com. Jesse Eisenberg. The Apple Way. Assumptions. - PowerPoint PPT Presentation

Citation preview

Page 1: Objective-C for C# and Java Developers

Objective-C for C# and Java

DevelopersJaxCodeCamp 2012

Page 2: Objective-C for C# and Java Developers

Who am I

•David Fekke

•.NET Developer by day, iOS by night

•Presenter JaxMUG, JaxDug, JaxJug, JSSUG

•Mac User 1986

Page 3: Objective-C for C# and Java Developers

JaxMUG

Meet once a month at the Southside Library

jaxmug.com

Page 4: Objective-C for C# and Java Developers

Jesse Eisenberg

Page 5: Objective-C for C# and Java Developers

QuickTime™ and a decompressor

are needed to see this picture.

The Apple Way

Page 6: Objective-C for C# and Java Developers

Assumptions

•Somewhat familiar with OO concepts

•Used a language like Java, C# or C++

•Open minded

•Actually used an iPhone, iPad or iPod Touch

Page 7: Objective-C for C# and Java Developers

Requirements

•You need a Intel Mac with Mac OS X

•Some knowledge of OO concepts

•Xcode tools (FREE)

•iOS (SDK)

Page 8: Objective-C for C# and Java Developers

iOS Platform

•ARM Processor

•128/256/512/1024 MB RAM

•BSD UNIX

•Mach Microkernel

•COCOA APIs

Page 9: Objective-C for C# and Java Developers

COCOA

•COCOA is a OO Framework

•Based on NextStep

•Mostly written in Objective-C

•iOS Devices use COCOA Touch

Page 10: Objective-C for C# and Java Developers

Cocoa Touch

Media

Core Services

Core OS

Page 11: Objective-C for C# and Java Developers

COCOA Framework•NS (NextStep)

•CF (Core Foundation)

•CA (Core Animation)

•CI (Core Image)

•Core Data

•OpenGL ES

Page 12: Objective-C for C# and Java Developers

COCOA Conventions

•Most classes begin with NS, I.E. NSObject, NSString, NSArray or NSNumber

•Designed around MVC pattern

•Heavy use of delegation

•iOS specific components based on UIKit

Page 13: Objective-C for C# and Java Developers

Tiobe Programming Index

Position Language

1 C

2 Java

3 Objective-C

4 C++

5 C#

Page 14: Objective-C for C# and Java Developers

Objective-C•Somewhere in-between C++ and Java

•Created by Brad Cox and Tom Love in 1982

•Based on C with SmallTalk like extentions

•Used in COCOA, OpenStep and GNUStep

•Class based OO language

Page 15: Objective-C for C# and Java Developers

-(BOOL)validateNumRangeWithStartNumber:(int)startNumber EndNum:(int) endNumber{

if (startNumber >= endNumber){

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"End value Too Small" message:@"Sorry" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];[alertView release]; return YES;

} else {return NO;

}}

Page 16: Objective-C for C# and Java Developers

Obj-C vs C#

Obj-C C#[[object method]

method];obj.method().method();

Memory Pools Garbage Collection +/- static/instancenil null

(void)methodWithArg:(int)value {}

void method(int value) {}

YES NO true false@protocol interface

Page 17: Objective-C for C# and Java Developers

Objective-C Structure

•Obj-C Class composed of two files: header and implementation, or .h and .m

•header uses the @interface and implementation uses @implementation

Page 18: Objective-C for C# and Java Developers

#import <UIKit/UIKit.h>

@interface LottoRandomAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window;}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

Page 19: Objective-C for C# and Java Developers

#import "LottoRandomAppDelegate.h"

@implementation LottoRandomAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch [window makeKeyAndVisible];}

- (void)dealloc { [super dealloc];}

@end

Page 20: Objective-C for C# and Java Developers

Properties

•Objective-C 2.0

•@property (strong) NSString *myStr;

•@synthesize myStr = _myStr;

•No longer need @synthesize keyword in iOS 6.0

Page 21: Objective-C for C# and Java Developers

Property Getters and

Setters@synthesize myStr = _myStr;

-(NSString *)myStr{ return _myStr;}

-(void)setMyStr:(NSString *)aMyStr{ _myStr = aMyStr;}

Page 22: Objective-C for C# and Java Developers

@interface extension

@interface MasterViewController () { NSMutableArray *_objects;}@end

Page 23: Objective-C for C# and Java Developers

Categories

•Similar to method extensions and partial classes

•You can add methods to classes out side of the implementation file

Page 24: Objective-C for C# and Java Developers

@interface NSString (reverse)-(NSString *) reverseString;@end

@implementation NSString (reverse) -(NSString *) reverseString{ NSMutableString *reversedStr; int len = [self length];  // Auto released string reversedStr = [NSMutableString stringWithCapacity:len];   // Probably woefully inefficient... while (len > 0) [reversedStr appendString:[NSString stringWithFormat:@"%C", [self characterAtIndex:--len]]];   return reversedStr;} @end

Page 25: Objective-C for C# and Java Developers

Blocks

•C constructs for performing closures

•Similar to Lamda expressions in C#

•^{ ... Code goes here ... }

•Newer Obj-C APIs require blocks

•Used heavily in GCD

Page 26: Objective-C for C# and Java Developers

dispatch_queue_t myQueue = dispatch_queue_create("get people list", NULL); dispatch_async(myQueue, ^{ NSURL *myURL = [NSURL URLWithString:@"http://192.168.1.70/APISamples/api/people"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; NSURLResponse *response = nil; NSError *myErr; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&myErr]; _objects = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myErr]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); // Ending main queue }); // End myQueue

Page 27: Objective-C for C# and Java Developers

Selectors•SEL type defines a method

signature

•-(void)setAction:(SEL)aSelector

•SEL mySelector;

•mySelector = @selector(drawMyView:);

•[myButton setAction:mySelector];

Page 28: Objective-C for C# and Java Developers

Objective-C Literals

•New in iOS 6 and Mac OS X 10.8

•NSArray *array = @[ @"Hello", NSApp, [NSNumber numberWithInt:42] ];

• NSDictionary *dictionary = @{@"name" : NSUserName(),@"date" : [NSDate date],@"processInfo" : [NSProcessInfo processInfo]};

Page 29: Objective-C for C# and Java Developers

Memory Management

•C used methods like malloc and free

•Obj-C uses object retain pool

•Garbage Collection on the Mac, but not on the iOS Devices

•Use ARC for future implementations

Page 30: Objective-C for C# and Java Developers

ARC

•Automatic Reference Counting

•Not Garbage Collection

•Compiler adds memory management into the compiled code

•Use Strong and Weak keywords to give hint to the compiler

Page 31: Objective-C for C# and Java Developers

MVC

•Model-View-Controller

•COCOA has Controller classes

•UIViewController Class

•Views are in the XIB (NIB) files or StoryBoards

Page 32: Objective-C for C# and Java Developers

Controllers

•iPhone Apps commonly have multiple views

•Controllers can Segue to each View

•Navigation Controller used to load different views

•UINavigationController

Page 33: Objective-C for C# and Java Developers

SDK Tools

•Xcode 4.5 IDE

•Interface Builder (Views)

•Instruments (Profiler tool)

•iPhone Simulator

Page 34: Objective-C for C# and Java Developers

Xcode 4.5

•LLVM, Clang and GCC compiler 4.2

•Support for Obj-C, C++, Python and Ruby (iPhone only uses Obj-C & C++)

•Code editor with code completion

•Support for GIT, SVN and CVS

Page 35: Objective-C for C# and Java Developers

Interface Builder•Tool for laying out interfaces

•Built into Xcode

•StoryBoards for Mobile

•Also works with older XIB and NIB files

•Bind Actions and Outlets in Controllers

Page 36: Objective-C for C# and Java Developers

Demo

Page 38: Objective-C for C# and Java Developers

Book Resources

•COCOA Programming For Mac OS X by Aaron Hillegass

•iPhone Developer’s Cookbook by Erica Sadun

•APress Books

Page 39: Objective-C for C# and Java Developers

Mono Touch

•Develop iPhone Apps with C#

•Xamarin IDE

•Ahead of time compilation

•Works with IB

Page 40: Objective-C for C# and Java Developers

PhoneGap

•HTML, CSS and JavaScript

•Compiles into an .app that can be uploaded to the app store

•Native vs. Run everywhere

•Also Sencha Touch

Page 41: Objective-C for C# and Java Developers

Contact

•davidfekke at gmail dot com

•twitter.com/davidfekke

•http://www.fekke.com/blog/

•Please come out to the JaxMUG

Page 42: Objective-C for C# and Java Developers

Questions?