[item setImageKey:key]; crashes app


I have reached the stage of the chapter immediately before adding the code to delete the old image.
The app crashes whether within the iphone simulator or my iPhone with:

2013-02-11 15:59:01.355 Homepwner[541:707] -[__NSCFType copyWithZone:]: unrecognized selector sent to instance 0x1eb660
2013-02-11 15:59:01.359 Homepwner[541:707] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSCFType copyWithZone:]: unrecognized selector sent to instance 0x1eb660’
*** First throw call stack:
(0x372e388f 0x34fe8259 0x372e6a9b 0x372e5915 0x37240650 0x34fe5ed5 0x34fed82f 0x98685 0x991f1 0x31222c5b 0x348fa079 0x3490b797 0x349398db 0x3493979b 0x348e5b19 0x34910317 0x3723d3fd 0x30fd2e07 0x30fd2dc3 0x30fd2da1 0x30fd2b11 0x30fd3449 0x30fd192b 0x30fd1319 0x30fb7695 0x30fb6f3b 0x339f922b 0x372b7523 0x372b74c5 0x372b6313 0x372394a5 0x3723936d 0x339f8439 0x30fe5cd5 0x969fd 0x96998)
terminate called throwing an exception(lldb)

Using break points I have established that the offending statement is :

[item setImageKey:key];

within -(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info

in DetailViewController.m

When I step into that statement the crash occurs after reaching:

@property (nonatomic, copy) NSString *imageKey;
in BNRItem.h

An unrecognized selector suggests to me that synthesizing the string has failed to generate a method with a selector of “setImageKey:”

Code follows:


#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
@property (nonatomic) NSString *itemName;
@property (nonatomic)    NSString *serialNumber;
@property (nonatomic)    int valueInDollars;
@property (nonatomic)    NSDate *dateCreated;
@property (nonatomic)    BNRItem *containedItem;
@property (nonatomic,readwrite, weak) BNRItem *container;
@property (nonatomic, copy) NSString *imageKey;

+(id) footerItem;

-(id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber;


top of BNRItem.m

#import "BNRItem.h"

@implementation BNRItem
@synthesize itemName, serialNumber, valueInDollars, dateCreated, containedItem, container, imageKey;

DetailViewController.m imagepicker protocol method:

-(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    NSLog(@"Entered didFinishPicking");
    //get image from info dir, put in image view. Then remove picker frokm screen
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    // create GUID, then make string with it, set item image key with it store in imageStore with image
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *key = (__bridge NSString *)newUniqueID;
    [item setImageKey:key];
    [[BNRImageStore sharedStore] setImage:image forKey:[item imageKey]];
    // need to manually release CF objects
    [imageView setImage:image];
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"Completed didFinishPicking");

There is only one copy of bnrItem.h & .m in finder. I have no clue whether I have done something wrong or whether the app build is failing, or something else. Can someone help?


Found solution. It was my code. The line before [item setImageKey:key]; was wrong.

Instead of what I coded:

NSString *key = (__bridge NSString *)newUniqueID;

it should have been:

NSString *key = (__bridge NSString *)newUniqueIDString;

I thought selector only referred to setImageKey: and not the arguments too.

My bad.