Upload
ponce
View
64
Download
0
Tags:
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
Objective-C for C# and Java
DevelopersJaxCodeCamp 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
QuickTime™ and a decompressor
are needed to see this picture.
The Apple Way
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 Touch
Media
Core Services
Core OS
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
Tiobe Programming Index
Position Language
1 C
2 Java
3 Objective-C
4 C++
5 C#
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;
}}
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
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) IBOutlet UIWindow *window;
@end
#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
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
@interface MasterViewController () { NSMutableArray *_objects;}@end
Categories
•Similar to method extensions and partial classes
•You can add methods to classes out side of the implementation file
@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
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 = [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
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
Demo
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
Questions?