Skip to content
This repository was archived by the owner on Sep 14, 2026. It is now read-only.
This repository was archived by the owner on Sep 14, 2026. It is now read-only.

nullable e crash fix #31

Description

@souzainf3

Hi, any suggestions to fix crash and swift usage.

  1. Add on 'objectWithContentsOfFile' the 'nullable' flag to Swift optional compatibility.
  2. Try|catch on 'unarchiveObjectWithData:'.

Bellow the implementation of suggestions.

+ (nullable instancetype)objectWithContentsOfFile:(NSString *)filePath
{
    //load the file
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    //attempt to deserialise data as a plist
    id object = nil;
    if (data)
    {
        NSPropertyListFormat format;
        object = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:NULL];
       
		//success?
		if (object)
		{
			//check if object is an NSCoded unarchive
			if ([object respondsToSelector:@selector(objectForKeyedSubscript:)] && ((NSDictionary *)object)[@"$archiver"])
			{
                @try {

                    object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
                }
                @catch (NSException* exception) {
                    NSLog(exception);
                    // delete corrupted archive
                    // initialize libraryDat from scratch
                }
			}
		}
		else
		{
			//return raw data
			object = data;
		}
    }
    
	//return object
	return object;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions