Search code examples
macosassets

NSDataAsset data requirements


So the new data assets class allows reading 'data', but there appears to be some content restrictions? I have a file like so test.csv (collapsed here for viewing as a single line but actually 1-line per string):

½f ⅛m 1½f ¼m 2½f ⅜m 3½f ½m 4½f ⅝m 5½f ¾m 6½f ⅞m 7½f 1m 1m ½f 1⅛m 1m 1½f 1¼m 1m 2½f 1⅜m 1m 3½f 1½m 1m 4½f 1⅝m 1m 5½f 1¾m 1m 6½f 1⅞m 1m 7½f 2m

The file I maintain via TextEdit, and read such like so (NSData category but various methods can return different inherent - to the 'type', data):

+ (id)assetWithName:(NSString *)name
{
    NSDataAsset * asset = [[[NSDataAsset alloc] initWithName:name] autorelease];
    NSData * data = [[[NSData alloc] initWithData:asset.data] autorelease];

    NSAssert(data.length > 0, @"'%@' has zero data.length ", name);// Yoink

    NSString * string = [[[NSString alloc] initWithData:data
                                               encoding:NSUTF8StringEncoding] autorelease];
    NSString * type = asset.typeIdentifier;

    //  default to Ascii when UTF8 doesn't work
    if (!string.length)
    {
        string = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
    }

    if ([@"public.xml" isEqualToString:type])
    {
        return [self assetBook:string];
    }
    else
    if ([@"public.comma-separated-values-text" isEqualToString:type])
    {
        return [self assetCSVs:string];
    }
    else
    {
        NSLog(@"'%@' has unknown asset type %@",name,type);
        return [self assetCSVs:string];
    }
}

All was dandy, until I altered the text.

I hated having the fractions like "½" stored as "1/2" so I took to replacing these by the single character equivalents.

However, once doing so, the assert fires, so the class appears to not like my edits. I've taken to in-lining the file as a single string (above) - yuck, which I pull apart (-componentsSeparatedByString:) to an array, but perhaps someone else can tell me what's wrong with the approach?

Overall I favor assets' data obfuscation but it appears to have limits.


Solution

  • Not sure why, but naming my text files with a ".dat" type works.

    The asset type reported is "dyn.ah62d4rv4ge80k2py" which I'm guessing is like the old Finder first few file bytes contents but this type was the same for several .dat files of differing content so it's probably related the the method used.