Frage

Currently I'm using a method to set the data protection attribute, that looks rather awkward to me. I'm wondering if there is a more direct way, instead of dealing with NSString, and NSMutableDictionary? That's what I'm currently using (I know it could be optimized by "using" statements, but that's not what I'm after here):

public static void SetDataProtectionFor ( string sFileToProtect )
        {
            NSError oError = null;
            NSMutableDictionary oDic = null;

            try
            {

                oDic = new NSMutableDictionary ();
                oDic.Add ( new NSString ("NSFileProtectionKey"), new NSString ("NSFileProtectionComplete") );


                NSFileManager.DefaultManager.SetAttributes ( oDic, sFileToProtect, out oError );
                if ( oError != null )
                {
                    throw new DataProtectionGoneMissingException ();
                }
            }
            catch ( Exception ex )
            {
                throw new DataProtectionGoneMissingException ();
            }
            finally
            {
                if ( oDic != null )
                {
                    oDic.Dispose ();
                    oDic = null;
                }

            }
        }
War es hilfreich?

Lösung

Right now ? not much. That's the Objective-C way and the MonoTouch (generated) bindings allows you to follow the same pattern.

You could avoid creating the NSString and use the ones provided by MonoTouch, e.g. 'NSFileManager.FileProtectionComplete and NSFileManager.FileProtectionKey. That would avoid creating the two NSString instances many times (if your method is called multiple times, MonoTouch versions are lazily created and cached) and remove the possibility of typos.

So using C#3 syntax this would become:

NSMutableDictionary oDic = new NSMutableDictionary () {
    { NSFileManager.FileProtectionKey, NSFileManager.FileProtectionComplete }
};

Also we try to provide more C#-ish API to do the same job as NSDictionary. OTOH since there's a lot of API using NSDictionary for options we only have them (so far) for the most common ones.

note: please feel free to file enhancement bug reports when you find cases where the API could be better

E.g. based on similar cases (that were done before) this would/should ideally look like this:

var attr = new NSFileManagerAttribute ();
attr.ProtectionKey = NSFileManager.ProtectionComplete; // enum version
NSFileManager.DefaultManager.SetFileAttribute (sFileToProtect, attr);

It won't save much lines of code (at least for a single value) but it would make the source looks a lot better (IMO) and easier to use with code completion and avoid typos (over strings).

Andere Tipps

You could use SetObject rather than Add on the NSMutableDictionary to avoid having to create new NSString's, but it doesn't really make that much of a difference to what you already have e.g.

var oDic = new NSMutableDictionary ();
oDic.SetObject ("NSFileProtectionComplete", "NSFileProtectionKey");

As far as I know, this is the only way of setting file attributes.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top