copy - Using NSOpenPanel and copying selected file - Objective-C -
i'm trying create app copy selected sound file app's directory. it, i've written following code :
nsopenpanel* opendlg = [nsopenpanel openpanel]; [opendlg setcanchoosefiles:yes]; [opendlg setallowsmultipleselection:no]; [opendlg setcanchoosedirectories:no]; [opendlg setallowedfiletypes:[nsarray arraywithobjects:@"aif",@"aiff",@"mp3",@"wav",@"m4a",nil]]; if ( [opendlg runmodalfordirectory:nil file:nil] == nsokbutton ) { nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error; nsstring *datapath = [[nsbundle mainbundle] bundlepath]; nslog(@"datapath %@", datapath); nslog(@"selected files : %@",[[opendlg urls] objectatindex:0]); if ([filemanager fileexistsatpath:datapath] == no) { [filemanager copyitematpath:[[opendlg urls] objectatindex:0] topath:[[nsbundle mainbundle] bundlepath] error:&error]; nslog(@"file copied"); } } the problems can select each types of files (not aif, wav, mp3 etc.) , never file copied. although, paths correct. when delete if statement, error saying : [nsurl filesystemrepresentation]: unrecognized selector sent instance 0x1005a0b90. what's wrong in code?
you're passing nsurl api expects path in nsstring. might consider using url-based api:
- (bool)copyitematurl:(nsurl *)srcurl tourl:(nsurl *)dsturl error:(nserror **)error ns_available(10_6, 4_0); like this:
[filemanager copyitematurl: [[opendlg urls] objectatindex:0] tourl: [nsurl fileurlwithpath: [[nsbundle mainbundle] bundlepath]] error:&error]; also, i'm guessing file has been copied bundle since description indicates [filemanager fileexistsatpath:datapath] returning no (since nslog never executed.) can either check manually, or can ask nsfilemanager delete existing file before copying in newly selected file.
Comments
Post a Comment