Main.m
Edit Font Size COMMAND + <//compile run shortcut key COM + r#import <Foundation/Foundation.h> #import "Iphone.h"//To create object//1 with class file. To use # Import introduces the associated. h file//2. Create an object//3. Assign a value to the member variable of an object//#import的作用//1. Ability to introduce corresponding header file, provide prompt code hint//2. Ability to automatically prevent duplicate ingestion of header file int main (int argc , const char * argv[]) {iphone * phone = [iphone new]; Phone->_ram = 512; PHONE->_CPU = 1.5; %@ is actually the object placeholder NSLog (@ "%@", phone); /* Instance 3 char * s = "abc"; Define a string variable in oc NSString * str = @ "ABCD"; NSString * str2 = @ "EFG"; NSLog (@ "%@", str); NSLog (@ "%@%@", STR,STR2); stringWithFormat is specifically used to stitch string methods//using the same method as NSLog NSString * STR3 = [NSString stringwithformat:@ "%@%@", STR,STR2]; *///How do I create an OC object? 1. The method that creates the object must be called, new is dedicated to creating the object//2.oc as long as the calling method must be implemented within []//phone is called an instance variable, saving an object's pointer address//1. Open up memory space to store object//2. Initialization The value of the member variable is 0//3. Returns the pointer address of an object//iphone * phone = [Iphone new]; Directly assigns a value to the member variable of an object//phone->_ram = 512; PHONE->_CPU = 1.5;//NSLog (@ "Phone CPU%.2f,ram%.2f ", Phone->_cpu,phone->_ram); '-[iphone Aboutmyphone]: Unrecognized selector sent to instance 0X100102D70 ' no implementation of the//OC in the method call is also called the sending message, which is often said message mechanism/ /[phone Aboutmyphone]; [Phone Sendmessag: "is OK?"]; [Phone Sendmessag: "is OK?": "123456"]; [Phone sendmessagwithmessagecontent: "is OK?" Andphonenumber: "13527689735"]; Insert code here ...//1. There is no need to add \ n line-break//2. Some system-related information is automatically printed//3. The string to be stitched with the%@ placeholder//4.oc must use the @ "" to represent the *// Example code 1 int year = 2015; int month = 7; int day = 15; NSLog (@ "Hello, world%d%d%d%@!", year,month,day,@ "starts Today"); printf ("Hello World%d%d%d\n", year,month,day); */return 0; }
Iphone.h
#import the declaration of the <foundation/foundation.h>//@interface class file starts//@end class file declaration ends//:nsobject inherited syntax, all classes inherit from NSObject base class/ /syntax Rules://1. Class name naming rules: Hump nomenclature, and the first letter must be capitalized//2. Member variables: Be sure to underline the beginning @interface iphone:nsobject{ //@public The modified member variable, which can be accessed directly outside the class @public float _cpu;//is used to store the size of the CPU float _ram;//is used to store the internal capacity}//is specifically used to declare the method//1 in the member variable//oc. Called object method// You can only use objects (instance variables) to invoke//and must start with a-sign//oc as long as the data type that appears in the method declaration must be used () to extend the method void in the aboutmyphone;//c language (void) sendMessage (char * Content)///Send message//oc How many arguments in the method declaration must correspond to how many: fast switching between//.h and. m files Control + COM + up/down-(void) Sendmessag: (char *) content;-(void) SENDMESSAG: (char *) content andphonenumber: (char *) the method name in Phonenumber;//oc is focused on self-explanatory, that is, the ability to decipher the function of a specific method directly through the name of the method-(void) Sendmessagwithmessagecontent: (char *) messagecontent andphonenumber: (char *) PHONENUMBER;//2. Class Method @end
Iphone.m
#import "Iphone.h"//@implementation Class implementation section of the Start//@end class implementation part of the end of the//.m file is used to implement a series of methods declared in the. h file @implementation iphone//1. can copy not handwriting//2. Use code hint function as much as possible-(void) aboutmyphone{//Single line comment shortcut key COM +? Quick position to start/end COM + left/right//Quick check Current line COM + SHIFT + Left/right//NSLog (@ "Aboutmyphone already implemented"); Within the class, you can use the name of the member variable directly to get the value of the member variable NSLOG (@ "Phone CPU%.2f,ram%.2f", _cpu,_ram);} -(void) Sendmessag: (char *) content{NSLog (@ "%s", content);} -(void) Sendmessag: (char *) content andphonenumber: (char *) phonenumber{NSLog (@ "Send message%s to%s", phonenumber,content);} -(void) Sendmessagwithmessagecontent: (char *) messagecontent andphonenumber: (char *) phonenumber{NSLog (@ "Send message%s to%s", phonenumber,messagecontent);} Overriding the description method//description recommends that you rewrite this method in real-world development, and then print out the meaningful member variables in the class so that it is very convenient for us to debug the program-(NSString *) description{//optimization return [NSString stringwithformat:@ "My cpu=%.1f my internal storage is =%.1FMB", _cpu,_ram]; NSString *result = [NSString stringwithformat:@ "My cpu=%.1f my internal storage is =%.1FMB", _cpu,_ram]; return result; return @ "FFFFFFFF";} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
An object-oriented approach to designing an iphone