Introduction to Objective-C

Objective-Objective-C is a strict superset of C — any C program can go straight through the Objective-C compiler without modification, and it’s perfectly legal to use C code in Objective-C. Objective-C has been described as a thin layer on top of C, because what Objective-C is all about is adding object-oriented features to the body of C.

extension Content type
.h Header files. Header files contain declarations of classes, types, functions, and constants. The #import option is identical to the #include option when including a header file, except that it ensures that the same file will only be included once.
.m Source code files. This is a typical source file extension that can contain both Objective-C and C code.
.mm Source code files. Source code files with this extension can contain C++ code in addition to Objective-C and C code. Use this extension only if you really need to use C++ classes or features in your Objective-C code.

2. Grammar

1. The string

Most Objective-C generally don’t use C-style strings. Instead, most frameworks pass strings to NSString objects. The NSString class provides a class wrapper for strings, with all the benefits you’d expect, including built-in memory management for holding strings of any length, support for Unicode, and printf-style formatting tools

NSString *string_0 = @" Afeixiaohuozi "; NSString * string_1 = [NSString String]; NSString * string_1 = [NSString String]; string_1 = @"afeixiaohuozi"; NSString * string_2 = [NSString String WithString:string_1]; NSString * string_2 = [NSString String WithString:string_1]; NSString * string3 = [NSString stringWithFormat:@"%@",string_1]; NSString * string_4 = [NSString stringWithFormat:@"hello %d %c",5,'A']; NSString *string_5 = [[NSString alloc] init]; NSString *string_6 = [[NSString alloc] initWithString:string_3]; NSString *string_6 = [[NSString alloc] initWithString:string_3]; NSString *string_7 = [[NSString alloc] initWithFormat:@"%@",string_4]; NSString * alloc = [NSString alloc] initWithFormat:@"%@",string_4];
Class 2.

The Objective-C class specification consists of two parts: Interface and Implementation. The Interface section contains the definition of the class declarations and instance variables, as well as the methods associated with the class. The Implementation section contains the actual code for the class method.

  • interface

    The definition section clearly defines the name, data members, and methods of the class. Start with the keyword @interface and end with the keyword @end

    @interface MyObject : NSObject { int memberVar1; // id memberVar2; } +(return_type) class_method; // class method -(return_type) instance_method1; // instance method -(return_type) instance_method2: (int) p1; -(return_type) instance_method3: (int) p1 andPar: (int) p2; @end // The +/- sign before the @end // method represents the type of the function. The + sign (+) represents a class method that can be called without an instance, similar to static member functions in C++. The minus sign (-) is the normal instance method.
    When you define a new method in Objective-C, the colon (:) in the name of the method is used to pass arguments, as opposed to C, which passes arguments around the parentheses of mathematical functions. Objective-C methods improve program readability by allowing arguments to be wrapped in the middle of the name, rather than all appended to the end of the method name. - (void) setColorFilter :(float) red Green: (float) Green Blue:(float) Blue; /* declared method */ myColor setColorFilter: 1.0 Green: 0.8 Blue: 0.2]; Method call / * * / / / this method signature is setColorToRed: Green, Blue,. Each colon is followed by a float class that represents red, green, and blue.
  • Implementation

    The implementation block contains the implementation of the public method and the definition of private variables and methods. The block starts with the keyword @implementation and ends with @end.

    @implementation MyObject { int memberVar3; } +(return_type) class_method {... //method implementation } -(return_type) instance_method1 { ... } -(return_type) instance_method2: (int) p1 { ... } -(return_type) instance_method3: (int) p1 andPar: (int) p2 { ... } @end
  • Create an object

    Objective-C creates objects using two messages, alloc and init. Alloc allocates memory, init initializes objects.

    MyObject * my = [[MyObject alloc] init];
    MyObject * my = [MyObject new];
    3. The enumeration

    The Foundation framework has provided us with a “unified and convenient” way to define enumerations

    Typedef NS_ENUM(NSUInteger, ttgState) {// The first argument is a type, the second is a type alias ttgStateOK = 0, ttgStateError, ttgStateUnknow}; typedef NS_OPTIONS(NSUInteger, TTGDirection) { TTGDirectionNone = 0, TTGDirectionTop = 1 << 0, TTGDirectionLeft = 1 << 1, TTGDirectionRight = 1 << 2, TTGDirectionBottom = 1 << 3 };