Question

In Xcode 5.0.2 I am trying to recreate the example iOS SDK: Working with NSUserDefaults by Mr. Jeroen van Rijn:

enter image description here

I have checked in my complete project at the Github, for example here is ViewController.m where the image loading happens:

- (void)viewDidLoad
{
    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *firstName = [defaults objectForKey:@"firstName"];
    NSString *lastName = [defaults objectForKey:@"lastname"];
    int age = [defaults integerForKey:@"age"];
    NSString *ageString = [NSString stringWithFormat:@"%i",age];
    NSData *imageData = [defaults dataForKey:@"image"];
    UIImage *contactImage = [UIImage imageWithData:imageData];
    // Update the UI elements with the saved data
    _firstNameTextField.text = firstName;
    _lastNameTextField.text = lastName;
    _ageTextField.text = ageString;
    _contactImageView.image = contactImage;
    [super viewDidLoad];
}

- (IBAction)save:(id)sender
{
    // Hide the keyboard
    [_firstNameTextField resignFirstResponder];
    [_lastNameTextField resignFirstResponder];
    [_ageTextField resignFirstResponder];
    // Create strings and integer to store the text info
    NSString *firstName = [_firstNameTextField text];
    NSString *lastName  = [_lastNameTextField text];
    int age = [[_ageTextField text] integerValue];
    // Create instances of NSData
    UIImage *contactImage = _contactImageView.image;
    NSData *imageData = UIImageJPEGRepresentation(contactImage, 100);
    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:firstName forKey:@"firstName"];
    [defaults setObject:lastName forKey:@"lastname"];
    [defaults setInteger:age forKey:@"age"];
    [defaults setObject:imageData forKey:@"image"];
    [defaults synchronize];
    NSLog(@"Data saved");
}

Unfortunately, when running in iPhone simulator the app fails with:

2013-12-20 10:15:25.417 MyDefaults[2213:70b] -[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00
2013-12-20 10:15:25.426 MyDefaults[2213:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173a5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bd8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017d7903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0172a90b ___forwarding___ + 1019
    4   CoreFoundation                      0x0172a4ee _CF_forwarding_prep_0 + 14
    5   MyDefaults                          0x000028e6 -[ViewController viewDidLoad] + 598
    6   UIKit                               0x0033f318 -[UIViewController loadViewIfRequired] + 696
    7   UIKit                               0x0033f5b4 -[UIViewController view] + 35
    8   UIKit                               0x002679fd -[UIWindow addRootViewControllerViewIfPossible] + 66
    9   UIKit                               0x00267d97 -[UIWindow _setHidden:forced:] + 312
    10  UIKit                               0x0026802d -[UIWindow _orderFrontWithoutMakingKey] + 49
    11  UIKit                               0x0027289a -[UIWindow makeKeyAndVisible] + 65
    12  UIKit                               0x00225cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    13  UIKit                               0x0022a3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    14  UIKit                               0x0023e87c -[UIApplication handleEvent:withNewEvent:] + 3447
    15  UIKit                               0x0023ede9 -[UIApplication sendEvent:] + 85
    16  UIKit                               0x0022c025 _UIApplicationHandleEvent + 736
    17  GraphicsServices                    0x036e12f6 _PurpleEventCallback + 776
    18  GraphicsServices                    0x036e0e01 PurpleEventCallback + 46
    19  CoreFoundation                      0x016b5d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    20  CoreFoundation                      0x016b5a9b __CFRunLoopDoSource1 + 523
    21  CoreFoundation                      0x016e077c __CFRunLoopRun + 2156
    22  CoreFoundation                      0x016dfac3 CFRunLoopRunSpecific + 467
    23  CoreFoundation                      0x016df8db CFRunLoopRunInMode + 123
    24  UIKit                               0x00229add -[UIApplication _run] + 840
    25  UIKit                               0x0022bd3b UIApplicationMain + 1225
    26  MyDefaults                          0x0000376d main + 141
    27  libdyld.dylib                       0x01d7870d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Is it because there is no image data stored (as NSData with the key "image") in NSUserDefaults initially?

Was it helpful?

Solution

you're trying to set an image in UIView, not UIImageView :-) so t think your _contactImageView is not of Type UIImageView. look at the first line of your StackTrace:

[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00

OTHER TIPS

it might be crashing because NSUserDefauls storing null object initially. try storing some default image as NSData for the initial load. or may be you are setting in UIView instead of UIImageview.

Check before assign to imageview

    NSData *imageData = [defaults dataForKey:@"image"];
    if(imageData){
    UIImage *contactImage = [UIImage imageWithData:imageData];
    _contactImageView.image = contactImage;
}

Have a look at this line from the console log:

2013-12-20 10:15:25.417 MyDefaults[2213:70b] -[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00

I'm guessing the problem is caused at this line: _contactImageView.image = contactImage; because according to the error message, _contactImageView is actually an instance of UIView, not UIImageView. Set the class of that variable to be UIImageView and all should be ok!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top