ios Development: An Introduction to Objective-C

Keywords: Java

I. Objective-C Basic Grammar
  • 1. String
    The string of Objective-C is wrapped in double quotation marks and preceded by an @ symbol.

    title = @"Hello World";
    if(title == @"hello World") {}
    
  • 2. Function Call

    • No parameters
      justRun();
      
    • With parameters
      NSLog(@"show string: %@", str);
      CGRectMake(0, 0, 0, 0);
      
  • 3. Passing messages to class/instance methods (sending messages to objects)

    • No parameters
      [obj method];
      

    Corresponding java version
    java obj.method();

    • With one parameter:
      [car fly:1];
      

    Corresponding java version
    car fly(1);

    • With multiple parameters
      The non-first parameter should have a similar key-value pair to represent the parameter Blue:(float)blue, that is, the method names are stitched together.

      - (void) setColorToRed: (float)red Green: (float)green Blue:(float)blue {...} //Definition method
      [myColor setColorToRed: 1.0 Green: 0.8 Blue: 0.2]; //Call method
      

      Corresponding java version

      public void setColorToRedGreenBlue(float red, float green, float blue) {...}
      myObj.setColorToRedGreenBlue(1.0, 0.8, 0.2);  
      
  • 4. Function Chain Call

    UINavigationBar *bar = [[[UINavigationBar alloc] init] autorelease];
    

Corresponding Java code
java UINavigationBar bar = UINavigationBar.alloc().init().autorelease();

  • 5. Creating Objects

Creating objects requires two messages, alloc and init. Alloc allocates memory and init initializes objects
objectivec MyObject * my = [[MyObject alloc] init];
In Objective-C 2.0, you can use new directly if you don't need parameters to create objects.
objectivec MyObject * my = [MyObject new];

To define the initialization process yourself, you can override the init method to add additional work. (constructor similar to C++)
objectivec - (id) init {if (self= [super init]) {// must call init // do something here...} return self;}

  • 6. id dynamic type

id is a generic object type that can point to objects belonging to any class
objectivec id obj = [Person new];

II. Categories

Object-C classes are divided into two parts: interface definition and implementation. Interfaces and implementations begin with @interface, @implementation, and end with @end. The symbol @ is a magical symbol in Objective-C.

  • 1. Interface Definition

Interface is placed in the header file with the file extension. h

Object-C Interface is more like a Class class in java
The Protocol of Objective-C is like the Interface interface in java

Object-C Customization An Interface Example

  @interface MyObject {
       int memberVar1;
       id  memberVar2;
   }

  -(void) instance_method1;
  -(BOOL) instance_method2: (int) p1;
  -(UITableViewCell *) instance_method3: (int) p1 andPar: (int) p2;
  @end
  • 2. implementation of Interface

In the implementation file, the file extension is.m

```objectivec
 @implementation MyObject {
     int memberVar3;
 }

-(void) instance_method1 {
    ....
}
-(BOOL) instance_method2: (int) p1 {
    ....
}
 -(UITableViewCell *) instance_method3: (int) p1 andPar: (int) p2 {
     ....
 }
@end
```

Java version of the above code

```Java
public class MyObject {
    protected int memberVar1;
    protected OtherClass memberVar2;
    private int memberVar3;

    public void instance_method1() {
        ....
    }

    public boolean instance_method2(int p1) {
        ....
    }

    public UITableViewCell instance_method3andPar(int p1, int p2) {
        ....
    }
}
```
  • 3. Class static method and instance method

    Static methods: Class methods have a plus prefix
    Instance method: Instance method has a minus prefix

    Class Definition

    @interface MyObject : NSObject
        +(void) sayHello;
        -(void) sayHello2;
    @end
    
    @implementation MyObject
    
    +(void) sayHello {
        NSLog(@"Hello, World");
    }
    -(void) sayHello2 {
        NSLog(@"Hello, World2");
    }
    @end
    

    call

    [MyClass sayHello];
    
    MyObject *mycls = [MyObject new];
    [mycls sayHello2];
    
  • 4. Class Inheritance

    @interface MyObject : NSObject
    @end
    

    Corresponding java code

    public class MyObject extends NSObject {
    }
    
Protocol

Interface interface equivalent to Java

  • 1. Definition of protocol

    @protocol Locking
        -(void)lock:(NSString)str;
    @end
    

    Corresponding Java code

    publilc interface Locking {
        public void lock(String str);
    }
    
  • 2. Inheritance of Agreement

    The agreement itself can also inherit other agreements.

    @protocol Locking <NSObject>
        -(void)lock:(NSString)str;
    @end
    

    Corresponding Java code

    public interface Locking extends NSObject {
        public void lock (String str);
    }
    
  • 3. Optional methods

The protocol can contain optional methods, which, as the name implies, can be implemented without classes, with the @optional keyword added, and a class can not implement print:methods when implements the protocol.
objectivec @protocol Locking @optional -(void)lock:(NSString)str; @end

  • 4. Implementation of Protocol

A class implements certain protocols that are written in the Interface definition, with multiple protocol names separated by commas
```objectivec

@interface  MyLockingController : NSObject <Locking, Drawable>

@end
```

Corresponding Java code

public class MyLockingController extends NSObject implements Locking, Drawable {
}

objectivec Reference Link on Wikipedia

Posted by neclord81 on Tue, 30 Jul 2019 06:54:11 -0700