nskeyedarchiver不适用于ios8,但适用于ios7和模拟器

时间:2014-09-29 15:22:03

标签: ios objective-c ios7 ios8 nskeyedarchiver

所以我使用NSKeyedArchiver作为心愿单,它在iOS 7和模拟器上运行良好,但在iOS 8设备上运行不正常。

这里是我正在使用的代码:

wishlist.m

- (void)viewDidLoad
{
    NSString *voirexpo = @"viensdeexpo";
    [[NSUserDefaults standardUserDefaults] setObject:voirexpo forKey:@"voirexpo"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    [super viewDidLoad];

    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    refait=0;
    refait2=0;
    wishlist * car2=[NSKeyedUnarchiver unarchiveObjectWithFile:@"myWishlist1.bin"];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSData * objectData = [defaults objectForKey:@"myWishlist1.bin"];
    if(objectData != nil)
    {
        wishlist * car3 = [NSKeyedUnarchiver
                               unarchiveObjectWithData:objectData];
        NSLog(@"YOOOOOOUUUUHOOOOO   %@", [car3 description]);

    }
    NSLog(@"YOOOOOOUUUUHOOOOO   %@", [car2 description]);


    rent=@"0";

    self.navigationItem.backBarButtonItem =
    [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                     style:UIBarButtonItemStyleBordered
                                    target:nil
                                    action:nil];

}

-(NSString*)pathForCacheFile:(NSString*)fileName
{

    NSArray*documentDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,    NSUserDomainMask, YES);
    NSString*path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];
    }
    return [NSString stringWithFormat:@"%@/%@", path, fileName];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(_dataSource)
    {
        _dataSource=nil;
    }


    _dataSource=[NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForCacheFile:@"myWishlist1.bin"]];

    tableview.scrollEnabled=YES;

    [tableview reloadData];

} 

cardescription.m (我选择将车放入心愿单的视图)

-(NSString*)pathForCacheFile:(NSString*)fileName
{

    NSArray*    documentDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString*   path = nil;

    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];
    }

    return [NSString stringWithFormat:@"%@/%@", path, fileName];
}

static wishlist *mywishlist;
-(void)saveInWishlist
{
    if(mywishlist)
        mywishlist=nil;
    mywishlist=[wishlist new];

    mywishlist.exponom=wishexpo;
    mywishlist.idvoiture=TelephoneCellulaire;
    mywishlist.imvoiture=dataimage;
    mywishlist.objid=objectId;

    {
        NSMutableArray *temp=[NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForCacheFile:@"myWishlist1.bin"]];


        //        mywishlist.imageSrc=filePath;

        if(temp.count==0 || temp==nil)
        {
            NSMutableArray*newArr=[NSMutableArray array];

            [newArr addObject:mywishlist];

            [NSKeyedArchiver archiveRootObject:newArr toFile:[self pathForCacheFile:@"myWishlist1.bin"]];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            // userObject is the object we want to save...
            [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:newArr] forKey:@"myWishlist1.bin"];
            [defaults synchronize];

        }
        else
        {
            [temp addObject:mywishlist];
            [NSKeyedArchiver archiveRootObject:temp toFile:[self pathForCacheFile:@"myWishlist1.bin"]];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            // userObject is the object we want to save...
            [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:temp] forKey:@"myWishlist1.bin"];
            [defaults synchronize];
        }
    }
}

- (IBAction)BACKO:(id)sender {

    [self saveInWishlist];

    NSLog(@"ICIII %@",wishexpo);

    wishlist * car2=[[wishlist alloc]init];
    car2.exponom=wishexpo;
    car2.idvoiture=TelephoneFix;
    car2.imvoiture=dataimage;
    car2.objid=objectId;
    [NSKeyedArchiver archiveRootObject:car2 toFile:@"
"];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wishlist"
                                                    message:@"This car has been successfully added to your wishlist"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}

-(NSString*)pathForCacheFile1:(NSString*)fileName
{

    NSArray*documentDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString*path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];
    }
    return [NSString stringWithFormat:@"%@/%@", path, fileName];
}

1 个答案:

答案 0 :(得分:1)

使用NSKeyedArchiver时出现同样的错误,仅在iOS8之后出现错误。在我的情况下,我这样做:

[defaultManager changeCurrentDirectoryPath:customDirectory];
[NSKeyedArchiver archiveRootObject:blankDictionary toFile:filename];

但是当我这样做时它起作用了:

NSString fullPath = [NSString stringWithFormat:@"%@/%@", customDirectory, filename];
[NSKeyedArchiver archiveRootObject:blankDictionary toFile:fullPath];

我的猜测是NSKeyedArchiver中存在一个错误,相对路径名也不起作用。如果搜索路径中有相对路径名,请尝试将其删除。