SlideShare ist ein Scribd-Unternehmen logo
1 von 76
Downloaden Sie, um offline zu lesen
Objective-c
FOR

JAVA
DEVELOPERS
1The basics
 OBjective-c For Java developers
System.out.println("Hello, World!");   NSLog(@"Hello, World!");




1Hello world
byte aByte = 0;                    char aByte = 0;
short aShort = 0;                  short aShort = 0;
int anInt = 0;                     int anInt = 0;
long aLong = 0;                    long aLong = 0;
float aFloat = 0;                  float aFloat;
double aDouble = 0;                double aDouble;
boolean aBoolean = true; //false   BOOL aBoolean = YES; //NO
char aChar = 'a';                  char aChar = 'a';




1Basic types
Object anObject = new Object();    NSObject *anObject = [[NSObject alloc] init];
NSString *aString = [[NSString alloc]
String aString = new String(chars);
                                                         initWithUTF8String:chars];




1Methods
NSString *aString = [[NSString alloc]
String aString = new String(chars);
                                                         initWithUTF8String:chars];




1Methods
NSString *aString = [[NSString alloc]
String aString = new String(chars);
                                                         initWithUTF8String:chars];




1Methods
NSString *aString = [[NSString alloc]
String aString = new String(chars);
                                                         initWithUTF8String:chars];




1Methods
NSString *aString = [[NSString alloc]
String aString = new String(chars);
                                                         initWithUTF8String:chars];




1Methods
Recap
 the
basics
 are
 the
same!
Recap
 the
basics
 are
 the     also, YAY

same!       for
         unsigned
2Meet the objects
 OBjective-c For Java developers
String s = new String();   NSString *s = [[NSString alloc] init];




2Pointers
String s = new String();   NSString *s = [[NSString alloc] init];



 0x3DE2FE                                 Stuff



2Pointers
String s = new String();   NSString *s = [[NSString alloc] init];



 0x3DE2FE                                 Stuff



2Pointers
String s = new String();   NSString *s = [[NSString alloc] init];



 0x3DE2FE                                 Stuff



2Pointers
String s = new String();   NSString *s = [[NSString alloc] init];



 0x3DE2FE
    *                                  Object
                                        Stuff



2Pointers
String s = new String();   NSString *s = [[NSString alloc] init];



 0x3DE2FE                                 Stuff



2Pointers
"Many of those strange primitive wrapper classes, like Integer and
Number came from Lee Boynton, one of the early NeXT Obj-C class library
guys who hated 'int' and 'float' types."


Patrick Naughton (one of the original creators of Java)




2NSWhat?
NSString *aString = [[NSString alloc]
String aString = new String("A string.");
                                                     initWithString:@"A string."];




2String
NSString *aString = [[NSString alloc]
String aString = new String("A string.");
                                                     initWithString:@"A string."];




2String
NSString *aString = [[NSString alloc]
String aString = new String("A string.");
                                                     initWithString:@"A string."];

      String aString = "A string.";           NSString *aString = @"A string.";




2String
NSString *one = @"1 +";
String one = "1 + ";
                                         int two = 2;
int two = 2;
                                         NSString *eq = @"=";
String equals = " = ";
                                         float three = 3.0;
