Objective-C basics: Basic knowledge of Object-Oriented Programming (OOP) (1) -- indirect, objective-coop
Since Cocoa is based on the concept of OOP and Objective-C is also an object-oriented programming language, it is necessary to frequently discuss the concept of OOP when learning Objective-C.
0x01 what is indirect?
Indirection is a key concept of OOP. It can be understood as "indirectly obtaining a value through a pointer in code, rather than directly obtaining it ".
0x02 variables and indirect
Basic variables are indirect practical applications. For example, variables are used to indicate the number of loops. To change the number of loops, you only need to modify the variable values in the Code:
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {int count = 100; // variable count plays an indirect role NSLog (@ "The numbers from 1 to % d:", count); for (int I = 1; I <= count; I ++) NSLog (@ "% d \ n", I); return 0 ;}
0x03 indirect use of file names
Built-in phrase string list in the program
Assume that a program can calculate and output the length of multiple phrase strings:
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {const char * words [4] = {"Joe-Bob \" Handyman \ "Brown ", "Jacksonville \" Sly \ "Murphy", "Shinara Bain", "George \" Guitar \ "Books"}; // int wordCount = 4; // The count to be counted for (int I = 0; I <wordCount; I ++) NSLog (@ "% s is % lu characters long", words [I], strlen (words [I]); return 0 ;}
In this program, if we often want to modify the phrase string list, we must change the program frequently, and also modify the number variable to be calculated synchronously. In case of a large number of words, we must make a lot of changes and be very careful not to make mistakes. From an object-oriented perspective, this is unscientific.
Put the phrase string list into a text file
Therefore, we can use another way of thinking to construct a program:
Moving all words to a text file outside the Code organizes the file structure according to certain rules, such as a line of name, which is also indirect.
Create a folder (word.txt) and store it in the tmp directory: Joe-Bob "Handyman" BrownJacksonville "Sly" MurphyShinara BainGeorge "Guitar" Books
Then construct a program to read the text file:
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {FILE * wordFile = fopen ("/tmp/words.txt", "r "); // tmp is a temporary Unix directory. char word [100] is cleared when the computer restarts. while (fgets (word, 100, wordFile )) {// Replace the linefeed with the null character word [strlen (word)-1] = '\ 0'; NSLog (@ "% s is % lu characters long", word, strlen (word);} fclose (wordFile); return 0 ;}
After using this scheme, we can change the combination of phrase strings at any time in a text file without modifying the program to avoid damage to the source code. However, this solution is still not convenient, and the file path cannot be specified by the user. In case the file is not in the position specified by the program, we must modify the source code.
Specify the file name using command line parameters
Therefore, we need to make further improvements:
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {if (argc = 1) // verify whether the user provides the path name as the startup parameter, the value of argc must be at least 1. If you provide a path name, argc must be greater than 1 {NSLog (@ "you need to provide a file name"); return 1; // when argc is set to 1, no FILE can be read.} // If argc is set to 1, FILE * wordFile = fopen (argv [1], "r") is read by path "); char word [100]; while (fgets (word, 100, wordFile) {// Replace the linefeed with an empty character word [strlen (word) -1] = '\ 0'; NSLog (@ "% s is % lu characters long", word, strlen (word);} fclose (wordFile); return 0 ;}
Here, we can view the argv array to obtain the path of the phrase string file. Argv [0] stores the program name, And argv [1] stores the file name provided by the user.
If you run this program in a terminal application, specify the file name in the command line as follows:
$ ./Word-Length-4 /tmp/words.txtJoe-Bob "Handyman" Brown is 24 characters longJacksonville "Sly" Murphy is 25 characters longShinara Bain is 12 characters longGeorge "Guitar" Books is 21 characters long
For how to set the file path before debugging, see:
Getting started with Xcode-provide the file path when running the program
-
Top
-
0
-
Step on
-
0
View comments