@interface NSMutableArray (sort)
-(void)pawpawSort;
@end
#import "NSMutableArray+sort.h"
@implementation NSMutableArray (sort)
-(void)pawpawSort{
for (int i = 0; i < self.count; i++) {
for (int j = 0; j < self.count - i - 1; j++) {
if ([self[j]intValue ] > [self[j+1]intValue]) {
[self exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
}
@end
然后在appdelegate.h中调用#import "NSMutableArray+sort.h"然后
NSMutableArray *array=[NSMutableArray arrayWithObjects:@"4432",@"234",@"3432",@"321",nil];
[array pawpawSort];
NSLog(@"%@",array);
个人心得: 语法说不能给类添加属性,要知道属性就是方法,类目就是添加类方法的,所以个人认为是可以添加属性的~
3:延展中声明的方法可以不实现。
#import "Host.h"
//延展的声明
@interface Host()
-(BOOL)hasHouse;
@end
@implementation Host
ios6中的用法 就是以上的不要~
个人心得:可以在其他类中强制使用延展,但是系统会提示没有定义!延展是一种特殊的类目
optional时可选择实现的方法。
#import <Foundation/Foundation.h>
@protocol ClassProtocol <NSObject>
@required
-(void)notSheep;
-(void)notGetOffShoes;
@optional
-(void)notChat;
@end
#import "ClassProtocol.h"
@interface Student : NSObject< ClassProtocol >{
NSString *_name;
}
@property(nonatomic ,retain)NSString *name;
-(id)initWithName:(NSString *)aName;
-(void)leranClass:(NSString *)aClass;
@end
#import "Student.h"
@implementation Student
@synthesize name=_name;
-(id)initWithName:(NSString *)aName{
self=[super init];
if (self) {
self.name=aName;
}
return self;
}
-(void)leranClass:(NSString *)aClass{
NSLog(@"%@ is learning %@",self.name,aClass);
}
-(void)notSheep{
NSLog(@"%@ can not sleep!!",self.name);
}
-(void)notGetOffShoes{
NSLog(@"%@ can not get off shoes",self.name);
}
-(void)dealloc{
[_name release];
[super dealloc];
}
@end
代理模式
即本身不做实际的事情,而要求其他人去做
例如要卖房子,不是直接销售,二十让中介去帮自己销售
中介就是我们的代理
//定义协议
#import <Foundation/Foundation.h>
@protocol RentDelegate <NSObject>
@required
-(void)renter:(id)aRenter
money:(int)aMoney
salar:(NSString *)aSalar;
@end
#import <Foundation/Foundation.h>
#import "RentDelegate.h"
#import "Host.h"
@interface Host : 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"
@implementation Host
@synthesize name=_name,address=_address,delegate=_delegate;
-(id)initWithName:(NSString *)aName Address:(NSString *)aAddress{
self=[super init];
if (self) {
self.name=aName;
self.address=aAddress;
}
return self;
}
-(void)rentHouse:(NSString *)salarName{
[self.delegate renter:self.name money:100000 salar:salarName];
}
-(void)dealloc{
[_name release];
[_address release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "RentDelegate.h"
@interface Salar : NSObject<RentDelegate>{
NSString *_name;
}
@property(nonatomic,retain) NSString *name;
-(id)initWithName:(NSString *)aName;
@end
#import "Salar.h"
@implementation Salar
@synthesize name=_name;
-(id)initWithName:(NSString *)aName{
self=[super init];
if (self) {
self.name=aName;
}
return self;
}
-(void)renter:(id)aRenter money:(int)aMoney salar:(NSString *)aSalar{
NSLog(@"%@代理%@:%d出租房子!",aSalar,aRenter,aMoney);
NSLog(@"房子出租成功!");
}
-(void)dealloc{
[_name release];
[super dealloc];
}
@end
//添加在AppDelegate.m
Salar *s=nil;
s=[[Salar alloc] initWithName:@"21世纪 "];
Host *host;
host=[[Host alloc] initWithName:@"wangzhibao" Address:@"eeeeeee"];
host.delegate=s;
[host rentHouse:s.name];
[host release];
[s release];