编写高质量iOS代码的52个有效方法(持续更新)

2017-06-13  本文已影响0人  Arackboss
1.少用for循环遍历,多用基于块的遍历
2.将类的实现代码分散到便于管理的数个分类中
3.在既有类中使用关联对象存放自定义数据
 NSArray *array = @[@1,@2,@3,@4,@5,@6,@7,@8,@9];
 [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@",obj);
    }];```
![屏幕快照 2017-06-12 下午9.04.53.png](https://img.haomeiwen.com/i3425795/9fb2b83fd9c17759.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

同样`NSDictionary`和`NSSet`也一样,只是稍微有些不同而已
用此方式来遍历大大胜过其他方式的地方在于:遍历时可以直接从块中获取更多信息,在遍历数组时可以知道当前所针对的下标。遍历有序Set时也一样。而在遍历字典时无需额外编码,即可同时获取键与值,因而省去了根据给定键来获取对应值这一步。另外一个好处是能够修改块的方法签名,以免进行类型转换操作,从效果上讲,相当于把本来需要执行的类型装换操作交给块方法签名来做。比方说,要用“快速遍历法”来遍历字典,如果已知字典中的对象必定为字符串,则可以这样编码:

for(NSString *key in aDictionary){
NSString *obj = (NSString *)aDictionary[key];
//Do something with 'key' and 'obj'
}

如果改用基于块的方式来遍历,那么就可以在块方法签名中直接转换:

NSDictionary aDictionary = /...*/ ;
[aDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key,NSString *obj,BOOL *stop){
//Do something with 'key' and 'obj'
}];```

#import<Foundation/Foundation.h>
@interface Person:NSObject
@property(nonatomic,copy,readonly)NSString *firsName;
@property(nonatomic,copy,readonly)NSString *lastName;
@property (nonatomic,strong) NSArray *friends;
@end

-(void)addFriend:(EQPerson *)person;
-(void)removeFriend:(EQPerson *)person;
-(BOOL)isFreendsWith:(EQPerson *)person;

-(void)perforWork;
-(void)takeVacationFromWork;
@end

在Person这个类中有两类方法,一类是与朋友有关的,一类是与工作有关的,这时我们可以将两类方法的实现写在不同的两个分类中:

#import <Foundation/Foundation.h>
@interface EQPerson : NSObject

@property(nonatomic,copy,readonly)NSString *firsName;
@property(nonatomic,copy,readonly)NSString *lastName;
@property (nonatomic,strong) NSArray *friends;

@end

@interface EQPerson(Friend)
-(void)addFriend:(EQPerson *)person;
-(void)removeFriend:(EQPerson *)person;
-(BOOL)isFreendsWith:(EQPerson *)person;

@end

@interface EQPerson(work)

-(void)perforWork;
-(void)takeVacationFromWork;

@end

EQPerson的Friend分类:

#import "EQPerson+Friend.h"

@implementation EQPerson (Friend)


-(BOOL)isFreendsWith:(EQPerson *)person{
    //do something
    return NO;
}

-(void)addFriend:(EQPerson *)person
{
    //do something
}

-(void)removeFriend:(EQPerson *)person
{
    //do something
}

@end

EQPerson的Work分类:

#import "EQPerson+work.h"

@implementation EQPerson (work)

-(void)perforWork
{
    //do something
}

-(void)takeVacationFromWork
{
    //do something
}

@end

也就是把不同类型的实现文件分别写入对应的分类中

-(void)creatAlertView
{
    UIAertView *alert = [UIAlertView alloc] initWithTitle:@"Question" message:@"What do you want to do?"
delegate:self cancleButtonTitle:@"Cancle" otherButtonTitles:@"Continue",nil];

[alert show];

}

然后在其代理协议里面写响应事件:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
  //do some thing
} else{
  //do something
}
}

我们需要在代理方法里面判断点击的是哪个按钮,如果同一个类中有多个这样的视图,那么代码就会变得更加难管理,这时我们可以通过关联对象来完成,创建完视图后,给他关联一个block,等到执行delegate的时候将block读出来执行就可以了:

#import <objc/runtime.h>
static void *EOCMyAlertViewKey = @"EOCMyAlertViewKey";
-(void)creatAlertView
{
    UIAertView *alert = [UIAlertView alloc] initWithTitle:@"Question" message:@"What do you want to do?"
delegate:self cancleButtonTitle:@"Cancle" otherButtonTitles:@"Continue",nil];

void(^block)(NSInteger) = ^(NSInteger buttonIndex){
     if(buttonIndex == 0){
       //do some thing
}else{
       //do some thing
}
};
objc_setAssociatedObject(alert,EOCMyAlertViewKey,block,OBJC_ASSOCIATION_COPY);

[alert show];
}  

然后在代理方法里面只需要执行block就可以达到我们想要的效果了:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  void (^block)(NSInteger) = objc_getAssociatedObject(alertView,EOCMyAlertViewKey);
block(buttonIndex);
}
上一篇 下一篇

猜你喜欢

热点阅读