iOS-黑科技

2018-06-29  本文已影响143人  木马不在转
1.创建不能被继承的类

只要在基类的@interface 前面加上 objc_subclassing_restricted 这个属性即可:

__attribute__((objc_subclassing_restricted))
@interface Eunuch : NSObject

@end

@interface Child : Eunuch // <--- Compile Error

@end
2.标志子类继承这个方法时需要调用 super,否则给出编译警告
@interface Father : NSObject

- (void)hailHydra __attribute__((objc_requires_super));

- (void)addHeader NS_REQUIRES_SUPER;

@end

@implementation Father

- (void)hailHydra {

NSLog(@"hail hydra!");

}

- (void)addHeader{

}

@end

@interface Son : Father

@end

@implementation Son

- (void)hailHydra {

// <--- Warning missing [super hailHydra]

}

- (void)addHeader{

// <--- Warning missing [super hailHydra]

}
3.用于 C 函数,可以定义若干个函数名相同,但参数不同的方法,调用时编译器会自动根据参数选择函数原型
__attribute__((overloadable)) void logAnything(id obj) {

NSLog(@"%@", obj);

}

__attribute__((overloadable)) void logAnything(int number) {

NSLog(@"%@", @(number));

}

__attribute__((overloadable)) void logAnything(CGRect rect) {

NSLog(@"%@", NSStringFromCGRect(rect));

}

// Tests

logAnything(@[@"1", @"2"]);

logAnything(233);

logAnything(CGRectMake(1, 2, 3, 4));

@end
4.创建 [keyboardUtil adaptiveViewHandleWithAdaptiveView:<#(UIView *), ...#>, nil]的类似方法
- (void)adaptiveViewHandleWithAdaptiveView:(UIView *)adaptiveView, ...NS_REQUIRES_NIL_TERMINATION;
5.抛出异常
    @try {
    //            代码可能会抛出异常
    return [self swizzObjectAtIndex:index];
    } @catch (NSException *exception) {
    //            < #处理异常抛出@try块# >
    // 在崩溃后会打印崩溃信息,方便我们调试。
    NSLog(@"---------- %s Crash Because Method %s  ----------\n", class_getName(self.class), __func__);
    //            NSCAssert(self.count - 1 < index, @" 数组越界了 %@ ",[exception callStackSymbols]);
    } @finally {
    //            < #代码执行是否抛出异常# >
    }
6.对象创建的几种方式
    NSString *p = stringConcatenation(@"哈哈");

    NS_INLINE NSString*stringConcatenation(NSString*data)
    {
    NSString *s = [NSString stringWithFormat:@"拼合%@",data];

    return s;
    };//p 拼合哈哈
         /******         *******/
    sender(@"6不6");

    void sender(NSString*p)
    {
    NSString *s = [NSString stringWithFormat:@"拼合 3 %@",p];
    NSLog(@"%@",s);
    };
    UIView *vs = ({
    UIView *k = [[UIView alloc] init];
    k;
    });
    [self.view addSubview:vs];
7.自定义控件属性在XIB中引用
.h 中
@property(nonatomic) IBInspectable CGFloat spacing;

.m 中
- (void)setSpacing:(CGFloat)spacing {
  //自定义处理
}
8.valueForKeyPath的特殊用法
NSArray *array = @[@10, @23, @43, @54, @7, @17, @5];

 CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
 CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
 CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
 CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
 NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);
/*159.000000 22.714285 54.000000 5.000000*/
NSArray *array = @[@"qq", @"wechat", @"qq", @"msn", @"wechat"];
[array valueForKeyPath:@"@distinctUnionOfObjects.self"];
待续。。。
[addressTextField  setValue:[UIColor redColor] forKeyPath:@”_placeholderLabel.textColor”];
上一篇下一篇

猜你喜欢

热点阅读