1 / 42

Objective-C for C# and Java Developers

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.

ponce
Download Presentation

Objective-C for C# and Java Developers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objective-C for C# and Java Developers • JaxCodeCamp 2012

  2. Who am I • David Fekke • .NET Developer by day, iOS by night • Presenter JaxMUG, JaxDug, JaxJug, JSSUG • Mac User 1986

  3. JaxMUG Meet once a month at the Southside Library jaxmug.com

  4. Jesse Eisenberg

  5. The Apple Way

  6. Assumptions • Somewhat familiar with OO concepts • Used a language like Java, C# or C++ • Open minded • Actually used an iPhone, iPad or iPod Touch

  7. Requirements • You need a Intel Mac with Mac OS X • Some knowledge of OO concepts • Xcode tools (FREE) • iOS (SDK)

  8. iOS Platform • ARM Processor • 128/256/512/1024 MB RAM • BSD UNIX • Mach Microkernel • COCOA APIs

  9. COCOA • COCOA is a OO Framework • Based on NextStep • Mostly written in Objective-C • iOS Devices use COCOA Touch

  10. COCOA Framework • NS (NextStep) • CF (Core Foundation) • CA (Core Animation) • CI (Core Image) • Core Data • OpenGL ES

  11. 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

  12. Tiobe Programming Index

  13. 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

  14. -(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; • } • }

  15. Obj-C vs C#

  16. 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

  17. #import <UIKit/UIKit.h> @interface LottoRandomAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; } @property (nonatomic, retain) IBOutletUIWindow *window; @end

  18. #import "LottoRandomAppDelegate.h" @implementation LottoRandomAppDelegate @synthesize window; - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [windowmakeKeyAndVisible]; } - (void)dealloc { [superdealloc]; } @end

  19. Properties • Objective-C 2.0 • @property (strong) NSString *myStr; • @synthesize myStr = _myStr; • No longer need @synthesize keyword in iOS 6.0

  20. Property Getters and Setters @synthesize myStr = _myStr; -(NSString *)myStr { return_myStr; } -(void)setMyStr:(NSString *)aMyStr { _myStr = aMyStr; }

  21. @interface extension @interfaceMasterViewController () { NSMutableArray*_objects; } @end

  22. Categories • Similar to method extensions and partial classes • You can add methods to classes out side of the implementation file

  23. @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

  24. 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

  25. 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

  26. Selectors • SEL type defines a method signature • -(void)setAction:(SEL)aSelector • SEL mySelector; • mySelector = @selector(drawMyView:); • [myButton setAction:mySelector];

  27. 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]};

  28. 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

  29. 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

  30. MVC • Model-View-Controller • COCOA has Controller classes • UIViewController Class • Views are in the XIB (NIB) files or StoryBoards

  31. Controllers • iPhone Apps commonly have multiple views • Controllers can Segue to each View • Navigation Controller used to load different views • UINavigationController

  32. SDK Tools • Xcode 4.5 IDE • Interface Builder (Views) • Instruments (Profiler tool) • iPhone Simulator

  33. 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

  34. 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

  35. Demo

  36. 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/

  37. Book Resources • COCOA Programming For Mac OS X by Aaron Hillegass • iPhone Developer’s Cookbook by Erica Sadun • APress Books

  38. Mono Touch • Develop iPhone Apps with C# • Xamarin IDE • Ahead of time compilation • Works with IB

  39. PhoneGap • HTML, CSS and JavaScript • Compiles into an .app that can be uploaded to the app store • Native vs. Run everywhere • Also Sencha Touch

  40. Contact • davidfekke at gmail dot com • twitter.com/davidfekke • http://www.fekke.com/blog/ • Please come out to the JaxMUG

  41. Questions?

More Related