Learning Object-C classes and functions from command-line programs

[experimental environment]

Playing IOS on virtual machines really makes people want to raise their desks (')( Pro-test VMWare12, MACOS 10.10.5, Xcode 7.2 can be compatible with each other, the highest supported IOS version is 9.2, with download address, installation tutorial can Baidu itself.

VMWare WorkStations 12: Link: http://pan.baidu.com/s/1dETuoMH password: mcxb

MACOS 10.10: Link: http://pan.baidu.com/s/1nvJQJI5 password: c3l3 ps: Upgradable to 10.10.5 according to Baidu Tutorial

[Analyzing Objective-C Class]

On the basis of Xcode's default command line project, a new file named Student is created, which can be used to input a student's name, language achievement, mathematics achievement and English achievement.

After the creation is completed, two files, Student.h and Student.m, are automatically added to the Xcode project, which are similar to the header and class files of C++. At this time, the engineering structure of Xcode is shown in the following figure.

Among them, the. h file is used to declare the variables and methods of the class. The. m file first refers to the. h file and implements the method of its declaration.

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject{
    NSString* _name;
    int       _chinese;
    int       _math;
    int       _english;
}

-(void)setName:(NSString*) name;
-(void)setChinese:(int)chinese;
-(void)setMath:(int)math;
-(void)setEnglish:(int)english;
-(NSString*)name;
-(int)chinese;
-(int)math;
-(int)english;

@end
Student.m

#import "Student.h"

@implementation Student

-(void)setName:(NSString*) name{
    _name = name;
}

-(void)setChinese:(int)chinese{
    _chinese = chinese;
}

-(void)setMath:(int)math{
    _math = math;
}

-(void)setEnglish:(int)english{
    _english = english;
}

-(NSString*)name{
    return _name;
}

-(int)chinese{
    return _chinese;
}

-(int)math{
    return _math;
}

-(int)english{
    return _english;
}

@end
main.m

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student* student = [[Student alloc] init];
        [student setName:@"youzi"];
        [student setChinese:103];
        [student setMath:115];
        [student setEnglish:108];
        NSLog(@"%@ -- %d %d %d",[student name],[student chinese],[student math],[student english]);
    }
    return 0;
}
In main function, we first initialize an object student of Student with [[Student alloc] init], then call the four set methods of Student object to set its variable value, and print it out. The result is as follows:



[Analytic Objective-C Function]

1. Single-parameter function

As shown above, functions

-(void)setName:(NSString*) name
In'-', the function is an object function, the caller is an object of the class,'+'is a class function,'setName' is a function name,'NSString'is a string type of OC, and'name' is a parameter name.

2. Multi-parameter function

In the example above, the four member variables of the Student class are set by four functions, which can also be merged into one function.

-(void)setName:(NSString*)name Chinese:(int)chinese Math:(int)math English:(int)english
The invocation method is:

[student setName:@"youzi" Chinese:103 Math:115 English:108]

Keywords: xcode iOS Vmware

Added by countcet on Sun, 07 Jul 2019 22:46:40 +0300