【备忘】IOS难写且常用的语法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cl61917380/article/details/81905059

1.方法和变量black的写法

typedef void (^SureBlock)(NSString *score);
typedef void (^CancelBlock)(void);
@implementation MyClass{
	CancelBlock _cancelBlock;
    CloseBlock _sureBlock;
}
-(void)method:(void(^)(NSString *score))sureBlock cancelBlock:(void(^)(void))cancelBlock{
	_sureBlock = sureBlock;
	_cancelBlock = cancelBlock;
}
@end

2.available

if (@available(iOS 8.0, *)){
}

3.#deif

#ifdef DEV_VERSION

#else

#endif

#ifdef __OBJC__
	#import <UIKit/UIKit.h>
	#import <Foundation/Foundation.h>
#endif

4.pch

#ifndef Demo_Log_Debug_pch
	#define Demo_Log_Debug_pch
	#define safeString(obj) (([obj isEqual:[NSNull null]] || (obj == nil) || [@"null" isEqual:obj] || [@"<null>" isEqual:obj] || [@"(null)" isEqual:obj]) ? @"" : ([NSString stringWithFormat:@"%@",obj]))
	#define isEmptyString(obj) (([obj isEqual:[NSNull null]] || obj==nil || [@"null" isEqual:obj]) ? 1 : 0)
#endif

#ifdef __OBJC__
	#import <UIKit/UIKit.h>
	#import <Foundation/Foundation.h>
#endif

5.AppDelegate init

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *viewController = [[ViewController alloc]init];
    UINavigationController *root = [[UINavigationController alloc]initWithRootViewController:viewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window setRootViewController:root];
    [self.window makeKeyAndVisible];
    
    return YES;
}

//test push
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
    [button setTitle:@"push" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)push{
    MyViewController *vc = [[MyViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

6.单例 sharedInstance

//单例
+ (id)sharedInstance{
    static MyClass *instance;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

7.dispatch_get_main_queue

#define WEAKSELF typeof(self) __weak weakSelf = self;

__strong __typeof(weakSelf)strongSelf = weakSelf;

dispatch_async(dispatch_get_main_queue(), ^{

});

- (void)exChangeMessageDataSourceQueue:(void (^)(void))queue {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), queue);
}

8.enum:

typedef NS_ENUM(NSInteger, PPAlertViewHideAnimation){
    PPAlertViewHideAnimationNone,
    PPAlertViewHideAnimationFadeOut,
};

9.阴影shadow

view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.9]; //
view.layer.shadowColor = [UIColor grayColor].CGColor;
view.layer.shadowOffset = CGSizeMake(0, 3);
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 6.0;
view.layer.cornerRadius = 10.0;
view.clipsToBounds = NO;

10.多参数

NSMutableArray *points = [NSMutableArray arrayWithCapacity:0];
[points addObject:point];

va_list args;
NSValue *p;
va_start(args, point);
while ((p = va_arg(args, NSValue *))) {
    [points addObject:p];
}
va_end(args);

11.inline

#define force_inline __inline__ __attribute__((always_inline))

猜你喜欢

转载自blog.csdn.net/cl61917380/article/details/81905059