2012-11-08 5 views
7

मैं इस समारोह है कि दस्तावेजों निर्देशिका में फ़ाइल के आकार देता है बनाया है, यह काम करता है, लेकिन मैं चेतावनी मिल है कि मैं ठीक करने के लिए, समारोह इच्छा:चेतावनी 'fileAttributesAtPath: traverseLink अब मान्य नहीं है: पहला ios में पदावनत 2.0

-(unsigned long long int)getFileSize:(NSString*)path 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *getFilePath = [documentsDirectory stringByAppendingPathComponent:path]; 

NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:getFilePath traverseLink:YES]; //*Warning 
unsigned long long int fileSize = 0; 
fileSize = [fileDictionary fileSize]; 

return fileSize; 
} 

* चेतावनी 'fileAttributesAtPath है: traverseLink: पहले आईओएस 2.0 में बहिष्कृत किया गया है'। इसका क्या अर्थ है और मैं इसे कैसे ठीक कर सकता हूं?

+1

संभावित डुप्लिकेट [? कैसे fileAttributesAtPath चेतावनी के साथ समस्याओं को हल करने] (http://stackoverflow.com/questions/9019353/how-to- हल-मुद्दों के साथ-fileattributesatpath चेतावनी) –

उत्तर

8

ज्यादातर मामलों में, जब आपको किसी बहिष्कृत विधि के बारे में कोई रिपोर्ट मिलती है, तो आप इसे संदर्भ दस्तावेज़ों में देखते हैं और यह आपको बताएगा कि किस प्रतिस्थापन का उपयोग करना है।

fileAttributesAtPath:traverseLink: Returns a dictionary that describes the POSIX attributes of the file specified at a given. (Deprecated in iOS 2.0. Use attributesOfItemAtPath:error: instead.)

तो attributesOfItemAtPath:error: का उपयोग करें।

NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:nil]; 

और पूरी तरह से है:

NSError *error = nil; 
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:getFilePath error:&error]; 
if (fileDictionary) { 
    // make use of attributes 
} else { 
    // handle error found in 'error' 
} 

संपादित करें:

यहाँ आसान तरीका है मामले में आप नहीं जानते कि पदावनत मतलब है, इसका मतलब है कि विधि या कक्षा अब अप्रचलित है। आपको एक समान क्रिया करने के लिए एक नई एपीआई का उपयोग करना चाहिए।

+0

तुम मुझे एक उदाहरण कैसे attributesOfItemAtPath उपयोग करने के लिए दे सकता है: त्रुटि: – DanM

+0

यह वस्तुतः तुम क्या प्रयोग कर रहे हैं के समान है। आप 'त्रुटि' के लिए 'nil' पास कर सकते हैं: 'पैरामीटर जल्दी से शुरू करने के लिए। – rmaddy

+1

'विशेषताएँफिट एटपैथ: त्रुटि: 'सिम्लिंक का समर्थन नहीं करता है। तो आपके कोड में प्रश्न से 'ट्रैवर्सलिंक: हाँ' के समान व्यवहार नहीं है। –

1

स्वीकृत उत्तर प्रश्न से traverseLink:YES को संभालने के लिए भूल गया।

एक बेहतर जवाब दोनों attributesOfItemAtPath:error: और stringByResolvingSymlinksInPath उपयोग कर रहा है:

NSString *fullPath = [getFilePath stringByResolvingSymlinksInPath]; 
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil]; 
की
+1

यह स्वीकृत उत्तर से काफी बेहतर है! –

संबंधित मुद्दे