420 likes | 702 Views
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.
E N D
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
Assumptions • Somewhat familiar with OO concepts • Used a language like Java, C# or C++ • Open minded • Actually used an iPhone, iPad or iPod Touch
Requirements • You need a Intel Mac with Mac OS X • Some knowledge of OO concepts • Xcode tools (FREE) • iOS (SDK)
iOS Platform • ARM Processor • 128/256/512/1024 MB RAM • BSD UNIX • Mach Microkernel • COCOA APIs
COCOA • COCOA is a OO Framework • Based on NextStep • Mostly written in Objective-C • iOS Devices use COCOA Touch
COCOA Framework • NS (NextStep) • CF (Core Foundation) • CA (Core Animation) • CI (Core Image) • Core Data • OpenGL ES
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
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
-(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; • } • }
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
#import <UIKit/UIKit.h> @interface LottoRandomAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; } @property (nonatomic, retain) IBOutletUIWindow *window; @end
#import "LottoRandomAppDelegate.h" @implementation LottoRandomAppDelegate @synthesize window; - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [windowmakeKeyAndVisible]; } - (void)dealloc { [superdealloc]; } @end
Properties • Objective-C 2.0 • @property (strong) NSString *myStr; • @synthesize myStr = _myStr; • No longer need @synthesize keyword in iOS 6.0
Property Getters and Setters @synthesize myStr = _myStr; -(NSString *)myStr { return_myStr; } -(void)setMyStr:(NSString *)aMyStr { _myStr = aMyStr; }
@interface extension @interfaceMasterViewController () { NSMutableArray*_objects; } @end
Categories • Similar to method extensions and partial classes • You can add methods to classes out side of the implementation file
@interfaceNSString(reverse) -(NSString*) reverseString; @end @implementationNSString(reverse) -(NSString*) reverseString { NSMutableString*reversedStr; intlen = [self length]; // Auto released string reversedStr =[NSMutableStringstringWithCapacity:len]; // Probably woefully inefficient... while(len >0) [reversedStr appendString:[NSStringstringWithFormat:@"%C", [self characterAtIndex:--len]]]; returnreversedStr; } @end
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
dispatch_queue_t myQueue = dispatch_queue_create("get people list", NULL); dispatch_async(myQueue, ^{ NSURL *myURL = [NSURLURLWithString:@"http://192.168.1.70/APISamples/api/people"]; NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:myURL]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json"forHTTPHeaderField:@"Accept"]; NSURLResponse *response = nil; NSError *myErr; NSData *data = [NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:&myErr]; _objects = [NSJSONSerializationJSONObjectWithData:data options:kNilOptionserror:&myErr]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableViewreloadData]; }); // Ending main queue }); // End myQueue
Selectors • SEL type defines a method signature • -(void)setAction:(SEL)aSelector • SEL mySelector; • mySelector = @selector(drawMyView:); • [myButton setAction:mySelector];
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]};
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
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
MVC • Model-View-Controller • COCOA has Controller classes • UIViewController Class • Views are in the XIB (NIB) files or StoryBoards
Controllers • iPhone Apps commonly have multiple views • Controllers can Segue to each View • Navigation Controller used to load different views • UINavigationController
SDK Tools • Xcode 4.5 IDE • Interface Builder (Views) • Instruments (Profiler tool) • iPhone Simulator
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
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
Internet Resources • http://developer.apple.com • http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/ • http://www.stanford.edu/class/cs193p/cgi-bin/index.php • http://www.cocoadev.com/
Book Resources • COCOA Programming For Mac OS X by Aaron Hillegass • iPhone Developer’s Cookbook by Erica Sadun • APress Books
Mono Touch • Develop iPhone Apps with C# • Xamarin IDE • Ahead of time compilation • Works with IB
PhoneGap • HTML, CSS and JavaScript • Compiles into an .app that can be uploaded to the app store • Native vs. Run everywhere • Also Sencha Touch
Contact • davidfekke at gmail dot com • twitter.com/davidfekke • http://www.fekke.com/blog/ • Please come out to the JaxMUG