OC Study First day

Source: Internet
Author: User

OC First day 2016-3-14

Eg:1

#import <Foundation/Foundation.h>

Include header files in OC using #import

#import <Foundation/Foundation.h> representation: Contains the Foundation.h in the Foundation framework

Root class of Nsobject:oc

The suffix name of the OC implementation file is. m

Summarize the characteristics of the classes and methods inside the system

int main (int argc, const char * argv[]) {

@autoreleasepool {

Insert code here ...

NSLog (@ "Hello, world!"); /Implement Printing function

NSLog (@ "Hello, world!"); /has self-wrapping function.

int intvalue;

scanf ("%d", &intvalue);//Implement input data from the keyboard

NSLog (@ "intvalue:%d", intvalue);

char* p = "helloworld!";

NSLog (@ "p:%s", p);

The first parameter of NSLog: Formatting a string

The type to which the formatted string belongs is the NSString type, which is @ "Hello, world!" This is a string object

Declares a String object

nsstring* [email protected] "Helloworld-helloworld"; the object declared in//oc is a pointer

NSLog (@ "str:%@", str);//Output OC object, using the output format is%@

Declaring and initializing an object

nsstring* str1=[nsstring stringwithutf8string: "HELLO oc!"];

Converts a C string to a String object in OC (Initializes a string object in OC with a C string)

Use the form of a method in OC [object method name + argument list]

NSLog (@ "str:%@", str);

Equivalent

NSLog (@ "str:%@", [str description]);

%@ the output object, the compiler will callback the description method, and if the object belongs to a class that does not have the description method overridden, only the type and address can be output

NSLog (@ "str1:%@", str1);

printf ("Hello world!");

}

return 0;

}

Eg:2

1.c++ vs. OC: implementation file suffix name (. cpp. h); header file contains different preprocessing commands (#include #import)

#import可以防止头文件被重复包含 but still does not prevent cross-containment

To prevent cross-inclusion: use class in C + + to forward life; use @class in OC to make forward declaration

The basic structure of the 2.OC class definition

@interface Class Name: Parent class name

Member List

@end

3. Basic structure of class implementation

@implementation class Name

Implementation of members in a class

//

@end

4. Declaration of members in a class: instance variables (corresponding to C + + member variables) and methods (corresponding to C + + member functions)

The declaration of the instance variable is appended with "{//instance variable declaration}" after the parent class name

Declaration of the method, in the {//instance variable declaration} method declaration

#import <Foundation/Foundation.h>

@interface Mypoint:nsobject

{

Instance variable//default access permission is @pro tected

int x;

int y;

}

Method declaration

-(void) print;//implementation printing functions like non-static member functions in C + +

Method declaration format in OC:

-(return value type of the method) label name 1: (parameter 1 type) parameter 1 name label 2: (Parameter 2 type) Parameter 2 ...

Methods that begin with '-' are referred to as "instance methods."

-(void) SetX: (int) _x AndY: (int) _y;//Modify information

The declarative approach is to note that the number of colons is the same as the number of formal parameters. The method name consists of a label name and a colon (for example: Setx:andy:);

The first label name can not be omitted, other label names may be omitted

@end

int main (int argc, const char * argv[]) {

@autoreleasepool {

Insert code here ...

nsstring* [email protected] "Hello oc!";

NSLog (@ "str:%@", str);

Arrays in OC can only hold objects

nsnumber* Num=[nsnumber Numberwithint:10];

Convert basic data type int to OC object

NSLog (@ "nsnumber:%@", num);

//

nsnumber* num1=[nsnumber Numberwithchar: ' A '];

Convert basic data type char to OC object

NSLog (@ "nsnumber:%@", NUM1);

nsnumber* Num2=[nsnumber Numberwithbool:false];

NSLog (@ "%@", num2);

nsnumber* Num3=[nsnumber numberwithfloat:12.3];

NSLog (@ "%@", num3);

Nsinteger intvalues=20;//is not a class type, do not add *

nsnumber* Num4=[nsnumber Numberwithinteger:intvalues];

NSLog (@ "%@", NUM4);

NSLog (@ "Intvalues:%ld", intvalues);

addresscard* card=[[addresscard alloc]initwithname:@ "Tom" andemail:@ "[email protected]"];

NSLog (@ "Hello, world!");

}

return 0;

}

#import <Foundation/Foundation.h>

@interface Mypoint:nsobject

{

Instance variable

int x;

int y;

}

-(ID) INITWITHX: (int) _x AndY: (int) _y;

-(void) print;

-(NSString *) description;

-(void) SetX: (int) _x AndY: (int) _y;//Modify information

@end

@implementation MyPoint

-(void) print

{

NSLog (@ "x:%d, y:%d", X, y);

}

-(void) SetX: (int) _x AndY: (int) _y

{

x=_x;

y=_y;

}

-(ID) INITWITHX: (int) _x AndY: (int) _y

{

if (self = [super init]) {

x=_x;

y=_y;

}

return self;

}

Overriding the Description method

-(NSString *) description

{

nsstring* p=[nsstring stringWithFormat: @ "x:%d, y:%d", X, Y];

return p;

}

@end

int main (int argc, const char * argv[]) {

@autoreleasepool {//

mypoint* P=[[mypoint alloc] initwithx:1 andy:2]; Alloc equivalent to New in C + + opens heap space init: Initialization method (function to initialize instance variables in heap space)//The object itself is a pointer

Alloc: Application heap Space if the application heap space is successful, the OC instantiation object is returned (that is, the first address of the return heap space).

NSLog (@ "%@", p);//What if the content of the P object needs to be output? Overriding the description method in the P-owning class

Overriding: Defining a method in a derived class that is the same as the base class prototype, the derived class overrides the method of the base class

[P print];//[] is a salient feature of the message mechanism [receiver message]

Receiver: Message's recipient message: Messages

[P print]; Send a print message to the object p. The compiler searches for a method with the same name as the message in the P-owning class, and finds the execution

How do I disable the ARC mechanism (automatic reference counter mechanism)?

buildsettings--Search bar Search language--language objective c--objective-c antomatic Reference counting Change the value of this key to No

[P release];

NSLog (@ "\ n");

}

return 0;

}

OC Study First day

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.