Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:56:06+00:00 2026-05-30T21:56:06+00:00

Consider the following two code samples: NSData *imgData = UIImagePNGRepresentation(imgFull); NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

  • 0

Consider the following two code samples:

    NSData *imgData = UIImagePNGRepresentation(imgFull);
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];   
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"itemImg_%i.png", timestamp]]; //add our image to the path
    [imgData writeToFile:fullPath atomically:YES];

and

NSData *imgData = UIImagePNGRepresentation(imgFull);
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];   
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"itemImg_%i.png", timestamp]]; //add our image to the path
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:imgData attributes:nil];

The second example requires an extra line of code and the initialization of an NSFileManager object, whereas the first example simply has the NSData object imgData write itself to a file. An additional advantage of the first example is that it can overwrite a pre-existing file that has the same name.

My question is: when creating new files, under what circumstances would you actually want to use NSFileManager and its method createFileAtPath:contents:attributes:?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:56:08+00:00Added an answer on May 30, 2026 at 9:56 pm

    The advantage of the NSFileManager method is the attributes field:

    A dictionary containing the attributes to associate with the new file. You can use these attributes to set the owner and group numbers, file permissions, and modification date. For a list of keys, see “File Attribute Keys.” If you specify nil for attributes, the file is created with a set of default attributes.

    This feature is unusual to use on iOS, but NSFileManager is much older than iOS.

    BTW, the extra line you’re describing almost never comes up in real code. Either you already have a fileManager variable that you were using for other reasons, or you combine the two lines into one:

    [[NSFileManager defaultManager] createFileAtPath:fullPath contents:imgData attributes:nil];
    

    And just one more. As you note:

    An additional advantage of the first example is that it can overwrite a pre-existing file that has the same name.

    Well, that’s an advantage or a disadvantage depending on what you want. If you mean “create this file, but don’t overwrite it if it already exists,” then the FM method is much more convenient. Maybe it’s an error to overwrite an existing file; this saves you the call to fileExistsAtPath:. Maybe you want to create an empty file if it’s not there, but leave it alone if it is. Simple: pass [NSData data] as the contents value.

    So which is better depends on what problem you’re solving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the two following similar code samples. One where clause. bool validFactory = fields
Consider the following source code. I have two classes CBar and CFoo. CFoo inherits
consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two segments of code in Java, Integer x=new Integer(100); Integer y=x;
Please consider the two following snippets of code: (function f() { var x; try
Consider the following two methods, written in pseudo code, that fetches a complex data
Consider the following two fragments of code: scala> def f1(x:Any) = x match {
Consider the two following Python code examples, which achieves the same but with significant
Consider the following code char bar[] = hello world \One\, two, 'three'; char *zSQL
Consider the following two blocks of Java psedo-code in a system that uses optimistic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.