编程细节 - 1

1.换了图片之后发现不是自己想要的!! 

—       查看Images.xcassets 有没有遗留旧图

关于:Images.xcassets详解:

转自:  http://blog.csdn.net/yongyinmg/article/details/23845319




2.时间换算后发现输出为空 

—      有可能会是格式不对哦

- (NSString*)GetCurTime


{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];<span style="color:#ff0000;">// 这里改成: yyyy-MM-dd HH:mm:ss 就可以去得到时间差啦!</span>
    
    NSString*timeString=[formatter stringFromDate: [NSDate date]];
    
    [formatter release];
    
    return timeString;
}



3.直接从服务器拿数字,例如设置Label值等

注意哦,添加个intValue、integerValue,转成NSNumber等操作

       

 [cell.moneyLabelsetText:[NSStringstringWithFormat:@"%@", [[self.payPaidRecord.publicArrayobjectAtIndex:indexPath.row]objectForKey:@"money"]]];

添加个[NSString stringWithFormat:]方法来做约束,这里有趣的是,从服务器拿的数据看起来是数字,但是方法用的是%@,讨论一番后,认为是系统自动转成NSNumber类型。




4.存取方法的有趣:

@property (nonatomic,copy) NSString *source;

- (NSString*)source
{
    return [NSString stringWithFormat:@"来自%@", _source];
}
当重写get方法的时候,很奇怪的一点就是_source自动加了一个下划线,直觉告诉我是不是因为要区别函数名? 等有时间去StackOverflow解决它吧!


5.iOS中的private:

记得做面试题目的时候见过一题,OC中有私有变量吗?

嘿嘿,在autolayout的学习中,见到了OC的private变量:

NS_CLASS_AVAILABLE_IOS(6_0)
@interface NSLayoutConstraint : NSObject
{
    @private			
    id _container;
    id _firstItem;
    id _secondItem;
    CGFloat _constant;
    CGFloat _loweredConstant;
    id _markerAndPositiveExtraVar;
    id _negativeExtraVar;
    float _coefficient;
    UILayoutPriority _priority;
    uint64_t _layoutConstraintFlags;
    id _flange;
}

千真万确是有private变量的,但是其中文章很多,例如:


为什么会报这个错误,哎呀,怨自己当时C/C++不好好学,回头解决吧!但是直观的就是因为私有变量自己才能用呗,所以赋给它就不行啦。但是我试着:


这样也不行,所以,肯定要后续慢慢解决掉这些问题!




6.数据不能写死啊:

37的SDK作为中间层传数据给渠道的SDK的时候,要用单例生成或者接口传数据,而不是写死!!!




7.优雅: 

如果平常的时候我们会用10,-10 ,  但引入UIEdgeInsets变量时候既可以形象地表示四周的边距也可以用作数据

UIView *superview = self;

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[superview addConstraints:@[

    //view1 constraints
    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeTop
                                multiplier:1.0
                                  constant:padding.top],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeLeft
                                multiplier:1.0
                                  constant:padding.left],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1.0
                                  constant:-padding.bottom],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeRight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeRight
                                multiplier:1
                                  constant:-padding.right],

 ]];


8.让世界更清爽的写法 - NSArray NSMutableArray NSDictionary NSMutableDictionary NSNumber

参考:http://www.tuicool.com/articles/iaaeqi

提一提其中一点,更新NSMutableArray的简便:

LLVM4.0以前的更新数据方式如下:

NSMutableArray *oldMutable = [NSMutableArray arrayWithArray: old];

[mutable replaceObjectAtIndex:1 withObject:@"disposed"];
LLVM4.0以后:

NSMutableArray *newMutable = [NSMutableArray alloc] init];

newMutable[2] = @ " myObject " ;
就是更迈向C和java罢了!


9.函数还能这么写这么用

[button addTarget:self action:@selector(buttonClicked:)  
 forControlEvents:UIControlEventTouchUpInside];  
我们都知道button点击的@selectoer函数这么写,而传参的时候addTarget:action:forControlEvents:这个方法会自动地传入参数:

如下面的:(id)sender ,也可以直接打印这个sender来验证

- (void)buttonClicked:(id)sender  
{  
      
    [UIView beginAnimations:@"ShowHideView" context:nil];  //这里context:nil,值得思考  
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];  
    [UIView setAnimationDuration:1.0];  
    [UIView setAnimationDelegate:self];  
    [UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];  
      
    self.firstView.alpha = 0.0;  
      
    [UIView commitAnimations];  
}  

现在有趣的这个buttonClicked里面的一个方法:

[UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];  
这个是一个多参数的,我们可以理解上面的单参的情况,你编译器很好处理,我们看看具体如何实现:

- (void)showHideDidStop:(NSString*)animtaionID finished:(NSNumber*)finished context:(void*)context  
{  
    [UIView beginAnimations:@"ShowHideView2" context:nil];  
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];  
    [UIView setAnimationDuration:1.0];  
    [UIView setAnimationDelay:1.0];  
      
    self.firstView.alpha = 1.0;  
      
    [UIView commitAnimations];  
}  
和buttonClicked:有一点一样就是仍然没有使用到方法所赋的参数,就好像各玩各一样,方法里面自己做处理不管多少个参数和参数的意义。

这里能不能理解为编译器也做了处理,自己赋了参数,但是如此理解,有没有一个格式和参数数量,参数类型咧。有待日后验证哦。

其实只要弄懂点击事件的(id)sender参数的机制和原理就OK了。



10.当可以使用参数的时候,直接了当!

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"UIViewController will rotate to Orientation: %ld", self.interfaceOrientation);
}
完全可以这样写:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"UIViewController will rotate to Orientation: %ld", toInterfaceOrientation);
}




11.扩展

#import "LBCGTransformController.h"

@interface LBCGTransformController ()

@property (nonatomic, strong) UIButton* headImageView;
@end

@implementation LBCGTransformController

@end

上面的添加 圆括号 后叫做私有扩展。

之所以叫私有扩展是有其意义的,这样定义的属性,是私有的,无法点取的。



12.缩进格式

什么都不做的时候的格式:

CGPathAddCurveToPoint(thePath, NULL, 320.0, 500.0, 566.0, 500.0, 566.0, 74.0);

适当编排:

CGPathAddCurveToPoint(thePath, NULL, 74.0, 500.0,
                                     320.0, 500.0,
                                     320.0, 74.0);
多好看!



13.readonly 和 const

下面注意到readonly 和 const

#pragma mark - SelectionObject

@interface SelectionObject : NSObject<NSCopying>

@property(nonatomic, readonly)BOOL selected;
@property(nonatomic, readonly)uint32_t section;

- (id)initWithSection:(const uint32_t)section
          andSelected:(BOOL)selected;

@end



14.init family

有一次重写init方法爆出来了一个奇怪问题,一看:


嘻嘻, 细心一看吧,就是initwith 后面这个W小写了,这种错误和不能定义和MRC相冲突的变量一样!  init family 在学习官方文旦的时候也看见过这词!



15.图层的颜色要用CGColor

为什么图层的颜色要用CGColor?

layer.backgroundColor = [UIColor brownColor].CGColor;

查看图层的backgroundColor属性就知道:

@property CGColorRef backgroundColor;

原来它的颜色类是CGColorRef

同理:

layer.contents = (id)[UIImage imageNamed:@"12.png"].CGImage;

也是如此

那为什么呢:

因为:CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的;UIColor、UIImage是定义在UIKit框架中的



待续...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值