How to quickly master objective-c language

How to quickly master objective-c language

  1. Learn basic syntax: Understand the basic syntax rules of Objective-C, including variables, data types, operators, control flow statements, etc. You can learn by reading tutorials, reference books, or online resources.

  2. Familiar with object-oriented programming: Objective-C is an object-oriented programming language and requires mastering concepts such as classes, objects, inheritance, and polymorphism. Understand Objective-C class definition, instantiated object, method invocation and other operations.

  3. Master the Foundation framework: The Foundation framework is the basic framework of Objective-C and includes many commonly used classes and methods, such as NSString, NSArray, NSDictionary, etc. Learn and become familiar with the common classes and methods of the Foundation framework. You can learn from official documents or tutorials.

  4. Learn iOS development: If your goal is to develop iOS, you need to learn knowledge related to iOS development, including UIKit framework, interface design, data storage, network communication, etc. You can learn by reading iOS development tutorials, reference books or online resources.

  5. Practical projects: Consolidate the knowledge learned through practical projects. You can try to write some simple applications, from simple interface design to complex function implementation, and gradually improve your programming skills.

  6. Reference excellent code: By reading and analyzing excellent Objective-C code, you can learn some programming skills and best practices. You can refer to open source projects, tutorial sample codes, etc.

  7. Continuous learning and practice: Objective-C is a widely used programming language. Continuous learning and practice are the keys to improving yourself. Keep an eye on new technologies and development tools, participate in community discussions and exchanges, and share experiences and learning resources with other developers.

Record some meanings that you don’t understand here.

In Objective-C, @property is the keyword used to declare properties. By using the @property keyword, corresponding instance variables, getters, and setter methods can be automatically generated for properties.

The declaration of attributes is usually placed in the interface file (.h file) of the class, for example:

@interface MyClass : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) NSInteger age; @end

In the above example, the MyClass class declares two properties: name and age. The nameattribute is an object of type NSString. Use the strong modifier to indicate that a strong reference to the object is maintained in the reference count. ageThe attribute is a basic data type of NSInteger type. Use the assign modifier to indicate direct assignment to instance variables.

By using the@property keyword, the compiler automatically generates the following:

  1. Instance variable: For the name attribute, the compiler will automatically generate an instance variable named _name. For theage attribute, since it is a basic data type, no instance variable will be generated.
  2. getter method: The compiler will automatically generate getter methods named name and age to obtain the value of the attribute.
  3. setter method: The compiler will automatically generate setter methods named setName: and setAge: for setting the value of the property.

When using attributes, you can access and modify the attribute's value through dot syntax, for example:

MyClass *myObject = [[MyClass alloc] init]; myObject.name = @"John"; // 调用setName:方法 NSInteger myAge = myObject.age; // 调用age方法

In addition to the nonatomic and strong modifiers in the above example, @property also supports other modifiers, such as:

  • atomic: Indicates that the attribute is atomic, the default is atomic.
  • weak: Indicates that the attribute uses weak references.
  • readonly: Indicates that only getter methods are generated, and setter methods are not generated.
  • copy: Indicates that the attribute is copied when assigned.

These modifiers can be combined as needed to meet different needs.

Starting from Xcode 4.4, the @synthesize keyword is no longer required, and the compiler will automatically synthesize the instance variables and access methods of the property. Therefore, in recent versions of Objective-C, it is generally no longer necessary to use the @synthesize keyword explicitly.

In an ARC (Automatic Reference Counting) environment, the compiler will automatically insert the @autoreleasepool keyword, so there is usually no need to explicitly use the @autoreleasepool keyword. The @autoreleasepool keyword is only needed if manual memory management is required.

#import <Foundation/Foundation.h>

@interface Myclass : NSObject

@property (nonatomic,strong)NSString *name;
@property (nonatomic)int age;

- (void) sayHello;

@end


@implementation Myclass

- (void)sayHello{
    NSLog(@"Hello,my name is %@,and i am %d years old",self.name,self.age);

}

@end

In Objective-C, assign is a property modifier used to specify the assignment behavior of object properties. Specifically, assign is used to modify basic data types (such as NSInteger, CGFloat, BOOL etc.) properties.

When an attribute is declared asassign, it means that the assignment operation of the attribute is a simple assignment, and the reference count of the attribute will not be managed. This means that when the object is released, the object referenced by the property is not automatically released. Therefore, when using assign to modify object properties, special attention needs to be paid to avoid dangling pointer problems caused by accessing released objects.

It should be noted that assign is the default attribute modifier. If the attribute modifier is not explicitly specified, the attribute will be treated as or so that the reference count is managed automatically. assignstrongweak

Here are some examples showing the use of the assign modifier:

@property (nonatomic, assign) NSInteger age; @property (nonatomic, assign) CGFloat height; @property (nonatomic, assign) BOOL isStudent;

In the above example, age, height, and isStudent are all properties of the basic data type, and use < /span>assign modifier to declare. This means that assignment operations to these properties are simple assignments and no reference counting management is performed.

Guess you like

Origin blog.csdn.net/Vermouth_00/article/details/134297900