[OC] Summary of object-oriented foundation and Foundation framework


foreword

The author spent three weeks learning some basic knowledge of OC, and hereby write this blog to summarize and apply the knowledge learned.

1. Object-oriented

1. Classes and Objects

Classes and objects are a very important part of our object-oriented programming language. We create our objects through our classes. Our steps to create our class are divided into two parts: the interface and the implementation part. The interface is used to define our methods and member variables, and we can also use @property to define our properties. The implementation part provides the implementation for our methods

2. Create and use objects

Then we start creating and using our objects. Next, let's create an example of an object: FKUser *a = [[FKUser alloc] init];

Where a is our variable name, FKUser is our class, and alloc allocates space in the heap memory for the object we created.
Here we need to focus on understanding our alloc . When we use alloc, we actually call allocWithZone, which will be used later when we use the singleton mode.
Then there is how to call our method: the way we call the method follows the logic of our daily speech: subject-verb-object.
Our subject is our class or object, the predicate is our method, and the object is the parameter we pass in.
For example, Cai Xukun plays basketball: Cai Xukun is the object , playing is the method , and basketball is the parameter . Sometimes our method may not need to pass parameters, so we don’t have our object at this time

3. Singleton mode

Next is our more important singleton mode. In fact, our singleton mode can also be called singleton class mode. It is a class method. The specific function will continue to study later and write a blog summary. Here we first ask a question: Why are the addresses of the singleton variables created by the two methods different? Later, in the process of learning, the author will gradually solve this problem and improve the singleton mode: Implementation part: function part: the
output
here
insert image description here
is
insert image description here
0

4. Concealment and encapsulation

Next comes our hiding and encapsulation section. What we are more important here is our several access control characters and our @property , which must be emphasized hereOur properties are our member variables. Then what we need to remember is that our protect access control allows our member variables to be accessed in this class and its subclasses. When we define our member variables in the interface part, our protect will be used by default.
Then there is another point that when we use our dot syntax, our dot syntax is to access the properties of the class , and the essence is to call our set and get methods. Our -> default is to access our member variables, but our member variables are often protected, so errors often occur.

5. Object initialization

Then we proceed to initialize our object. Originally, our init method is to set the member variables of our object as initial values, but we can also rewrite our init method to reduce our code reuse.
Here is the template for our object initialization:
insert image description here
Here the author explains self = [super init]
Before that, we need to explain our self keyword:The self keyword always points to the object on which the method is calledThe biggest function of the self keyword is to allow a method in a class to access another method and member variable of the class. From this we can understand that our self keyword is generally used in the implementation part. Another point is that our self keyword can only call our instance methods .
Our code means that if our object is successfully created, then we assign an initial value to this object.

6. Inheritance

Next comes our inheritance part:
The characteristic of our inheritance part is that our subclasses will inherit the member variables and methods exposed by our parent class, and our subclasses can also override the methods of our parent class .
Our super keyword is to allow our subclass methods to call the methods in our parent class after rewriting .

7. Polymorphism

The key to class polymorphism is that our program is divided into a compilation phase and a running phase . The type of the variable is the type of our compilation phase, and the class of our space allocated for the object is the type of our running phase. We can only call the methods of the class in our compilation phase, but the last call must be the method of the class in the running phase. We also have to remember that we cannot call methods that the runtime class has but does not have at compile time .

Come down and we come to the next part of our object-oriented

8. Packaging class and processing object

First of all, what we learn is packaging classes and processing objects. We know that oc is an object-oriented programming language, but it is also extended from c language. But the basic types in our c language are not objects, such as int, double and the like. They have no object characteristics , and they have no attribute methods to call.
Our NSNumber and our NSValue are our wrapper classes, and they can add our basic type of data to our NSArray collection.Because our oc requires that the elements in our collection must be objects.
When we wrap our base types into objects, the common way is to use our class methods to directly wrap our type-specific values ​​into NSNumbers .
The key to these methods isCall our class method to wrap the primitive type into an object to add it to the collection

9. Rewrite the description method

After we wrap our value into an object we definitely need to print it. When we use NSLog to print our object alone, we actually call our description method to return the hexadecimal of our class , but we don't want to get this thing, what we want is his value, so we have to rewrite our description method.

10. == and isEqual

= = and the isEqual method are also the focus of our oc study.
Our == will only return true if the addresses of our two variables are the same, and our isEqual is an instance method that all pointer variables can call to determine whether they are equal to other pointer variables.
But we need to note that isEqual in our NSObject is actually no different from ==, but this method is rewritten in our NSString class, and its standard is that as long as the character sequences of the two strings are the same, it will return true.
Another point is that in order to save our memory, our oc will point these two pointer variables to the same object when creating two identical instance variables.

11. Categories and Extensions

The next line of learning is our category and extension
Our category can ensure that we can dynamically add new methods to our existing classes without accessing the source code of the parent class or creating subclasses. We generally define the modular method in the h file of the category and implement it in the m file.
But our extension is only used to define our method, and the implementation part of the method will be implemented in the class.

