寫這篇博客的目的是為了把平時一些瑣碎的知識點總結一些厉膀,不然也容易忘記溶耘。
1.數(shù)組中內容去重復
//普通做法
if (![FeedConnArray containsObject:_ConnField.text]) {
[FeedConnArray addObject:_ConnField.text];
}
//牛逼做法 一行代碼去掉數(shù)組中重復的內容
self.dataList = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
2.NS_REQUIRES_SUPER 用法
在我們定義函數(shù)是,希望子類override時必須要調用子類方法是服鹅,可以在頭文件中定義函數(shù)是添加**NS_REQUIRES_SUPER **屬性凳兵,添加后如果子類不調用super 編譯器會報錯。
/** 初始化 */
- (void)prepare NS_REQUIRES_SUPER;
/** 擺放子控件frame */
- (void)placeSubviews NS_REQUIRES_SUPER;
/** 當scrollView的contentOffset發(fā)生改變的時候調用 */
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change NS_REQUIRES_SUPER;
3.Masonry使用技巧
Masonry 使用時都是很多屬性都是相對于父控件而言企软,所以有時候可以節(jié)省很多書寫代碼庐扫。下面講一個最基本的技巧
//創(chuàng)建個UIView
UIView * tempview = [UIView new];
tempview.backgroundColor = [UIColor redColor];
[self.view addSubview:tempview];
//最基礎用法,同時制定上下左右的和和需要對齊的控件的位置
[tempview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(20);
make.right.equalTo(self.view).offset(-20);
make.top.equalTo(self.view).offset(20);
make.bottom.equalTo(self.view).offset(-20);
}];
//按照上一步可以簡化一些代碼,父控件部分可以省略
[tempview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(20);
make.right.offset(-20);
make.top.offset(20);
make.bottom.offset(-20);
}];
//將上下左右簡化為一步
[tempview mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 20, 50, 50));
}];
//可以去掉父控件的代碼
[tempview mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));
}];
tips:使用Masonry時一定要注意使用前要制定父控件形庭,如果不指定铅辞,百分之百崩潰。