IOS objective-c常用代码备份记录

获取项目主目录

 NSString *homeDir = NSHomeDirectory();
 NSLog(@"主目录:%@", homeDir);

获取项目文档目录

NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
NSLog(@"获取项目文档目录
%@", documentsDirectoryPath);

配置数据本地化存储,最终存储到 项目目录/Library/Preferences/ 下面,为plist文件。

		NSMutableArray *mutArr = [[NSMutableArray alloc]initWithObjects:@"1", nil];

        //存入数组并同步

        [[NSUserDefaults standardUserDefaults] setObject:mutArr forKey:@"mutableArr"];

        [[NSUserDefaults standardUserDefaults] synchronize];

        //读取存入的数组 打印

        NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArr"];

        NSLog(@"打印====%@",arr);

弹窗

	    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示信息" preferredStyle:UIAlertControllerStyleAlert];//UIAlertControllerStyleAlert视图在中央
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
     
        NSString *str = [NSString stringWithFormat:
        @"https://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8"];
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
         }];//https在iTunes中找,这里的事件是前往手机端App store下载微信
         [alertController addAction:cancelAction];
         [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];

字典操作

	NSMutableDictionary *hookConfig;
    hookConfig = [[NSMutableDictionary alloc] init];
	[hookConfig setObject:[NSNumber numberWithBool:isButtonOn] forKey:@"isRedEnvelopeSwitchEnable"];

打印调用堆栈

	NSArray *syms = [NSThread  callStackSymbols];
    if ([syms count] > 1) {
     
        for (int i=0; i < [syms count]; i++) {
     
            NSLog(@"mytest======<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:i]);
        }
    } else {
     
         NSLog(@"mytest======<%@ %p> %@", [self class], self, NSStringFromSelector(_cmd));
    }

打印对象所有属性

unsigned int count;
objc_property_t *properties = class_copyPropertyList([obj class], &count);
for(int i=0;i<count;i++){
     
    objc_property_t property = properties[i];
    NSString *key = [[NSString alloc]initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
    //kvc读值
    NSString *value = [obj valueForKey:key];
    NSLog(@"%@->%@",key,value);
}

你可能感兴趣的:(ios,ios,objective-c,代码)