12. Agreement and entrustment

Then we come to the more important agreement and commission.
We can use our category to simulate our agreement. The author will focus on our formal agreement here.
Our protocol, like our class, can inherit from each other, and in our protocolOnly methods can be defined, without providing a method implementation.
But we need to pay attention to the inheritance format of our protocol here.
insert image description here
We need to add the class that our class needs to inherit in the interface part of our class, generally choose NSObject , and then follow the protocol we need to implement.
We also use two keywords to judge whether our method must be implemented or choose to implement
@required must implement
@optional is to choose to implement

Two, Foundation framework

Here we mainly talk about some classes contained in the framework, such as NSString, NSDate and collection classes.

1. NSString class

The way we assign a value to an NSString object is to directly assign a string constant to the object, for example: NSString *str = @"hello";
because our NSString is immutable, we can only modify our object indirectly through some methods to append or initialize our string after our original string, for example: these two methods
insert image description here
areThe object does not change, and the newly generated string is reassigned to the str pointer variable

2. NSMutableString class

Our class is different from the above class. Its character sequence can be changed. We use it through some methods. Here we can pay attention to the difference between our mutable and immutable: when we assign our NSString object, we must reassign the value returned by the method after the call to our object. For example, our NSMutableString can be modified directly
insert image description here
without reassignment a = [a stringByAppendingString:@"iii"];. The difference between the two class methods is that one has a stringby prefix and the other does not.

3. Date and time

OC provides NSDate and NSCalendar objects for handling these.
Let's introduce our NSDate first:
insert image description here

4. Date formatter

Our date formatters are divided into class and custom formatters
insert image description here

5. Timer

The author of the timer has not yet understood it here, and it will be added later when I understand it.

6. Object copy

Our object copy is also divided into copy and MutableCopy methods. The copy method returns an immutable string, and mutable returns a mutable string. Then we get our copy and modify it without any effect on the original.
Example:
insert image description here
Here we can see that the object we copied can be modified. But our self-created class iscannot call this method, we must override our copyWithZone method before we can successfully copy our objects.

When we call the copy method of the object to copy itself, the bottom layer of our program actually calls the copyWithXxxx: method to complete the actual copy work. What copy returns is actually the return value of the copyWithXxxx: method. The same goes for mutableCopyWithZone.

Deep copy and shallow copy is one of our focus, we review the definition of shallow copy here:When the member variable of an object is a pointer variable, if the program just copies the address of the pointer instead of actually copying the object pointed to by the pointer, then this method is called shallow copying.
That is to say, when we rewrite the copyWithZone method of a custom class, we do not really perform a deep copy, because the NSString type member variable similar to the copied object is the same as the actual pointing object of the copied variable .

Our solution here is toRecursively copy each member variable of pointer type
insert image description here

Here we will briefly talk about our copy attribute. In fact, its function is that when we use the set method to assign values ​​​​to our member variables, our program will call the copy method.Pass copied immutable copies to our objects to avoid illegal modifications

7、array

What we need to remember here is that the method we pass parameters to the array class is generally used. arrayWithObjects:
Then let's look at how to traverse the elements in our collection class. Here are two methods. The more commonly used method is the for...in method
insert image description here
.

For the method of variable array, we directly give his call:
insert image description here

8、set

We look at our collection, and the way we add elements to the collection is: setWithObjects: .

What we need to pay attention to when using set is that we generally rewrite our hash method, because the standard for judging the equality of two elements in our set is: the method isEqual returns yes and the return value of the hash method of the two objects is also equal, and set will judge that the two objects are equal. We rewrite our hash method generally with the following code:
insert image description here

9、dictionary

We can think of our dictionary as our linked list. He has key and value, which have a mapping relationship, and we use our key to find our value.
insert image description here
We see here that the value of our dictionary does not necessarily store basic type variables, but also other custom classes

This leads to a problem that the author encountered before:
a model class is defined, and its subclass is xiyoumobileperson.
insert image description here
Given the xiyoumobileperson class, there are some member variables in it . Because there is a property in our model class that is mutablearray, we can create a model class object and add objects to
insert image description here
the mutable array property inside with dot syntax. Pay attention to our objects hereIt doesn't have to be our basic type, it can also be our custom class, just like the value in our dictionary above

insert image description here

If we add a custom class to the array as above, then we can access the objects in the custom class by calling the method. Note that before we add elements to the array, weIt is necessary to create a custom class object first, and then use the arrayWithObjects: method to add our object to the array

Here we will mention our method of traversing the array:
insert image description here
our array has the same subscript as our c language, if we want to traverse the elements,Either find the object corresponding to our subscript first, or use our for...in method.
Also note here that if the element in our array is a custom class, then we must call our method to get the member variables of the custom class

Guess you like

Origin blog.csdn.net/weixin_72437555/article/details/130545526