[Informal agreement Objective-C language]

foreword

1. The role of classification is that the classes we write can be divided into multiple modules.

2. Can you write a classification for the class of the system?

3. Write a classification for the classes that come with the system, this is called an informal protocol

1. For example, there is the following 1 NSObject classification:

#import <Foundation/Foundation.h>
@interface NSObject (test)
– (void)run;
@end
#import “NSObject+test.h”
@iimplementation NSObject (test)
– (void)run
{
NSLog(@“我piapia的跑。。。”);
}
@end

Second, another Person class:

#import <Foundation/Foundation.h>
@interface Person:NSObject

@end
@implementation Person

@end

3. The main.m file is as follows:

#import <Foundation/Foundation.h>
#import “NSObject+test.h”
#import “Person.h”
int main()
{ Person *p1 = [Person new]; [p1 run]; } In this way, the p1 object also has run method.



Fourth, why does the Person class have a run method? Because the Person class inherits from the NSObject class, we have added a run method to the NSObject class through classification.

#import <Foundation/Foundation.h>
#import “NSObject+test.h”
#import “Person.h”
int main()
{ Person *p1 = [Person new]; [p1 run]; NSString *str = @" jack"; [str run]; } This is also possible.





5. The second function of classification: add methods to an existing class, such as NSObject class adding methods, you can't see its source code, you can only add methods to it through classification

For example, if you want to add a method to the NSString class to calculate the number of Arabic numerals in a string, what should you do:

6. The NSString class is very good, but I think there is one way to count the number of Arabic numbers in the string object.

For example: NSString *str = @"dfjsdij2oroo3or32p324lkltrw346";
Count how many Arabic numbers are in str:
idea: traverse each character in the string, if it is [0-9], the box variable is ++, and finally the box variable has How much will do.
How to judge whether each character is an Arabic numeral, and judge the ASCII code?
//Ideas:
//Traverse each character in the string to determine whether each character is an Arabic
number //The first question, how to traverse: is it a for loop
? for(int i = 0 ; i < str .length ; i++)
{

}
//The second question, how to get every 1 character, subscript, can you, str[i], can you, no, array can use subscript, this is not an array
//For example, now, I want to go In the string of str, the character whose subscript is 1 is not 'f', how to get it, how to get it in C language, is it str[1], remember, this is not acceptable, Because this is Objective-C, how do you get it? Remember, it is an object, and there are not many methods for an object. [str characterAtIndex:(NSUInteger)] is OK, and [str characterAtIndex:1] is OK. In this string, the character 'f' whose subscript is 1, what type is it, it is not a char type, then I use a char type variable to connect it:
char ch = [str characterAtIndex:1 ];
NSLog(@"ch = %c",ch);
Output: ch = f
Then if NSString *str = @"China I love you beijing";
what is [str characterAtIndex:1] obtained at this time? Is it a 'country'?
Output: ch = y'
I went, why is it a garbled character? At this time, you [str characterAtIndex:1] get 1 Chinese character, at this time, you char ch = [str characterAtIndex:1]; , ch has only 1 character and can't be saved, so what is the return value of this method, it is unichar, not char at all, - (unichar)characterAtIndex:(NSUInteger)index; So, it needs to be written as follows:
NSString *str = @"China I love you beijing";
unichar ch = [str characterAtIndex:1];
NSLog(@"ch = %c",ch);
What is unichar?
typedef unsigned short unichar;
unsigned short occupies two bytes; why is it two bytes, the reason is very simple, because 1 Chinese word, 1 byte cannot be saved, 1 Chinese in the C language, accounting for 3 Bytes, but in Objective-C, 1 Chinese character occupies 2 bytes, how many Chinese characters are there, 20,000 are enough, 1 byte can store 2 to the 8th power minus 1, 255 Chinese characters, 2 A byte can store 2 to the 16th power minus 1, 65535 Chinese characters, so 2 bytes are enough, try again:
NSString *str = @"China I love you beijing";
unichar ch = [str characterAtIndex: 1];
NSLog(@"ch = %c",ch);
output: ch = y`
is still garbled, because you use "%c" to output,
%d: it will start from the ch byte, and even Read 4 bytes,
%c: it will start from the ch byte, read 1 byte continuously, then find the ASCII code, print
%f:
%s:
%u: the first binary digit, not as a symbol Bits are directly regarded as data bits.
Therefore, the difference between each placeholder is that the way it is read in memory is different.
So, I use %c, but I just read the first byte of the 2 bytes of ch, and translate it into ASCII code, which may happen to be the character y', then I need to read a few bytes now, right? Read 2 bytes, what to do, use percent sign big C, %C, percent sign big C means, read 2 bytes consecutively, look up the ASCII code, and translate it back
//The third question, In this loop, we are going to get NSString *str = @"dfjsdij2oroo3or32p324lkltrw346"; for each character in it, it is to call the str method: [str characterAtIndex:i];, take a unichar variable and then follow it:
unichar ch = [ str characterAtIndex:i];
//At this time, the value of ch is the value of each character of our string.
//The fourth question, how to judge whether each character is an Arabic numeral:
if ( ch >= '0' && ch <='9')
{

  }
  //如果这个条件满足,就说明遍历出来的这个字符是阿拉伯数字吧。
  来个框变量:int count = 0;
  只要找到阿拉伯数字,框变量就++就可以了吧
  if (ch >= '0' && ch <= '9')
  {
          count++;
   }
   NSLog(@"count = %d",count);

7. Now we need to add a number method to the NSString class. Once this method is adjusted, we can calculate how many Arabic numbers are in the str object. How to add, is it not necessary to add by category?

Add a category to the NSString class: test
#import <Foundation/Foundation.h>
@interface NSString (test)
//Seek how many Arabic numbers are in the current string object, the current object is a string, so there is no need to bring The parameter, self is the current string
– (int)numberCount
@end
#import “NSString+test.h” @implementation
NSString (test)
///How many Arabic numbers are in the current string

  • (int)numberCount
    { int count = 0; //1. Traverse every 1 character in the current string object for(I = 0 ; I < self.length; I++) { unichar ch = [self characterAtIndex:i]; if(ch >= '0' && ch <= '9') { count++; } } return count; }











//Then, in main.m file
#import <Foundation/Foundation.h>
#import "NSString + test.h"
int main()
{ NSString *str = @"fsoifjo3r3049f095ijoerj1324"; int count = [str numberCount]; NSLog(@"%d", count); }



8. This is an informal protocol. Write a classification for an existing class, that is, a class for the system, and a class that comes with the system. This is an informal agreement. Therefore, the role of classification: 1. Divide bloated classes into categories. For multiple modules, easy to manage; 2. Extend a class, you find a class, I want to add a method to it, how to add it, use classification, these are the two application scenarios of classification.

Guess you like

Origin blog.csdn.net/madoca/article/details/126613813