stringByAppendingPathComponent和stringByAppendingString的区别

stringByAppendingString是字符串拼接,拼接路径时要在名称前加“/”;

stringByAppendingPathComponent是路径拼接,会在字符串前自动添加“/”,成为完整路径。

例如:

NSString *imagePath = [skinPath stringByAppendingString:[NSString stringWithFormat:@"/%@",imageName]];

NSString *imagePath = [skinPath stringByAppendingPathComponent:imageName]; NSLog(@"imagePath:%@",imagePath);
好的,下面是添加签到逻辑和数据库的代码: 首先,在 `ViewController.h` 文件中添加头文件: ```objective-c #import <FMDB/FMDB.h> ``` 然后,在 `ViewController.m` 文件中添加以下代码: ```objective-c @interface ViewController () @property (nonatomic, strong) FMDatabase *database; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化数据库 [self initDatabase]; // 其他代码不变 } - (void)initDatabase { NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString *dbPath = [docPath stringByAppendingPathComponent:@"checkin.db"]; self.database = [FMDatabase databaseWithPath:dbPath]; if ([self.database open]) { NSString *createTableSql = @"CREATE TABLE IF NOT EXISTS checkin (id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT)"; BOOL success = [self.database executeUpdate:createTableSql]; if (!success) { NSLog(@"创建表失败"); } } else { NSLog(@"打开数据库失败"); } } - (void)checkInButtonClicked { // 获取当前日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *dateString = [formatter stringFromDate:[NSDate date]]; // 查询数据库,判断今天是否已经签到过 NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM checkin WHERE date='%@'", dateString]; FMResultSet *resultSet = [self.database executeQuery:querySql]; if ([resultSet next]) { // 已经签到过了 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"今天已经签到过了" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; } else { // 没有签到过,插入数据库 NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO checkin (date) VALUES ('%@')", dateString]; BOOL success = [self.database executeUpdate:insertSql]; if (success) { // 签到成功 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"签到成功" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; } else { // 签到失败 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"签到失败" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; } } } @end ``` 这里我们使用了 `FMDB` 框架来操作数据库。在 `initDatabase` 方法中初始化了数据库,并创建了一个名为 `checkin` 的表。在 `checkInButtonClicked` 方法中,我们获取当前日期,并查询数据库,判断今天是否已经签到过。如果已经签到过了,弹出提示框;如果没有签到过,就插入数据库,并弹出签到成功的提示框。 这里只是一个简单的示例,实际的应用场景可能会更复杂。你需要根据自己的需求来设计数据库结构,并写出相应的 SQL 语句来操作数据库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值