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 8266125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:02:44+00:00 2026-06-08T05:02:44+00:00

I’m a newbie in objective-c. I got a problem in saving a string from

  • 0

I’m a newbie in objective-c.
I got a problem in saving a string from UITextField.

I declared a string in property and synthesize it which named “familyName” to store user’s family name.
Then I want to save the familyName when the user press a button.

-(IBAction)textFieldDidBeginEditing:(UITextField *)textField;
{}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
familyName = inputFamilyName.text;  
NSLog(@"the user's family name is %@", familyName); //it works here.

if (inputFamilyName == textField) {
    [inputFamilyName resignFirstResponder];
}
return YES;
}

-(IBAction)goToNameWheel:(id)sender{

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:familyName forKey:@"family"];
[userDefault synchronize];
}

It crashes when it runs the saving code, it said “Thread 1:EXC_BAD_ACCESS (code=1, address = 0x……”

After that, I try another test to see what happens to familyName, then I wrote:

-(IBAction)test:(id)sender{
NSLog(@"1.the family name is %@", inputFamilyName.text); // This works...
NSLog(@"2.the family name is %@", familyName);} // This crashes...

Finally… I try to retain familyName in textFieldDidBeginEditing:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
familyName = inputFamilyName.text;  
NSLog(@"the user's family name is %@", familyName); //it works here.

if (inputFamilyName == textField) {
    [inputFamilyName resignFirstResponder];
}
[familyName retain]; // just added randomly...
return YES;
}

Then everything works fine, I can save the familyName, print it..etc..
My question is…what exactly happens here, why do I have to retain it? Anything wrong with my original code?
Thx for your time!

  • 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-08T05:02:46+00:00Added an answer on June 8, 2026 at 5:02 am

    When you do

    familyName = inputFamilyName.text; 
    

    You’re not actually using the property. You’re using an instance variable with the same name as your property, but without the memory management stuff.

    What you should be doing is

    self.familyName = inputFamilyName.text; 
    

    Then the property’s retain will kick in, and you won’t have to manually retain.

    To make it harder to make the mistake, you could change your @synthesize statement to be:

    @synthesize familyName = _familyName;
    

    If you do that, then it means that the property called familyName will have an instance variable called _familyName backing it — which means it’s a lot harder to accidentally type the wrong one accidentally, thus avoiding this mistake.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.