iOS中stringByAppendingString 和 stringByAppendingPathComponent 的区别

来源:互联网 发布:良品铺子淘宝 编辑:程序博客网 时间:2024/05/30 23:30

今天在用FMDB创建数据库的过程中出现DB Open Error 14 的错误,仔细分析是路径不正确。

-(instancetype)init{

    if(self = [superinit]){

        NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YESlastObject];

        NSString *fileName = [docstringByAppendingPathComponent:@"bluetooth.db"];

        self.mDb = [FMDatabasedatabaseWithPath:fileName];

        if([self.mDbopen]){

            [selfneedUpdate];

        }

    }

    returnself;

}

我的路径中少了斜杠/,因为我误用了stringByAppendingString,其实用了stringByAppendingString也没关系,只是需要自己手动添加斜杠。使用stringByAppendingPathComponent就不需要手动操作了。

 NSString *imagePath = [skinPath stringByAppendingString:[NSString stringWithFormat:@"/%@",imageName]];//stringByAppendingString是在skinPath加后缀的意思 NSString *imagePath = [skinPath stringByAppendingPathComponent:imageName];//stringByAppendingPathComponent是在skinPath后面加上“/”号连接imageName让它成为完整的路径 NSLog(@"imagePath:%@",imagePath);
imagePath:/Users/will/Library/Application Support/iPhone Simulator/7.0.3/Applications/DA5B603D-4D07-4425-B7CC-5D49232189BE/Willing.app/tabbar_home.png

0 0
原创粉丝点击