@interfaceNSMutableArray(sort)
-(void)pawpawSort;
@end
#import"NSMutableArray+sort.h"
@implementationNSMutableArray(sort)
-(void)pawpawSort{
for(inti=0;i<self.count;i++){
for(intj=0;j<self.count-i-1;j++){
if([self[j]intValue]>[self[j+1]intValue]){
[selfexchangeObjectAtIndex:jwithObjectAtIndex:j+1];
}
}
}
}
@end
然后在appdelegate.h中调用#import"NSMutableArray+sort.h"然后
NSMutableArray*array=[NSMutableArrayarrayWithObjects:@"4432",@"234",@"3432",@"321",nil];
[arraypawpawSort];
NSLog(@"%@",array);
个人心得:语法说不能给类添加属性,要知道属性就是方法,类目就是添加类方法的,所以个人认为是可以添加属性的~
3:延展中声明的方法可以不实现。
#import"Host.h"
//延展的声明
@interfaceHost()
-(BOOL)hasHouse;
@end
@implementationHost
ios6中的用法就是以上的不要~
个人心得:可以在其他类中强制使用延展,但是系统会提示没有定义!延展是一种特殊的类目
optional时可选择实现的方法。
#import<Foundation/Foundation.h>
@protocolClassProtocol<NSObject>
@required
-(void)notSheep;
-(void)notGetOffShoes;
@optional
-(void)notChat;
@end
#import"ClassProtocol.h"
@interfaceStudent:NSObject< ClassProtocol >{
NSString*_name;
}
@property(nonatomic,retain)NSString*name;
-(id)initWithName:(NSString*)aName;
-(void)leranClass:(NSString*)aClass;
@end
#import"Student.h"
@implementationStudent
@synthesizename=_name;
-(id)initWithName:(NSString*)aName{
self=[superinit];
if(self){
self.name=aName;
}
returnself;
}
-(void)leranClass:(NSString*)aClass{
NSLog(@"%@islearning%@",self.name,aClass);
}
-(void)notSheep{
NSLog(@"%@cannotsleep!!",self.name);
}
-(void)notGetOffShoes{
NSLog(@"%@cannotgetoffshoes",self.name);
}
-(void)dealloc{
[_namerelease];
[superdealloc];
}
@end
代理模式
即本身不做实际的事情,而要求其他人去做
例如要卖房子,不是直接销售,二十让中介去帮自己销售
中介就是我们的代理
//定义协议
#import<Foundation/Foundation.h>
@protocolRentDelegate<NSObject>
@required
-(void)renter:(id)aRenter
money:(int)aMoney
salar:(NSString*)aSalar;
@end
#import<Foundation/Foundation.h>
#import"RentDelegate.h"
#import"Host.h"
@interfaceHost:NSObject{
NSString*_name;
NSString*_address;
id<RentDelegate>_delegate;
}
@property(nonatomic,assign)id<RentDelegate>delegate;
@property(nonatomic,retain)NSString*name;
@property(nonatomic,retain)NSString*address;
-(id)initWithName:(NSString*)aName
Address:(NSString*)aAddress;
-(void)rentHouse:(NSString*)salarName;
@end
#import"Host.h"
@implementationHost
@synthesizename=_name,address=_address,delegate=_delegate;
-(id)initWithName:(NSString*)aNameAddress:(NSString*)aAddress{
self=[superinit];
if(self){
self.name=aName;
self.address=aAddress;
}
returnself;
}
-(void)rentHouse:(NSString*)salarName{
[self.delegaterenter:self.namemoney:100000salar:salarName];
}
-(void)dealloc{
[_namerelease];
[_addressrelease];
[superdealloc];
}
@end
#import<Foundation/Foundation.h>
#import"RentDelegate.h"
@interfaceSalar:NSObject<RentDelegate>{
NSString*_name;
}
@property(nonatomic,retain)NSString*name;
-(id)initWithName:(NSString*)aName;
@end
#import"Salar.h"
@implementationSalar
@synthesizename=_name;
-(id)initWithName:(NSString*)aName{
self=[superinit];
if(self){
self.name=aName;
}
returnself;
}
-(void)renter:(id)aRentermoney:(int)aMoneysalar:(NSString*)aSalar{
NSLog(@"%@代理%@:%d出租房子!",aSalar,aRenter,aMoney);
NSLog(@"房子出租成功!");
}
-(void)dealloc{
[_namerelease];
[superdealloc];
}
@end
//添加在AppDelegate.m
Salar*s=nil;
s=[[Salaralloc]initWithName:@"21世纪"];
Host*host;
host=[[Hostalloc]initWithName:@"wangzhibao"Address:@"eeeeeee"];
host.delegate=s;
[hostrentHouse:s.name];
[hostrelease];
[srelease];