优秀代码收集

1.点击一下确定,再点一下否定

- (BOOL)didSelectedAction {

   _isSelect = !_isSelect;

   _nodePermission.selected = _isSelect;

   return_isSelect;

}

 

2.安全删除集合中的元素

思路:因为nsarray是动态变化的,所以要安全删除其中的元素,可以先另外建一个数组,将要删除的元素加入到这个数组中,再一下删除。

       - (NSArray *)filterPersonWithLastName : (NSString *)filterTest {

           Person *person = [[Person  alloc] init];

           NSMutableArray  *personList = [ person creatTempraryList ] ;

           NSLog (@ "before");

           [self  printingPersonArray : personList ]; //加入对象到数组中

           NSMutableArray *personsToRemove = [NSMutableArray  array] ;

           for ( Person *person  in  personList){

                   if  (filterTest  &&  [filterTest  rangeOfString :person.lastName    options:NSLiteralSearch].length  == 0 )    //判断 如果不符合的person将添加进删除数组

                      [ personsToRemove   addObject : person];

               }

               [personList  removeObjectInArray : personsToRemove ] ;

              NSLog (@"after");

               [ self  printingPersonArray:personList ];

               [ person release];

               return  personList;

       }

 

 

3.获取随即数

常用方法 :arc4random

1、获取一个随机整数范围在:[0,100)包括0,不包括100

int x = arc4random() % 100;

2、  获取一个随机数范围在:[500,1000),包括500,不包括1000

int y = (arc4random() % 501) + 500;

3、获取一个随机整数,范围在[from,to),包括from,不包括to

-(int)getRandomNumber:(int)from to:(int)to

{

   return (int)(from + (arc4random() % (to – from + 1)));

}


4.NSArray不能保证没有重复元素,这样就可以用NSSet来剔除数组中的重复元素

NSSet *uniqueElements = [ NSSet setWithArray :array ];

for (id  element  in  uniqueElements ){

       //do  something

}


5.比较

- (BOOL)isEqual:(id)other {

   if (other == self)

       returnYES;

   if (!other || ![other isKindOfClass:[selfclass]])

       returnNO;

   return [selfisEqualToSelectingItem:other];

}

 

 

5.关于custom类型的button显示不了title

UIButtonTypeCustom确实是一个自定义的按钮类型。设置为它的默认值是没有意义的值。如果你想显示的话,你必须设置一个非透明的背景颜色或它的标题颜色:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    
    
[button setFrame:CGRectMake(x, y, width, height)];  
    
[button setTitle:@"Button" forState:UIControlStateNormal];  
    
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];   
    
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
    
[someSuperview addSubview:button];


6. 内存管理的问题

这个例子很能解决些问题

-(void) viewDidLoad{ //初始化 


Child *temp = [[Child alloc] init];     //引用计数为1 
self.child = temp;//引用计数加1,现在是2 
[temp release];//引用计数减1,现在1,不为0就不会被释放 
//self.child=[[[Child alloc] init]autorelease]; 如果是这样一定要加上autorelease,否则会泄露。 



-(void) dealloc{ 
[cihld release]; //是否用? 不用加此行会不会泄露?疑惑       //必须要释放,执行完词语后引用计数为0 
//self.child = nil //也可以这样释放,这个可以仔细去看看retain,assign,copy等的用法含义 
[super dealloc]; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值