Use of Objective-c to create a class
The following is an example I wrote to test how Objective-c uses classes.
TestClass. h
/// TestClass. h // TestClass // Created by exchen on 6/15/15. // Copyright (c) 2015 exchen. All rights reserved. // # import
@ Interface TestClass: NSObject {// public member variable @ public int number1; int number2; NSString * Nstr; char strArray [20];} // member function declaration-(void) print;-(void) calc;-(NSString *) strAppend :( NSString *) string1 :( NSString *) string2; @ end
TestClass. m
//// TestClass. m // TestClass /// Created by exchen on 6/15/15. // Copyright (c) 2015 exchen. all rights reserved. // # import "TestClass. h "# import
@ Implementation TestClass // member function implementation-(void) print {printf ("% d \ n", number1); NSLog (Nstr); printf ("% s \ n ", strArray);}-(void) calc {number1 + = number2; printf ("% d \ n", number1);}-(NSString *) strAppend :( NSString *) string1 :( NSString *) string2 {NSString * strRet = [string1 stringByAppendingString: string2]; return strRet;} @ end
Main. m
/// Main. m // TestClass /// Created by exchen on 6/15/15. // Copyright (c) 2015 exchen. All rights reserved. // # import
# Import "TestClass. h" int main (int argc, const char * argv []) {@ autoreleasepool {// insert code here... NSLog (@ "Hello, World! ");} TestClass * tc = [[TestClass alloc] init]; // allocates memory tc-> number1 = 1; // assign a value to the class member variable tc-> number2 = 2; [tc calc]; // call the class member function strcpy (tc-> strArray, "strArray "); // assign a value to the class member string variable [tc print]; // call the class member function NSString * strRet = [tc strAppend: @ "string1": @ "string2"]; // call the NSLog (strRet) function with parameters; // print the return value return 0 ;}
Engineering