0

Here is the thing. At a point in my app, I want to show on a CollectionView the photos stored in Core Data (yes, I use External Storage). To get only the photos in my Entity I do the following:

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SmallThing"];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"smallphoto", nil]];
self.photos = [[managedObjectContext executeFetchRequest:fetchRequest error:nil]mutableCopy];

That seemed to work fine, it returns the data for the photos and store it on the array. Then I created a method to convert it to UIImage so I could store on another array as following:

- (NSMutableArray *)convertDataToImage:(UIImage *)image {
for (NSData *data in self.photos) {
    image = [[UIImage alloc] initWithData:data];
    [self.images addObject:image];
}
return self.images;
}

When I get to the line image = [[UIImage alloc] initWithData:data it crashes and I get the following error:

[16697:2367280] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSKnownKeysDictionary1 length]: unrecognized selector sent to instance 0x7fae0c117cc0'

I don't know what else I can do. Any ideas ?

Thanks in advance

2
  • This means data is really an NSKnownKeysDictionary1, not an NSData object. self.photos is not an array of NSData like your code is assuming. Commented Aug 18, 2015 at 23:44
  • Yes, sir, you were quite right. Now I'm having problem on adding the image to the images array. For some reason it's returning null Commented Aug 19, 2015 at 0:42

1 Answer 1

1

You are explicitly telling the fetch request to return dictionaries with the line:

[fetchRequest setResultType:NSDictionaryResultType];

Which means the array returned by executeFetchRequest... contains NSDictionary objects, with key-value pairs corresponding to the properties you specified.

Your code in convertDataToImage should be along the lines of:

for (NSDictionary *dictionary in self.photos) {
    NSData *data = dictionary[@"smallphoto"];
    ...
}

If you specified a different property to fetch other than smallphoto you would of course substitute that string there as appropriate.

Sign up to request clarification or add additional context in comments.

4 Comments

Very well! The error disappeared, but the image is returning nil therefore the images array is nil as well.
I mean the array is returning nil. It's like the images are not being added to it
@LeonardoRangel I don't understand what you mean by that. Which value is nil and when? What keys & values do the NSDictionarys in the fetched results have?
I figured it out, it was a stupid thing I was missing, I never initialized the images array so it was always nill. It's working just fine now. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.