iOS基础1

来源:互联网 发布:马士兵java教程下载 编辑:程序博客网 时间:2024/06/05 17:07

http://blog.csdn.net/jiangwei0910410003/article/details/52413396

http://blog.csdn.net/jiangwei0910410003/article/details/52413396

http://blog.csdn.net/jiangwei0910410003/article/details/52413396

http://blog.csdn.net/jiangwei0910410003/article/details/52413396

1111111111,oc语言基础

1,分类

@interface person(stone)。  //person分类类型     stone分类名

-----如果你想扩充一个类,就应该去继承这个类。但是oc里面有更好的方法,就是分类

-----可以使得不修改原来类的代码的基础上,对某个类进行方法的扩充

-----分类只能扩充方法,不能增加成员变量的定义

案例1111111111111111111111

/*

0000000.h

@interface Person(LP)

-  (void)study;

@end

00000000.m

@implementation Person(LP)

- (void)study

{NSLog(@"正在学习---");}

@end

000000000.m

@autoreleasepool{

Person *p1 = [[person alloc]initWithAge:33 andName:@"jack"];

        NSLog(@"年龄:%d,名字:%@",p1.age,p1.name);

        [p1 study]//调用分类的方法--分类创建的方法,还是使用原始类对象进行访问,需要引入分类的头文件

 }

 */

案例2

/*

除已有的Person.h和Person.m,新增两个文件(当然这样的分类 可以有很多个)

Person+Stone.h

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import "Person.h"  
  2.   
  3. @interface Person (Stone)//Person分类类型  Stone分类名  
  4. {  
  5.     //不能在分类中定义成员  
  6. }  
  7. - (void) info;  
  8.   
  9. @end  


Person+Stone.m

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import "Person+Stone.h"  
  2.   
  3. @implementation Person (Stone)  
  4.   
  5. - (void) info  
  6. {  
  7.     NSLog(@"call info method.");  
  8. }  
  9. @end  


main.m

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import "Person+Stone.h"  
  2. int main()  
  3. {  
  4.     Person* per = [[Person alloc] init];  
  5.     [per info];//分类创建的方法,还是使用原始类对象进行访问  需要引入分类的头文件  
  6.     return 0;  
  7. }  

*/

===========扩展扩展扩展扩展扩展扩展=========

===========类的延展延展延展延展延展=========

===========extensionextensionextension========

在普通类中的方法都是public的。。如果使用方法私有化,有两种方式:

1. 使用类似 分类的方式定义头文件,但是没有实现类,而是写在原始实现类.m中

PersonExtend.h

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import "Person.h"  
  2.   
  3. @interface Person () //() 一般不指定名字  
  4. - (void) smile; //声明  
  5. @end  

Person.m 引入PersonExtend.h ,并实现方法,  方法即为私有的。


2. 将@inteface ... @end 直接写到 Person.m中。或者不用@interface... @end声明方法,而在Person.m直接实现一个方法。不需要新建文件

Person.m

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import "Person.h"  
  2. #import "PersonExtend.h"  
  3. @interface Person (extension) //这里的@interface ... @end 可省略,但不建议。方便看出哪些是私有的方法  
  4. - (void) smile; //声明  
  5. @end  
  6. @implementation Person  
  7. void smile  
  8. {  
  9.   //在其他公有方法中,使用 [self smile] 调用  
  10. }  
  11. @end  

2,协议

oc中的协议就是相当于java中的接口(抽象类),只不过oc中的名字更形象点

协议就是定义了一组方法,然后让其他类去实现

==================================================

WithProtocol.h

#import <Foundation/Foundation.h>
@protocol WithProtocol <NSObject>
//默认是必须实现的
//必须实现
@required
- (void)finshTask;
- (void)dontLate;
//可选实现
@optional
- (void)wearNeat;
@end

================================================

这里就定义了一个协议WithProtocol

协议的定义格式

@protocol 协议名 <父协议>

定义方法

@end

协议中定义的方法还有两个修饰符:

@required:这个表示这个方法是其他类必须实现的,也是默认的值

@optional:这个表示这个方法对于其他类实现是可选的

这个就和类似与Java中的抽象类了,如果是abstract修饰的就必须实现,所以如果一个协议中没有@optional修饰的方法,那么这个协议就相当于Java中的接口了。


这里要注意的是,上面的代码中NSObject不是我们之前说的NSObject类了,而是NSObject协议,他也是OC中第一个协议,这个名字相同在OC中是没有关系的。

===================================

再看一下协议的使用

Student.h

#import <Foundation/Foundation.h>

#import "WithProtocol.h"

@interface Student : NSObject <WithProtocol>//使用协议很简单,直接在继承类(NSObject)后面<协议名>即可

-(void)study;

@end

====================================

====================================

Student.m

#import "student.h"

@implementation student

-(void)study{NSLog(@"study");}

//直接在.m文件中实现即可,不需要在.h文件中再次定义

#pragma mark -WithProtocol

-(void) finshTask{NSLog(@"完成任务");}

-(void)dontLate{NSLog(@"不迟到");}




====================================

+++++++++++++

===========================================

#pragma mark -WithProtocol这个作用就是做一下标记,标记后面的方法都是协议中的方法

这样就可以将一个类中的方法类别分得更细

看它是协议中的方法还是类本身的方法

===========================================

===========================================================================

测试类:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  main.m  
  3. //  11_ProtocolDemo  
  4. //  
  5. //  Created by jiangwei on 14-10-11.  
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. #import "Student.h"  
  12.   
  13. int main(int argc, const charchar * argv[]) {  
  14.     @autoreleasepool {  
  15.         Student *stu = [[Student alloc] init];  
  16.         [stu finshTask];  
  17.         [stu dontLate];  
  18.           
  19.         //判断wearNeat方法有没有在Student中实现了  
  20.         if([stu respondsToSelector:@selector(wearNeat)]){  
  21.             [stu wearNeat];  
  22.         }  
  23.     }  
  24.     return 0;  
  25. }  
这里有一个方法respondsToSelector:@selector,这个方法的作用是判断当前对象中是否定义了一个方法,这个方法还是很有用的,如果在Java中,我们可能需要用反射去实现了。


============================================================================


3,函数块

代码块也称block。是封装代码的一种机制,也可以称为匿名函数

使用这种机制可以将一段代码放入block变量中进行存储


4,Foundation

5,属性

6,KVC和KVO



0 0