Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS Development for Java Developers

iOS Development for Java Developers

Introduction to iOS Development and Objective-C, slated towards Java programmers. Provides some examples of common constructs in Java syntax and their Objective-C counterparts.
Shows some "weird" language and platform features that are foreign to most people primarily living and thinking in Java.

Sample application on GitHub at https://github.com/dschneller/CodingSerbia as a coding demo, showing some of the features presented and demoing the use of table view controllers and the MVC pattern in general.

Daniel Schneller

October 17, 2013
Tweet

More Decks by Daniel Schneller

Other Decks in Programming

Transcript

  1. Mapping$Java$to$ObjC Java Objective C int x = 5; int x

    = 5; boolean flag = true; boolean flag = false; BOOL flag = YES; // or != 0 BOOL flag = NO; // or == 0 Object o = null; id o = nil; map.put(“key”, “value”); dict[@”key”] = @”value”
  2. Mapping$Java$to$ObjC Java Objective C class Child extends Parent @interface Child

    : Parent @implementation Child interface TableViewDelegate @protocol TableViewDelegate class C implements I @interface C <I> @implementation C abstract class AbstractClass only by convention
  3. Mapping$Java$to$ObjC Java Objective C package com.codingserbia class MyClass CDSMyClass void

    instanceMethod() - (void) instanceMethod static int staticMethod() + (int) classMethod
  4. Mapping$Java$to$ObjC Java Objective C String s = “Hello, World” NSString*

    s = @”Hello, World” s = s.toUpperCase(); s = [s upperCaseString]; Object o = this.a().b(3).c(true) id o = [[[self a] b:3] c:YES] MyClass m = new MyClass(); MyClass* m = [[MyClass alloc] init]; c = new Captain(“Kirk”, “Jim”, 7); c = [Captain alloc] initWithLastname:@”Kirk” firstName:@”Jim” playedInMovies:7];
  5. No$NullPointerExcepDons String firstLowerCaseString(List x) { if (x == null) {

    return null; } else if (x.size() == 0) { return null; } else { Object o = x.get(0); if (x == null) { return null; } return x.toString().toLowerCase(); } }
  6. OpDonal$protocol$methods •Great$for$delegate/callback$based$APIs •Callers$must$check$at$runDme,$if$they$are$ implemented$on$the$called$object •Used$pervasively$throughout$frameworks @protocol UITableViewDataSource<NSObject> @optional - (NSString

    *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section; - (NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section; @end
  7. Categories •Extend$classes$at$runDme •New$methods$even$on$framework$classes •Great$for$decoupling @implementation NSNumber (CDSHumanReadable) - (NSString*)humanReadableFilesize {

    NSByteCountFormatter* formatter = [[NSByteCountFormatter alloc] init]; return [formatter stringFromByteCount:[self longLongValue]]; } @end
  8. Categories •Extend$classes$at$runDme •New$methods$even$on$framework$classes •Great$for$decoupling @implementation NSNumber (CDSHumanReadable) - (NSString*)humanReadableFilesize {

    NSByteCountFormatter* formatter = [[NSByteCountFormatter alloc] init]; return [formatter stringFromByteCount:[self longLongValue]]; } @end @interface NSDictionary (RKAdditions) - (NSString *)stringWithURLEncodedEntries; @end
  9. Method$‘swizzling’ •Give$all$views$a$border$for$debugging$by$ amending$UIView$constructors$appEwide @implementation UIView (CDSBorder) - (id)swzld_initWithFrame:(CGRect)frame { //

    Call original implementation id result = [self swzld_initWithFrame:frame]; if ([result respondsToSelector:@selector(layer)]) { // Get layer for this view. CALayer *layer = [result layer]; // Set border on layer. layer.borderWidth = 2; layer.borderColor = [[UIColor redColor] CGColor]; } // Return the modified view. return result; } + (void)load { Method original, swizzle; // Get the "- (id)initWithFrame:" method. original = class_getInstanceMethod(self, @selector(initWithFrame:)); // Get the "- (id)swzld_initWithFrame:" method. swizzle = class_getInstanceMethod(self, @selector(swzld_initWithFrame:)); // Swap their implementations. method_exchangeImplementations(original, swizzle); }