2011-11-12 10 views
7

हल (धन्यवाद Regexident)फ़ाइल नाम NSString अंतरिक्ष

मैं एक ऐप्लिकेशन है जो एक कस्टम -(id)init init विधि करने के लिए एक पीडीएफ के फ़ाइल पथ गुजरता है में अनावश्यक% 20 कहते हैं। यह तालिका में जोड़ा जाता है और जब यह चयन किया जाता है, यह एक गैर विद्यमान फ़ाइल के लिए किसी और बयान कहता है:

- (void) gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index { 

NSLog (@"Selected theArgument=%d\n", index); 

UIViewController *viewController = [[[UIViewController alloc]init]autorelease]; 
{ 
    //if file is built-in, read from the bundle 
    if (index <= 3) 
    { 
     // first section is our build-in documents 
     NSString *fileURLs = [_documentIconsURLs objectAtIndex:index]; 
     NSLog(@"file url -%@", fileURLs); 
     viewController = [[[xSheetMusicViewController alloc]initWithContentURL:fileURLs]autorelease]; 
    } 
    //if file is external, read from URL 
    else 
    { 
     // second section is the contents of the Documents folder 
     NSString *fileURL = [_documentIconsURLs objectAtIndex:index]; 
     NSLog(@"file url -%@", fileURL); 
     NSString *path; 
     NSString *documentsDirectoryPath = [self applicationDocumentsDirectory]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileURL]; 


     if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectoryPath]) 
     { 
      viewController = [[[xSheetMusicViewController alloc]initWithDocumentURL:fileURL]autorelease]; 
     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure!"               message:@"The Selected File Does Not Exist" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles: nil]; 
      [alert show]; 
      [alert release];   
      return; 
     } 
    } 
[self.navigationController setNavigationBarHidden:YES animated:NO]; 
[UIView beginAnimations:@"animation" context:nil]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:YES]; 
[self.navigationController pushViewController:viewController animated:NO]; 
[UIView commitAnimations]; 
    } 
} 

तो जब भी मैं इसे के नाम पर कोई जगह नहीं के साथ एक दस्तावेज़ है, यह धक्का और inits। लेकिन जब फ़ाइल नाम में एक स्थान होता है, तो यह कहता है कि यह अस्तित्व में नहीं है। और जब मैं if-else विधि को हटा देता हूं, तो यह init है, लेकिन क्रैश क्योंकि फ़ाइल मौजूद नहीं है। मैंने नियमित पथ के साथ फ़ाइल पथ में% 20 को प्रतिस्थापित करने का प्रयास किया है, लेकिन विधि अन्य भाग को कॉल करना जारी रखती है।

तो, फ़ाइल पथ मानकीकृत और पठनीय नहीं है, या मेरी और विधि गलत है?

+0

आपके कोड में "% 20" और परिवर्तनीय नामकरण ("... URL") द्वारा निर्णय यह प्रतीत होता है कि आप फाइल पथों के साथ फ़ाइल यूआरएल (एन्कोड रिक्त स्थान "% 20" इत्यादि) को भ्रमित कर रहे हैं (रिक्त स्थान का उपयोग करता है, आदि)। – Regexident

+0

फ़ाइल यूआरएल दस्तावेज़ से एक सेब विधि में एक स्ट्रिंग में परिवर्तित किया गया है उदाहरण उदाहरण। हालांकि यूआरएल एक गैर-डी नियंत्रक को पास किया जाता है। तो, मैं इसे कैसे ठीक करूं? – CodaFi

उत्तर

16

अपना रास्ता एक प्रतिशत भाग निकले पथ स्ट्रिंग मैं यह कोशिश करता हूँ प्रतीत होता है के रूप में:

[fileURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 

और मैं fileURLString को fileURL नाम बदलने था स्पष्ट है कि यह एक ऐसे URL पथ युक्त एक NSString, नहीं एक है बनाने के लिए वास्तविक NSURL (जो आपका परिवर्तनीय नाम झूठा रूप से इंगित करेगा)।

लेकिन मैं उस कोड को चेक करूंगा जो _documentIconsURLs को पॉप्युलेट करता है, क्योंकि यह आपकी समस्या की उत्पत्ति की संभावना है।

+0

अच्छा प्रयास करें! लेकिन आपका पहले सुझाव सही था। मैं [filename पथ] के बजाय [filename lastpathcomponent] को बुला रहा था; हालांकि मैं आपको अभी भी वोट दूंगा। धन्यवाद। – CodaFi