float three = 3.0f;
                                         NSString *s = [NSString stringWithFormat:@"%@
String s = one + two + equals + three;
                                         %d %@ %.1f", one, two, eq, three];




2“A” + “B”
NSString *one = @"1 +";
String one = "1 + ";
                                         int two = 2;
int two = 2;
                                         NSString *eq = @"=";
String equals = " = ";
                                         float three = 3.0;
float three = 3.0f;
                                         NSString *s = [NSString stringWithFormat:@"%@
String s = one + two + equals + three;
                                         %d %@ %.1f", one, two, eq, three];




2“A” + “B”
NSString *one = @"1 +";
String one = "1 + ";
                                         int two = 2;
int two = 2;
                                         NSString *eq = @"=";
String equals = " = ";
                                         float three = 3.0;
float three = 3.0f;
                                         NSString *s = [NSString stringWithFormat:@"%@
String s = one + two + equals + three;
                                         %d %@ %.1f", one, two, eq, three];




2“A” + “B”
NSString *one = @"1 +";
String one = "1 + ";
                                         int two = 2;
int two = 2;
                                         NSString *eq = @"=";
String equals = " = ";
                                         float three = 3.0;
float three = 3.0f;
                                         NSString *s = [NSString stringWithFormat:@"%@
String s = one + two + equals + three;
                                         %d %@ %.1f", one, two, eq, three];




2“A” + “B”
NSString *one = @"1 +";
String one = "1 + ";
                                         int two = 2;
int two = 2;
                                         NSString *eq = @"=";
String equals = " = ";
                                         float three = 3.0;
float three = 3.0f;
                                         NSString *s = [NSString stringWithFormat:@"%@
String s = one + two + equals + three;
                                         %d %@ %.1f", one, two, eq, three];




2“A” + “B”
NSString *one = @"1 +";
String one = "1 + ";
                                         int two = 2;
int two = 2;
                                         NSString *eq = @"=";
String equals = " = ";
                                         float three = 3.0;
float three = 3.0f;
                                         NSString *s = [NSString stringWithFormat:@"%@
String s = one + two + equals + three;
                                         %d %@ %.1f", one, two, eq, three];




2“A” + “B”
But... If I can useNSStringthen I +";
                                                  C++ *one = @"1
String one = "1 + ";
int two = 2;                can use operator overloading
                                                 int two = 2;
                                                 NSString *eq = @"=";
String equals = " = ";
float three = 3.0f;
                              to add that to Objective-C,
                                                 float three = 3.0;
                                                 NSString *s = [NSString stringWithFormat:@"%@
String s = one + two +   equals + three;   right?%d %@ %.1f", one, two, eq, three];




2“A” + “B”
But... If I can useNSStringthen I +";
                                                  C++ *one = @"1
String one = "1 + ";
int two = 2;                can use operator overloading
                                                 int two = 2;
                                                 NSString *eq = @"=";
String equals = " = ";
float three = 3.0f;
                              to add that to Objective-C,
                                                 float three = 3.0;
                                                 NSString *s = [NSString stringWithFormat:@"%@
String s = one + two +   equals + three;   right?%d %@ %.1f", one, two, eq, three];

                                           Nop :(




2“A” + “B”
String strings[] = {"1", "2"};   NSArray *strings = @[@"1", @"2"];




2(NS)Array
NSString **strings =
String strings[] = new String[10];
                                                malloc(10*sizeof(NSString *));




2(NS)Array
List<String> strings =                    NSMutableArray *strings =
             new ArrayList<String>(10);          [NSMutableArray arrayWithCapacity:10];




2(NS)Array
Map<Object, Object> m = new
                    HashMap<Object,Object>();   NSDictionary *m = @{@"key" : @"value"};
m.put("key","value");




2Dictionary
Map<Object, Object> m = new                     NSMutableDictionary *m =
                    HashMap<Object,Object>();             [@{@"key" : @"value"} mutableCopy];
m.put("key","value");                           [m setObject:@"key2" forKey:@"value2"];




2Dictionary
Map<Object, Object> m = new                     NSMutableDictionary *m =
                    HashMap<Object,Object>();             [@{@"key" : @"value"} mutableCopy];
m.put("key","value");                           [m setObject:@"key2" forKey:@"value2"];




2Dictionary
Map<Object, Object> m = new                     NSMutableDictionary *m =
                    HashMap<Object,Object>();             [@{@"key" : @"value"} mutableCopy];
m.put("key","value");                           [m setObject:@"key2" forKey:@"value2"];




2Dictionary
NSString *s = @"value1";
String s = "value1";                             NSNumber *n = @3.14159265359;
Number n = 3.14159265359;                        NSArray *a = @[@"value1", @"value2"];
String a[] = {"value1","value2"};                NSDictionary *m = @{@"key" : @"value"};
Map<String,String> m = new                       NSArray *arr = @[ @(roundf(2*M_PI)) ];
                     HashMap<String,String>();
m.put("key","value");                            NSMutableDictionary *dict =
Number arr[] = { Math.round(2*Math.PI) };                    [NSMutableDictionary dictionary];




2Literals
                                                 dict[@"key1"] = @"value1";
POINTER
                 Recap

    =
 OBJECT
ALLOC       CREATE
 INIT    INITIALIZE
   LITERALS ARE
     AWESOME
POINTER
                  Recap

    =
 OBJECT
ALLOC       CREATE
 INIT    INITIALIZE
   LITERALS ARE   STRING
                  CONCAT
     AWESOME       SUCKS
OBjective-c For Java developers

3implementations
 Interfaces &
.h                                                             .m
                                    @implementation Foo {
 @interface Foo : NSObject {
                                    //Private ivars
 //protected/private/public ivars
                                    }
 }
                                    //Synthesize properties
 //Property declarations
                                    //Method implementations
 //Method declarations
                                    //Private methods




 3.h & .m
 @end
                                    @end
.h                                                                          .m
                                      @implementation Foo {
                                         NSString *_bar;
                                      }
                                      - (id)initWithBar:(NSString *)bar {
 @interface Foo : NSObject               self = [super init];
 - (id)initWithBar:(NSString *)bar;       if (self != nil) {
 @end                                       _bar = [bar copy];
                                          }
                                         return self;




 3Foo class
                                      }
                                      @end
.h                                                                                       .m
                                                   @implementation Foo {
                                                      NSString *_bar;
                                                   }
                                                   - (id)initWithBar:(NSString *)bar {
 @interface Foo : NSObject    nil?    Don’t you   mean null? init];
                                                      self = [super
 - (id)initWithBar:(NSString *)bar;                    if (self != nil) {
 @end                                                    _bar = [bar copy];
                                                       }
                                                      return self;




 3Foo class
                                                   }
                                                   @end
.h                                                                                       .m
                                                   @implementation Foo {
                                                      NSString *_bar;
                                                   }
                                                   - (id)initWithBar:(NSString *)bar {
 @interface Foo : NSObject    nil?    Don’t you   mean null? init];
                                                      self = [super
 - (id)initWithBar:(NSString *)bar;                    if (self != nil) {
 @end
                                           Nop         }
                                                         _bar = [bar copy];

                                                      return self;




 3Foo class
                                                   }
                                                   @end
.h                                                                                  .m
                                              @implementation Foo {
                                                 NSString *_bar;
                                              }
                                              - (id)initWithBar:(NSString *)bar {
 @interface Foo : NSObject    nil? Don’t you mean null? init];
                                                 self = [super
 - (id)initWithBar:(NSString *)bar;               if (self != nil) {
 @end
                                         Nop      }
                                                    _bar = [bar copy];

                                                 return self;
                             self? Don’t you mean this?




 3Foo class
                                              }
                                              @end
.h                                                                                  .m
                                              @implementation Foo {
                                                 NSString *_bar;
                                              }
                                              - (id)initWithBar:(NSString *)bar {
 @interface Foo : NSObject    nil? Don’t you mean null? init];
                                                 self = [super
 - (id)initWithBar:(NSString *)bar;               if (self != nil) {
 @end
                                         Nop      }
                                                    _bar = [bar copy];

                                                 return self;
                             self? Don’t you mean this?




 3Foo class
                                              }

                                         Nop
                                              @end
.h                                                                          .m
                                      @implementation Foo {
                                         NSString *_bar;
                                      }
                                      - (id)initWithBar:(NSString *)bar {
 @interface Foo : NSObject               self = [super init];
 - (id)initWithBar:(NSString *)bar;       if (self != nil) {
 @end                                       _bar = [bar copy];
                                          }
                                         return self;




 3Foo class
                                      }
                                      @end
.h                                                                      .m
                                    @implementation Foo

 @interface Foo : NSObject          - (int)sumA:(int)a withB:(int)b {
                                       return a+b;
 - (int)sumA:(int)a withB:(int)b;   }
 + (Foo *)aFoo;
                                    + (Foo *)aFoo {
 @end                                  return [[Foo alloc] init];
                                    }




 3+ & -
                                    @end
.h
class Foo {
                                       @interface Foo : NSObject
    private String bar;
                                       @property (nonatomic, copy) NSString *bar;
                                       @end
    public String getBar() {
        return bar;
    }                                                                          .m
    public void setBar(String bar) {
                                       @implementation Foo
        this.bar = bar;
                                       @synthesize bar;
    }




3Properties
                                       @end
}
"I'm pretty sure that Java's 'interface' is a direct rip-off of Obj-C's 'protocol'
which was largely designed by these ex-NeXT'ers..."


Patrick Naughton (one of the original creators of Java)




3NSWhat?
@protocol Color <NSObject>

                             - (int)red;
interface Color {
                             - (int)green;
    public int getRed();
                             - (int)blue;
    public int getBlue();
    public int getGreen();
                             @opcional
}
                             - (int)rgb;




3Protocols
                             @end
.h                                                                .m

                             @implementation NSString (MD5)
 @interface NSString (MD5)
                             - (NSString *)md5Hash {
                               NSString *md5 = anMD5Func(self);
 - (NSString *)md5Hash;
                               return md5;
                             }
 @end




 3Categories
                             @end
Recap
    Interface               .H
IMPLEMENTATION              .M
    Interfaces are called
    protocols and there’s
     no abstract classes
      PROPERTIES
         ARE
       AWESOME
Recap
    Interface            .H
IMPLEMENTATION           .M
    Interfaces are called
    protocols and there’s   And
     no abstract classes imagine
      PROPERTIES          what you
                           can do
         ARE                with
       AWESOME          categories
4remember c?
 OBjective-c For Java developers
Foo.m                                                              main.m
 @implementation Foo
                            int main(int argc, char *argv[]) {
 - (int)anAddress {
                              @autoreleasepool {
   char *bar = malloc(1);
                                 Foo *foo = [[Foo alloc] init];
   free(bar);
                                 printf("0x%X",[foo anAddress]);
   return &bar;
                              }
 }
                            }




 4NOOO!
 @end
"Objective-C is a hybrid programming language[…]formed by grafting the
Smalltalk-80 style of object-oriented programming onto a C language
rootstock. Objective-C adds precisely one new data type, the
object[...]and precisely one new operation, the message expression. "




4Why C?
Cox, Brad J. (1991). Object Oriented Programming: An Evolutionary Approach
objc.h                                                     runtime.h


 typedef struct objc_object {
     Class isa;                      struct objc_class {
 } *id;                                  Class isa;
                                     };
 typedef struct objc_class *Class;




 4The Object
Core Foundation is a library with a set of programming interfaces
conceptually derived from the Objective-C-based Foundation framework
but implemented in the C language. To do this, Core Foundation
implements a limited object model in C. Core Foundation defines opaque
types that encapsulate data and functions, hereafter referred to as




4CF
“objects.”
Recap
  C Is at the
  bottom of
 objective-c
   and YOU
 SHOULD TAKE
ADVANTAGE OF
       IT
Recap
  C Is at the
  bottom of
 objective-c BUT C
   and YOU    DOES NOT
              HAVE ARC
 SHOULD TAKE SO YOU
              ARE STUCK
ADVANTAGE OF WITH
       IT      MALLOC
              AND FREE
5BLOCKS
 OBjective-c For Java developers
//create the array
NSArray *arr = @[ @16, @11, @88 ];

//sort it using a block
NSArray *sortedArr = [arr sortedArrayUsingComparator:^(id obj1, id obj2) {
 return [((NSNumber *)obj1) compare:(NSNumber *)obj2];
}];

//print




5Closures
NSLog(@"%@",sortedArr);
//save a block in a variable
int (^aBlock)(int, int) = ^(int i, int j) { return i * j; };

//execute it
int result = aBlock(1,2);




5Keep it
//get the block as a parameter
- (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock {
    return aBlock(i,j);
}




5Get it
“If you are using ARC, object variables are retained and released automatically as the block is copied and later
released.”


“If you use a block within the implementation of a method [...] If you access an instance variable by reference,
self is retained; If you access an instance variable by value, the variable is retained.”


“When you copy a stack-based block, you get a new block. If you copy a heap-based block, however, you
simply increment the retain count of that block and get it back as the returned value of the copy function or




5Memory
method.”



                Blocks &
Recap
 BLOCKS Is MY
   FAVORITE
 OBJECTIVE-C
LIBRARY AND IT
WILL BE ONE OF
  YOURS TOO
Recap
 BLOCKS Is MY
   FAVORITE
 OBJECTIVE-C
LIBRARY AND IT The
WILL BE ONE OF notation
              is hard to
  YOURS TOO     MASTER
                 though
6Other stuff
 OBjective-c For Java developers
Localizable.strings


                                               ...
                                               "About" = "Sobre";
NSString *localizedAbout =
                                               "Open" = "Abrir";
           NSLocalizedString(@"About", nil);
                                               "Close" = "Fechar";
                                               ...




6クール!
- (void)insertSublayer:(CALayer *)layer below:(CALayer *)sibling;
- (void)insertSublayer:(CALayer *)layer above:(CALayer *)sibling;

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:
                                                     (BOOL)startImmediately NS_AVAILABLE(10_5, 2_0);




6Read it
Recap

this is just the
  beginning.
 There’s a lot
    more to
 objective-c
Recap

this is just the
  beginning.
 There’s a lot
    more to
                 But now
 objective-c it won’t
                  scare
                  you !
7DEMO
 OBjective-c For Java developers
Thanks
                       for
                   listening
I’m @fbbernardo on twitter and fbernardo on github
      Thanks to @hugojsfranca for the awesome design suggestions

Weitere ähnliche Inhalte

Was ist angesagt?

python-cheat-sheet-v1
python-cheat-sheet-v1python-cheat-sheet-v1
python-cheat-sheet-v1
Hiroshi Ono
 
Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2
Hariz Mustafa
 

Was ist angesagt? (20)

Functional es6
Functional es6Functional es6
Functional es6
 
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle TreesModern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
 
Python Day1
Python Day1Python Day1
Python Day1
 
Python data structures
Python data structuresPython data structures
Python data structures
 
python-cheat-sheet-v1
python-cheat-sheet-v1python-cheat-sheet-v1
python-cheat-sheet-v1
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
The Ring programming language version 1.3 book - Part 14 of 88
The Ring programming language version 1.3 book - Part 14 of 88The Ring programming language version 1.3 book - Part 14 of 88
The Ring programming language version 1.3 book - Part 14 of 88
 
Python Regular Expressions
Python Regular ExpressionsPython Regular Expressions
Python Regular Expressions
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리
 
webScrapingFunctions
webScrapingFunctionswebScrapingFunctions
webScrapingFunctions
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
집단지성 프로그래밍 08-가격모델링
집단지성 프로그래밍 08-가격모델링집단지성 프로그래밍 08-가격모델링
집단지성 프로그래밍 08-가격모델링
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 
Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2
 
An Exploration of the Formal Properties of PromQL
An Exploration of the Formal Properties of PromQLAn Exploration of the Formal Properties of PromQL
An Exploration of the Formal Properties of PromQL
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Pytho dictionaries
Pytho dictionaries Pytho dictionaries
Pytho dictionaries
 
Studyx4
Studyx4Studyx4
Studyx4
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 

Andere mochten auch

Tuning Android Applications (Part One)
Tuning Android Applications (Part One)Tuning Android Applications (Part One)
Tuning Android Applications (Part One)
CommonsWare
 
Tuning android for low ram devices
Tuning android for low ram devicesTuning android for low ram devices
Tuning android for low ram devices
Droidcon Berlin
 
Android Performance Best Practices
Android Performance Best Practices Android Performance Best Practices
Android Performance Best Practices
Amgad Muhammad
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
Carol McDonald
 

Andere mochten auch (20)

Deep Parameters Tuning for Android Mobile Apps
Deep Parameters Tuning for Android Mobile AppsDeep Parameters Tuning for Android Mobile Apps
Deep Parameters Tuning for Android Mobile Apps
 
Tuning Android Applications (Part One)
Tuning Android Applications (Part One)Tuning Android Applications (Part One)
Tuning Android Applications (Part One)
 
Android performance tuning. Memory.
Android performance tuning. Memory.Android performance tuning. Memory.
Android performance tuning. Memory.
 
iOS Developer Overview - DevWeek 2014
iOS Developer Overview - DevWeek 2014iOS Developer Overview - DevWeek 2014
iOS Developer Overview - DevWeek 2014
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 
Introduction to ART (Android Runtime)
Introduction to ART (Android Runtime)Introduction to ART (Android Runtime)
Introduction to ART (Android Runtime)
 
iOS Application Testing
iOS Application TestingiOS Application Testing
iOS Application Testing
 
Performance optimization for Android
Performance optimization for AndroidPerformance optimization for Android
Performance optimization for Android
 
LAS16-201: ART JIT in Android N
LAS16-201: ART JIT in Android NLAS16-201: ART JIT in Android N
LAS16-201: ART JIT in Android N
 
Tuning android for low ram devices
Tuning android for low ram devicesTuning android for low ram devices
Tuning android for low ram devices
 
Android Performance Best Practices
Android Performance Best Practices Android Performance Best Practices
Android Performance Best Practices
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
Jvm Performance Tunning
Jvm Performance TunningJvm Performance Tunning
Jvm Performance Tunning
 
Layer architecture of ios (1)
Layer architecture of ios (1)Layer architecture of ios (1)
Layer architecture of ios (1)
 
Building iOS App Project & Architecture
Building iOS App Project & ArchitectureBuilding iOS App Project & Architecture
Building iOS App Project & Architecture
 
MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 Tips
 
Google ART (Android RunTime)
Google ART (Android RunTime)Google ART (Android RunTime)
Google ART (Android RunTime)
 
Designing better user interfaces
Designing better user interfacesDesigning better user interfaces
Designing better user interfaces
 
Android & iOS Automation Using Appium
Android & iOS Automation Using AppiumAndroid & iOS Automation Using Appium
Android & iOS Automation Using Appium
 

Ähnlich wie Objective-C for Java developers

Training Java - Lesson3
Training Java - Lesson3Training Java - Lesson3
Training Java - Lesson3
mittoq
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
Vince Vo
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan Samoon
 

Ähnlich wie Objective-C for Java developers (20)

Training Java - Lesson3
Training Java - Lesson3Training Java - Lesson3
Training Java - Lesson3
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operations
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
Team 1
Team 1Team 1
Team 1
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processing
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
02 arrays
02 arrays02 arrays
02 arrays
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Struktur data 1
Struktur data 1Struktur data 1
Struktur data 1
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Objective-C for Java developers

  • 2. 1The basics OBjective-c For Java developers
  • 3. System.out.println("Hello, World!"); NSLog(@"Hello, World!"); 1Hello world
  • 4. byte aByte = 0; char aByte = 0; short aShort = 0; short aShort = 0; int anInt = 0; int anInt = 0; long aLong = 0; long aLong = 0; float aFloat = 0; float aFloat; double aDouble = 0; double aDouble; boolean aBoolean = true; //false BOOL aBoolean = YES; //NO char aChar = 'a'; char aChar = 'a'; 1Basic types Object anObject = new Object(); NSObject *anObject = [[NSObject alloc] init];
  • 5. NSString *aString = [[NSString alloc] String aString = new String(chars); initWithUTF8String:chars]; 1Methods
  • 6. NSString *aString = [[NSString alloc] String aString = new String(chars); initWithUTF8String:chars]; 1Methods
  • 7. NSString *aString = [[NSString alloc] String aString = new String(chars); initWithUTF8String:chars]; 1Methods
  • 8. NSString *aString = [[NSString alloc] String aString = new String(chars); initWithUTF8String:chars]; 1Methods
  • 9. NSString *aString = [[NSString alloc] String aString = new String(chars); initWithUTF8String:chars]; 1Methods
  • 10. Recap the basics are the same!
  • 11. Recap the basics are the also, YAY same! for unsigned
  • 12. 2Meet the objects OBjective-c For Java developers
  • 13. String s = new String(); NSString *s = [[NSString alloc] init]; 2Pointers
  • 14. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff 2Pointers
  • 15. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff 2Pointers
  • 16. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff 2Pointers
  • 17. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE * Object Stuff 2Pointers
  • 18. String s = new String(); NSString *s = [[NSString alloc] init]; 0x3DE2FE Stuff 2Pointers
  • 19. "Many of those strange primitive wrapper classes, like Integer and Number came from Lee Boynton, one of the early NeXT Obj-C class library guys who hated 'int' and 'float' types." Patrick Naughton (one of the original creators of Java) 2NSWhat?
  • 20. NSString *aString = [[NSString alloc] String aString = new String("A string."); initWithString:@"A string."]; 2String
  • 21. NSString *aString = [[NSString alloc] String aString = new String("A string."); initWithString:@"A string."]; 2String
  • 22. NSString *aString = [[NSString alloc] String aString = new String("A string."); initWithString:@"A string."]; String aString = "A string."; NSString *aString = @"A string."; 2String
  • 23. NSString *one = @"1 +"; String one = "1 + "; int two = 2; int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0; float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 24. NSString *one = @"1 +"; String one = "1 + "; int two = 2; int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0; float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 25. NSString *one = @"1 +"; String one = "1 + "; int two = 2; int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0; float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 26. NSString *one = @"1 +"; String one = "1 + "; int two = 2; int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0; float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 27. NSString *one = @"1 +"; String one = "1 + "; int two = 2; int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0; float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 28. NSString *one = @"1 +"; String one = "1 + "; int two = 2; int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0; float three = 3.0f; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; %d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 29. But... If I can useNSStringthen I +"; C++ *one = @"1 String one = "1 + "; int two = 2; can use operator overloading int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0f; to add that to Objective-C, float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; right?%d %@ %.1f", one, two, eq, three]; 2“A” + “B”
  • 30. But... If I can useNSStringthen I +"; C++ *one = @"1 String one = "1 + "; int two = 2; can use operator overloading int two = 2; NSString *eq = @"="; String equals = " = "; float three = 3.0f; to add that to Objective-C, float three = 3.0; NSString *s = [NSString stringWithFormat:@"%@ String s = one + two + equals + three; right?%d %@ %.1f", one, two, eq, three]; Nop :( 2“A” + “B”
  • 31. String strings[] = {"1", "2"}; NSArray *strings = @[@"1", @"2"]; 2(NS)Array
  • 32. NSString **strings = String strings[] = new String[10]; malloc(10*sizeof(NSString *)); 2(NS)Array
  • 33. List<String> strings = NSMutableArray *strings = new ArrayList<String>(10); [NSMutableArray arrayWithCapacity:10]; 2(NS)Array
  • 34. Map<Object, Object> m = new HashMap<Object,Object>(); NSDictionary *m = @{@"key" : @"value"}; m.put("key","value"); 2Dictionary
  • 35. Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy]; m.put("key","value"); [m setObject:@"key2" forKey:@"value2"]; 2Dictionary
  • 36. Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy]; m.put("key","value"); [m setObject:@"key2" forKey:@"value2"]; 2Dictionary
  • 37. Map<Object, Object> m = new NSMutableDictionary *m = HashMap<Object,Object>(); [@{@"key" : @"value"} mutableCopy]; m.put("key","value"); [m setObject:@"key2" forKey:@"value2"]; 2Dictionary
  • 38. NSString *s = @"value1"; String s = "value1"; NSNumber *n = @3.14159265359; Number n = 3.14159265359; NSArray *a = @[@"value1", @"value2"]; String a[] = {"value1","value2"}; NSDictionary *m = @{@"key" : @"value"}; Map<String,String> m = new NSArray *arr = @[ @(roundf(2*M_PI)) ]; HashMap<String,String>(); m.put("key","value"); NSMutableDictionary *dict = Number arr[] = { Math.round(2*Math.PI) }; [NSMutableDictionary dictionary]; 2Literals dict[@"key1"] = @"value1";
  • 39. POINTER Recap = OBJECT ALLOC CREATE INIT INITIALIZE LITERALS ARE AWESOME
  • 40. POINTER Recap = OBJECT ALLOC CREATE INIT INITIALIZE LITERALS ARE STRING CONCAT AWESOME SUCKS
  • 41. OBjective-c For Java developers 3implementations Interfaces &
  • 42. .h .m @implementation Foo { @interface Foo : NSObject { //Private ivars //protected/private/public ivars } } //Synthesize properties //Property declarations //Method implementations //Method declarations //Private methods 3.h & .m @end @end
  • 43. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject self = [super init]; - (id)initWithBar:(NSString *)bar; if (self != nil) { @end _bar = [bar copy]; } return self; 3Foo class } @end
  • 44. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end _bar = [bar copy]; } return self; 3Foo class } @end
  • 45. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end Nop } _bar = [bar copy]; return self; 3Foo class } @end
  • 46. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end Nop } _bar = [bar copy]; return self; self? Don’t you mean this? 3Foo class } @end
  • 47. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject nil? Don’t you mean null? init]; self = [super - (id)initWithBar:(NSString *)bar; if (self != nil) { @end Nop } _bar = [bar copy]; return self; self? Don’t you mean this? 3Foo class } Nop @end
  • 48. .h .m @implementation Foo { NSString *_bar; } - (id)initWithBar:(NSString *)bar { @interface Foo : NSObject self = [super init]; - (id)initWithBar:(NSString *)bar; if (self != nil) { @end _bar = [bar copy]; } return self; 3Foo class } @end
  • 49. .h .m @implementation Foo @interface Foo : NSObject - (int)sumA:(int)a withB:(int)b { return a+b; - (int)sumA:(int)a withB:(int)b; } + (Foo *)aFoo; + (Foo *)aFoo { @end return [[Foo alloc] init]; } 3+ & - @end
  • 50. .h class Foo { @interface Foo : NSObject private String bar; @property (nonatomic, copy) NSString *bar; @end public String getBar() { return bar; } .m public void setBar(String bar) { @implementation Foo this.bar = bar; @synthesize bar; } 3Properties @end }
  • 51. "I'm pretty sure that Java's 'interface' is a direct rip-off of Obj-C's 'protocol' which was largely designed by these ex-NeXT'ers..." Patrick Naughton (one of the original creators of Java) 3NSWhat?
  • 52. @protocol Color <NSObject> - (int)red; interface Color { - (int)green; public int getRed(); - (int)blue; public int getBlue(); public int getGreen(); @opcional } - (int)rgb; 3Protocols @end
  • 53. .h .m @implementation NSString (MD5) @interface NSString (MD5) - (NSString *)md5Hash { NSString *md5 = anMD5Func(self); - (NSString *)md5Hash; return md5; } @end 3Categories @end
  • 54. Recap Interface .H IMPLEMENTATION .M Interfaces are called protocols and there’s no abstract classes PROPERTIES ARE AWESOME
  • 55. Recap Interface .H IMPLEMENTATION .M Interfaces are called protocols and there’s And no abstract classes imagine PROPERTIES what you can do ARE with AWESOME categories
  • 56. 4remember c? OBjective-c For Java developers
  • 57. Foo.m main.m @implementation Foo int main(int argc, char *argv[]) { - (int)anAddress { @autoreleasepool { char *bar = malloc(1); Foo *foo = [[Foo alloc] init]; free(bar); printf("0x%X",[foo anAddress]); return &bar; } } } 4NOOO! @end
  • 58. "Objective-C is a hybrid programming language[…]formed by grafting the Smalltalk-80 style of object-oriented programming onto a C language rootstock. Objective-C adds precisely one new data type, the object[...]and precisely one new operation, the message expression. " 4Why C? Cox, Brad J. (1991). Object Oriented Programming: An Evolutionary Approach
  • 59. objc.h runtime.h typedef struct objc_object { Class isa; struct objc_class { } *id; Class isa; }; typedef struct objc_class *Class; 4The Object
  • 60. Core Foundation is a library with a set of programming interfaces conceptually derived from the Objective-C-based Foundation framework but implemented in the C language. To do this, Core Foundation implements a limited object model in C. Core Foundation defines opaque types that encapsulate data and functions, hereafter referred to as 4CF “objects.”
  • 61. Recap C Is at the bottom of objective-c and YOU SHOULD TAKE ADVANTAGE OF IT
  • 62. Recap C Is at the bottom of objective-c BUT C and YOU DOES NOT HAVE ARC SHOULD TAKE SO YOU ARE STUCK ADVANTAGE OF WITH IT MALLOC AND FREE
  • 63. 5BLOCKS OBjective-c For Java developers
  • 64. //create the array NSArray *arr = @[ @16, @11, @88 ]; //sort it using a block NSArray *sortedArr = [arr sortedArrayUsingComparator:^(id obj1, id obj2) { return [((NSNumber *)obj1) compare:(NSNumber *)obj2]; }]; //print 5Closures NSLog(@"%@",sortedArr);
  • 65. //save a block in a variable int (^aBlock)(int, int) = ^(int i, int j) { return i * j; }; //execute it int result = aBlock(1,2); 5Keep it
  • 66. //get the block as a parameter - (int)operationWithArg1:(int)i arg2:(int)j block:(int (^)(int, int))aBlock { return aBlock(i,j); } 5Get it
  • 67. “If you are using ARC, object variables are retained and released automatically as the block is copied and later released.” “If you use a block within the implementation of a method [...] If you access an instance variable by reference, self is retained; If you access an instance variable by value, the variable is retained.” “When you copy a stack-based block, you get a new block. If you copy a heap-based block, however, you simply increment the retain count of that block and get it back as the returned value of the copy function or 5Memory method.” Blocks &
  • 68. Recap BLOCKS Is MY FAVORITE OBJECTIVE-C LIBRARY AND IT WILL BE ONE OF YOURS TOO
  • 69. Recap BLOCKS Is MY FAVORITE OBJECTIVE-C LIBRARY AND IT The WILL BE ONE OF notation is hard to YOURS TOO MASTER though
  • 70. 6Other stuff OBjective-c For Java developers
  • 71. Localizable.strings ... "About" = "Sobre"; NSString *localizedAbout = "Open" = "Abrir"; NSLocalizedString(@"About", nil); "Close" = "Fechar"; ... 6クール!
  • 72. - (void)insertSublayer:(CALayer *)layer below:(CALayer *)sibling; - (void)insertSublayer:(CALayer *)layer above:(CALayer *)sibling; - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx; - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately: (BOOL)startImmediately NS_AVAILABLE(10_5, 2_0); 6Read it
  • 73. Recap this is just the beginning. There’s a lot more to objective-c
  • 74. Recap this is just the beginning. There’s a lot more to But now objective-c it won’t scare you !
  • 75. 7DEMO OBjective-c For Java developers
  • 76. Thanks for listening I’m @fbbernardo on twitter and fbernardo on github Thanks to @hugojsfranca for the awesome design suggestions