Objective-C Syntax: What about object objects (5)

Source: Internet
Author: User
Objective-C syntax for object objects


If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/401
Create a class

Right-click the helloworld project, and click New file... In the displayed dialog box to add a new file to the project.



Select the file to create a Objective-C.class and click Next to continue creating.



Select the parent class of the currently created class. nsobject indicates that the normal object class is inherited. uitableviewcell and uiview are related to iOS display. Click Next to create the class.






Select the name of the class. The default name here is myclass. I will not modify it if it is test ~




After you click Save, the class myclass is fully created. It generates two files, myclass. h and myclass. M, which are basically the same as. h and. cpp in C ++. Here, only. h is used to write member object name Member method names, and. m is used to write the implementation of specific member methods and member objects.

Myclass. h
A large framework under Foundation OS, which includes a set of all class methods, and so on, so you need to import it here. @ Interface class start identifier, such as the class in Java or C @ End symbol of the class 
# Import <Foundation/Foundation. h> @ interface myclass: nsobject {// member variable nsstring * mname; int mnumber;} // member method-(ID) myinit;-(void) setinfo :( nsstring *) name: (INT) number;-(void) printinfo; @ end
Myclass. m

@ Implementation the specific method and object in implementation. h @ End is also the end symbol of the class.
-(ID) Init: This method is used for class initialization and creation. Each class needs to call the init method when it is created. After the parent class obtains the self pointer, here we can do some subclass initialization work. -(ID) The init method name can be modified at will, but the modified name needs to be written during alloc. Here we will write it -(ID) myinit
-(Void) The setinfo method is used to pass parameters, which is a little different from Java.
-(Void) printinfo method: Output member variable content
# Import "myclass. H "@ implementation myclass-(ID) myinit {self = [Super init]; If (Self) {// perform initialization here [self setinfo: @ "Yu Song Momo": 9527];} return self;}-(void) setinfo :( nsstring *) Name: (INT) number {// The parameter value assigned by mname = Name; mnumber = number;}-(void) printinfo {// output log nslog (@ "my name: % @", mname); nslog (@ "My number: % d ", mnumber) ;}@ end


Helloworldviewcontroller. h
Myclass * is used to declare a pointer to myclass.
#import <UIKit/UIKit.h>#import "MyClass.h"@interface HelloWorldViewController : UIViewController{        MyClass *myClass;}@end


Helloworldviewcontroller. m

-(Void) The viewdidload method is called when the interface is read here for syntax testing. Currently, the relevant code is written here first.


The myinit method is the initialization method that we re-write in myclass. Here, the initialization directly calls the method we write for initialization.

The priintinfo method can directly call the class method output information through the created myclass pointer.

The release method pointer must be released after use. If it is not released, the memory will be wasted. This is a terrible thing.

-(Void) viewdidload {[Super viewdidload]; // create the object myclass = [[myclass alloc] myinit]; // call the object method [myclass printinfo]; // release pointer [myclass release];}

Result displayed after running


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.