0

I am not a developer but I am trying to create an app that reads in the contents of a wavefront obj file from the native iOS "Files" folder on the iPad that a user selects, get the contents of the file into an array (or any other data structure that makes sense, if there is a better one to use), and pass it into a function does some calculations on the data. I am able to open up the Document Picker on the iPad and select a document. My next thought process is to try to create an NSData object using that URL, and then write that to an array. However, when I try to write a loop to print out the contents of the array, it never enters the loop so I don't think I'm writing the file to an array correctly. I also get an error saying "Failed to associate thumbnails for picked URL", not sure if that's any indication of what Im doing wrong. Here is what I have so far:

-(void)openDocumentPicker {                                                      
    UIDocumentPickerViewController* documentPicker =
      [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"]                                                     inMode:UIDocumentPickerModeImport];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
    documentPicker.allowsMultipleSelection = false;
    [self presentViewController: documentPicker animated: YES completion: nil];
}

-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    NSData *data = [NSData dataWithContentsOfURL: url];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSUInteger len = sizeof(array);
}

I have also tried to do this to write the data to an array:

NSData * data = [[NSData alloc] initWithContentsOfURL: url];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

and then loop through at the bottom to try to print out the contents of the array with

for (id object in array) {
NSLog(@"%@", object);
}

but it never enters the loop. I have also looked a little into arrayWithContentsOfURL but it looks like that is only for plist files? Any help would be greatly appreciated - thank you!

A.S
  • 1
  • What is the NSKeyedUnarchiver doing in the story. – matt Apr 14 '21 at 05:57
  • I saw from the top comment of this post to use NSKeyedUnarchiver go from a NSData to NSArray https://stackoverflow.com/questions/6108924/how-can-i-convert-a-nsdata-to-nsarray to – A.S Apr 14 '21 at 14:35

0 Answers0