iPhone (iOS): copying files from main bundle to documents folder causes crash

后端 未结 6 1812
日久生厌
日久生厌 2020-12-10 03:46

I am trying to set up my application so that on first launch, a series of files located in the \"Populator\" folder in the main bundle are copied into the documents director

6条回答
  •  一生所求
    2020-12-10 04:16

       NSError *error;
    

    You are declaring a local variable without initializing it. Therefore, it will be filled with garbage.

      [[NSFileManager defaultManager] copyItemAtPath:sourcePath 
                                            toPath:folderPath
                                             error:&error];
    

    If no error occurs on this line, the garbage status of error would still remain.

      NSLog(@"Error description-%@ \n", [error localizedDescription]);
    

    Now you send a message to some random, uninitialized location. This is the source of the crash.


    To avoid this, initialize error to nil.

    NSError* error = nil;
    //             ^^^^^
    

    Or print the error only when -copyItemAtPath:… returns NO (in which the error is correctly populated).

    if (![[NSFileManager defaultManager] copyItemAtPath:sourcePath ...]) {
      NSLog(...); 
    }
    

提交回复
热议问题