9.25 - OC - NSArray/自动布局

OC - NSArray各种常用的API

  • componentsJoinedByString 用某些自定符将字符串连接起来
NSString *str = [array componentsJoinedByString:@"."];
  • containsObject 判断是否有这个值
[array containsObject:@22]
  • indexOfObject 取数组元素的下标
NSInteger index2 = [array indexOfObject:@44]
  • eumerateObjectsUsingBlock
[array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
  • NSNotFound 判断是否找到
if(index3 == NSNotFound){
        NSLog(@"-->not found");
}
  • isEqual 比较数组中对象的值是否相等
array[i] isEqual:array2[i]
  • isEqualToArray 比较两个数组是否相等
[array2 isEqualToArray:array]
  • sortedArrayUsingConparator 排序
NSArray *array3 = @[@22,@1234,@456,@32,@67,@889,@990,@1234,@666];
    
NSArray *resultArray = [array3 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return obj1 > obj2;
}];

  • write to file
[array3 writeToFile:本地文件路径 atomically:YES];
  • reverseObjectEnumerator.allObjects 反转数组中的元素
NSArray *arr = @[@11,@22,@33];
NSLog(@"%@",arr.reverseObjectEnumerator.allObjects);

自动布局(Masonry)

注意点:

1.设置自动布局大小时,是mas_equalTo

make.size.mas_equalTo(CGSizeMake(300, 300));

2.在一个父视图内添加一个子视图,设置边距

make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

等同于

make.top.equalTo(sv).with.offset(10);
make.left.equalTo(sv).with.offset(10);
make.bottom.equalTo(sv).with.offset(-10);
make.right.equalTo(sv).with.offset(-10);

简单代码如下:

#import "ViewController.h"
#import 
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView *view1 = [UIView new];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
//
//    UIView *view2 = [UIView new];
//    view2.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:view2];
//    
//    UIView *view3 = [UIView new];
//    view3.backgroundColor = [UIColor purpleColor];
//    [self.view addSubview:view3];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.width.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(0.3);
    }];
    
//    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(view1.mas_bottom);
//        make.left.width.equalTo(view1);
//        make.height.equalTo(self.view).multipliedBy(0.3);
//    }];
//    
//    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(view1).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
//        
//    }];

    
    //循环排列:自动布局
    int count = 3;
    UIView *lastview = nil;
    for (int i = 1; i <= count; i++) {
        UIView *views = [UIView new];
        views.backgroundColor = [UIColor colorWithHue:(arc4random()%256/256) saturation:(arc4random()%128/256)+0.5 brightness:(arc4random()%128/256)+0.5 alpha:1];
        [self.view addSubview:views];
        
        [views mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(10);
            make.right.equalTo(self.view).offset(-10);
            make.height.equalTo(@(i*30));
            if (lastview){
                make.top.equalTo(lastview.mas_bottom).offset(10);
            }else{
                make.top.equalTo(view1.mas_bottom).offset(10);
            }
            
        }];
        lastview = views;
    }
}
@end

你可能感兴趣的:(9.25 - OC - NSArray/自动布局)