oc学习第一天

OC第一天 2016-3-14

 

eg :1

#import <Foundation/Foundation.h>

//在OC中包含头文件使用 #import

//#import <Foundation/Foundation.h> 表示是:包含的是Foundation 框架中的Foundation.h

//NSObject:OC的根类

//OC实现文件的后缀名为.m

//总结系统内部的类和方法的特点

 

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

    @autoreleasepool {

        // insert code here...

        NSLog(@"Hello, World!");//实现打印功能

 

        NSLog(@"Hello, World!");//本身有自动换行功能

        int intValue;

        scanf("%d", &intValue);//实现从键盘输入数据

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

        

        char* p = "helloworld!";

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

        //NSLog的第一个参数:格式化字符串

        //格式化字符串所属的类型是NSString类型,即@"Hello, World!"这是一个字符串对象

        //声明一个字符串对象

        NSString* str=@"helloworld-helloworld";//oc中所声明的对象都是指针

        NSLog(@"str:%@", str);//输出oc对象,使用的输出格式是%@

        //声明并初始化对象

        

        NSString* str1=[NSString stringWithUTF8String:"HELLO OC!"];

        //将一个c字符串转换为oc中的字符串对象(用c字符串初始化oc中的字符串对象)

        //在oc中使用方法的形式[对象    方法名+实参列表]

 

 

  NSLog(@“str:%@”, str);

等价

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

        //%@输出对象时,编译器会回调description方法, 如果对象所属类中没有重写description方法,则只能输出类型和地址

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

        //printf("hello world!");

    }

    return 0;

}

 

 

eg:2

//1.c++与oc区别:实现文件的后缀名(.cpp .h);头文件包含的预处理命令不同(#include #import)

//#import可以防止头文件被重复包含,但仍然不能防止交叉包含

//为了防止交叉包含:c++中使用class做前向生命;在oc中使用@class做前向声明

 

 

//2.OC类定义的基本结构

//@interface 类名 : 父类类名

//  成员列表

//@end

 

//3.类实现的基本结构

//@implementation 类名

//类中成员的实现

//

//@end

 

//4.类中成员的声明:实例变量(对应c++成员变量)和方法(对应c++成员函数)

//实例变量的声明在父类类名后面加“{ //实例变量声明}”

//方法的声明,在{//实例变量声明 }方法声明

 

#import <Foundation/Foundation.h>

@interface MyPoint : NSObject

{

    //实例变量  //默认访问权限为@pro     tected

    int x;

    int y;

    

}

//方法声明

-(void)print;//实现打印功能 类似c++中的非静态成员函数

//oc中方法声明格式:

//-(方法的返回值类型)标签名1:(形参1类型)形参1名 标签2:(形参2类型)形参2名。。。

 

//以'-'开头的方法被称为“实例方法”。

-(void)setX:(int)_x andY:(int)_y;//修改信息

//声明方法是注意:冒号的个数和形参个数一致。方法名是由标签名与冒号构成(如:setX: andY:);除了

//第一个标签名不可以省略之外,其它的标签名可以省略

@end

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

    @autoreleasepool {

        // insert code here...

//        NSString* str=@"hello oc!";

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

        //oc中的数组只能存放对象

//        NSNumber* num=[NSNumber numberWithInt:10];

//        //将基本数据类型int转化为oc对象

//        NSLog(@"NSNumber:%@", num);

//        

//        NSNumber* num1=[NSNumber numberWithChar:'A'];

//        //将基本数据类型char转化为oc对象

//        NSLog(@"NSNumber:%@", num1);

        

        NSNumber* num2=[NSNumber numberWithBool:false];

        NSLog(@"%@", num2);

        

        NSNumber* num3=[NSNumber numberWithFloat:12.3];

        NSLog(@"%@", num3);

        

        NSInteger intvalues=20;//并不是类类型,不用加*

        NSNumber* num4=[NSNumber numberWithInteger:intvalues];

        NSLog(@"%@", num4);

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

//        AddressCard* card=[[AddressCard alloc]initWithName:@"Tom" andEmail:@"Tom@gmail.com"];

        

        

        NSLog(@"Hello, World!");

    }

    return 0;

}

 

 

#import <Foundation/Foundation.h>

@interface MyPoint : NSObject

{

    //实例变量

    int x;

    int y;

}

 

-(id)initWithX:(int)_x andY:(int)_y;

-(void)print;

 

-(NSString *)description;

 

-(void)setX:(int)_x andY:(int)_y;//修改信息

@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;

 

}

//重写description方法

-(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相当于c++中的new开辟堆空间 init:初始化方法(作用给堆空间里的实例变量初始化) //对象本身就是指针

        //alloc:申请堆空间 如果申请堆空间成功,则返回oc实例化对象(即返回堆空间的首地址)。

        NSLog(@"%@", p);//如果需要输出的是p对象的内容,怎么办?在p所属类中重写description方法

        //重写:在派生类中定义一个与基类原型相同的方法,则派生类重写了基类的方法

        

        //[p print];//[]是消息机制的明显特征 [receiver message]

        //receiver:消息的接收者   message:消息

        //[p print];向对象p发送print消息。编译器会在p所属类中找寻与消息同名的方法,找到则执行

        

        //怎样禁掉ARC机制(自动引用计数器机制)?

//        BuildSettings--搜索栏中搜索language--language objective c--objective-c Antomatic Reference Counting将此键对应的值改为NO

        [p release];

        NSLog(@ " ");

        

    }

    return 0;

}

 

原文地址:https://www.cnblogs.com/about-zj-blog/p/5287183.html