iOS archiveRootObject 归档失败问题


归档失败问题出在路径上,NSHomeDirectory()

NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];

    stringPath = [stringPath stringByAppendingPathComponent:@"a.txt"];            // 成功

    NSLog(@"1111%@",stringPath);

    NSString * path2 = [NSHomeDirectory() stringByAppendingPathComponent:@"a.txt"];      // 失败,注意这里用的是NSHomeDirectory()

    NSLog(@"2222%@",path2);

    BOOL suc = [NSKeyedArchiver archiveRootObject:@"aaaa" toFile:path2];

    if (suc) {

        NSLog(@"user 归档成功!");

    }else{

        NSLog(@"user 归档失败!");

    }

 

1.NSHomeDirectory()  :  

/var/mobile/Containers/Data/Application/28C6BFE8-4E50-4DEB-9E55-18F0815CDBC5

2.[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0] :

/var/mobile/Containers/Data/Application/F7DC1040-57CB-4104-8AD6-55E9F287FC38/Documents/

看一下,

NSHomeDirectory() + /fileName   =  /var/mobile/Containers/Data/Application/28C6BFE8-4E50-4DEB-9E55-18F0815CDBC5/fileName  归档失败

[...NSDocumentDirectory...] +/fileName  = /var/mobile/Containers/Data/Application/F7DC1040-57CB-4104-8AD6-55E9F287FC38/Documents/fileName 成功 

红色区域是问题所在


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM