Object-C

仉联
2023-12-01

Objective-C: A Superset of C
.h header files. it contains type fucntion constant declarations.
.m source files.contain both Object-C and C code.
.mm like above.It contain C++ code in addition to Object-C and C code.
Classes


@interface Myclass:NSObject
{
  int count;
  id  data;
}
-(id)initWithString:(NSString*)aName; //Method declarations.
+(MyClass*)CreateMyClassWithString:(NSString*)aName;//Method declarations.
@end

Notes:
MyClass: class name
NSObject: Parent class name .Cocoa's base class.
int count; | Member variable declarations
id  data;  |


Objective-C supports both strong and weak typing for variables containing objects. Strongly typed variables include the class name in the variable type declaration. Weakly typed variables use the type id for the object instead.
eg:
MyClass *myObject1;  // Strong typing

id       myObject2;  // Weak typing
The id type implies a pointer.

Methods and Messaging
A class in Objective-C can declare two types of methods: instance methods and class methods. An instance method is a method whose execution is scoped to a particular instance of the class. In other words, before you call an instance method, you must first create an instance of the class. Class methods, by comparison, do not require you to create an instance, but more on that later.

The declaration of a method consists of the method type identifier, a return type, one or more signature keywords, and the parameter type and name information.

Declared Properties
Declared properties are a convenience notation used to replace the declaration and, optionally, implementation of accessor methods.
You include property declarations with the method declarations in your class interface. The basic definition uses the @property compiler directive, followed by the type information and name of the property. You can also configure the property with custom options, which define how the accessor methods behave. The following example shows a few simple property declarations:

@property BOOL flag;

@property (copy) NSString *nameObject;  // Copy the object during assignment.

@property (readonly) UIView *rootView;  // Declare only a getter method
Each readable property specifies a method with the same name as the property. Each writable property specifies an additional method of the form setPropertyName:, where the first letter of the property name is capitalized.
In your class implementation, you can use the @synthesize compiler directive to ask the compiler to generate the methods according to the specification in the declaration:

@synthesize flag;

@synthesize nameObject;

@synthesize rootView;

Strings

As a superset of C, Objective-C supports the same conventions for specifying strings as C. In other words, single characters are enclosed by single quotes and strings of characters are surrounded by double quotes. However, Objective-C frameworks typically do not use C-style strings. Instead, they pass strings around as NSString objects.

 类似资料: