Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7879983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:02:01+00:00 2026-06-03T04:02:01+00:00

The project is for iOS 5.0. I have set the automatic reference counting, anyhow

  • 0

The project is for iOS 5.0. I have set the automatic reference counting, anyhow I am not able to compile the code if I write a retain statement.

I wrote a switch in a function, because I don’t want to create if and a lot of if else branches.

My code portion it is relative simple and seems correct, but only seems to be:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];

switch (selectedRow) {
        
    case 0://English
        [userDefaults setObject:@"English" forKey:@"language"];
        break;
    case 1:// Deutsch 
        [userDefaults setObject:@"German" forKey:@"language"];   
        break;
    case 2://Français
        [userDefaults setObject:@"French" forKey:@"language"];         
        break;
    case 3://Italiano
        [userDefaults setObject:@"Italian" forKey:@"language"];       
        break;
    case 4://Español
        [userDefaults setObject:@"Spanish" forKey:@"language"];
        break;
        
    default:
        break;
}
// flush:
[userDefaults synchronize];

at runtime I got this message:
*** -[MyClass retain]: message sent to deallocated instance 0x6e78580
and it will shown the XCode as the breakline at some case branch’s [userDefaults setObject line.

Somewhere I saw a compile when compile the Switch it will create a class. But I am not sure in which language: Java, C#, or Obj-C and because I left my class and I am executing the switch class it will dealloc the userDefaults variable and that is the reason why is deallocated the userDefaults object. Now I am not sure how to write this switch to get working and looks like professional. I wouldn’t like to create in each case the userDefaults variable and flush there. The only one solution is to write this switch to if-else?

this is working:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    
    switch (selectedRow) {
            
        case 0://English
        {
            [userDefaults setObject:@"English" forKey:@"language"];
        }
            break;
        case 1:// Deutsch 
        {
            [userDefaults setObject:@"German" forKey:@"language"];   
        }
            break;
        case 2://Français
        {
            [userDefaults setObject:@"French" forKey:@"language"];         
        }
            break;
        case 3://Italiano
        {
            [userDefaults setObject:@"Italian" forKey:@"language"];       
        }
            break;
        case 4://Español
        {
            [userDefaults setObject:@"Spanish" forKey:@"language"];
        }
            break;
            
        default:
            break;
    }
    // flush:
    [userDefaults synchronize];

why?

  • not sure why It does worked, but than it crashed again.
    I have moved the variable inside the code:

    switch (selectedRow) {

          case 0://English
          {
              NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
              [userDefaults setObject:@"English" forKey:@"language"];
              // flush:
              [userDefaults synchronize];
          }
              break;
          case 1:// Deutsch 
          {
              NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
              [userDefaults setObject:@"German" forKey:@"language"]; 
              // flush:
              [userDefaults synchronize];  
          }
              break;
          case 2://Français
          {
              NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
              [userDefaults setObject:@"French" forKey:@"language"]; 
              // flush:
              [userDefaults synchronize];        
          }
              break;
          case 3://Italiano
          {
              NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
              [userDefaults setObject:@"Italian" forKey:@"language"];   
              // flush:
              [userDefaults synchronize];    
          }
              break;
          case 4://Español
          {
              NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
              [userDefaults setObject:@"Spanish" forKey:@"language"];
              // flush:
              [userDefaults synchronize];
          }
              break;
    
          default:
              break;
      }
    

This crashed again and I came here to check the answers

I saw the suggestion below:

NSArray * languages = [NSArray arrayWithObjects: @"English", @"German", @"French",@"Italian", @"Spanish", nil];
    NSString * selectedLanguage = [languages objectAtIndex: selectedRow];
    [[NSUserDefaults standardUserDefaults] setObject: selectedLanguage forKey:@"language"];

this crash at the same line:

[[NSUserDefaults standardUserDefaults] setObject: selectedLanguage forKey:@"language"];
  • what is wrong? can’t believe it.
    *** -[MyClass retain]: message sent to deallocated instance 0x6b532c0

the MyClass is a UIViewContoller. I do a language selection and press some buttons to navigate 1 or 2 screen and I am coming back and do the language selection again and it will crash, but not always. But when it crashing it always at the same line and always the same error message.

What has the

userDefaults setObject 

with navigations?

#—————————————————
Got the real problem: than easy to find the solution
#—————————————————

I wrote the: userDefaults setObject
Also I wrote : this is an ARC project and it fails at MyClass retain, which can’t be my code .

The code is a language selection with an action sheet.
If I do a search I will have the next link:
EXC_BAD_ACCESS invoking a block

  • from there I see I should have a copy generated somewhere instead of a retain.
    The question is is why is needed the retain at setObject?
    Well, because it was added a change listener…

//[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"language" options:NSKeyValueObservingOptionNew context:NULL];

the code removed and the content of the callback moved after the switch code it solves the problem – not needed a retain to do a callback

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:02:03+00:00Added an answer on June 3, 2026 at 4:02 am

    Not an answer to your question, but how about ditching the switch and using:

    NSArray * languages = [NSArray arrayWithObjects: @"English", @"German", @"French",
                                                     @"Italian", @"Spanish", nil];
    NSString * selectedLanguage = [languages objectAtIndex: selectedRow];
    [[NSUserDefaults standardUserDefaults] setObject: selectedLanguage forKey:@"language"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In X code, If I create a new project with automatic reference counting (ARC),
I have a Jenkins job set up that builds my iOS Xcode project and
I have code like this: public bool Set(IEnumerable<WhiteForest.Common.Entities.Projections.RequestProjection> requests) { var documentSession = _documentStore.OpenSession();
Overview I have an iOS project which contains 2 navigation controllers as shown in
I'm working on an iOS project that has to work from iOS4. I have
Now I have a Xcode project which is built for iOS 5, but now
I am new to iOS projects. In my current project, I have to process
I have my current base sdk set to iOS 4.2. I have this method:
So recently I have been trying to set up a Vim-based iOS workflow. I
I have a several XIB files in my iOS project for various custom table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.