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

  • Home
  • SEARCH
  • 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 6177493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:17:11+00:00 2026-05-24T00:17:11+00:00

Can I not do this in objective c? @interface Foo : NSObject { int

  • 0

Can I not do this in objective c?

@interface Foo : NSObject {
     int apple;
     int banana;         
}

@property int fruitCount;
@end

@implementation Foo
@synthesize fruitCount; //without this compiler errors when trying to access fruitCount

-(int)getFruitCount {
      return apple + banana;
}

-(void)setFruitCount:(int)value {
      apple = value / 2;
      banana = value / 2;
}

@end

I am using the class like this:

Foo *foo = [[Foo alloc] init];
foo.fruitCount = 7;

However my getter and setter’s are not getting called. If I instead write:

 @property (getter=getFruitCount, setter=setFruitCount:) int fruitCount;

My getter gets called however the setter still doesn’t get called. What am I missing?

  • 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-05-24T00:17:11+00:00Added an answer on May 24, 2026 at 12:17 am

    Your syntax is a bit off…
    to define your own implementation for property accessors in your example, use the following:

    @implementation Foo
    @dynamic fruitCount;
    
    // ⚠ NOTE that below has NOT "getFruitCount" name.
    
    - (int) fruitCount {
       return apple + banana;
    }
    - (void) setFruitCount :(int)value {
          apple = value / 2;
          banana = value / 2;
    }
    
    @end
    

    Using @synthesize tells the compiler to make default accessors, which you obviously don’t want in this case. @dynamic indicates to the compiler that you will write them. There used to be a good example in Apple’s documentation, but it somehow got destroyed in their 4.0 SDK update… Hope that helps!

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

Sidebar

Related Questions

Objective-C has directives like: @interface @implementation @end @protocol @property @synthesize I think of these
foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
In the Modern Objective-C runtime, you can do something like this: @interface MyClass :
I am using an Objective c class, a subclass of NSObject. This class cannot
Any reasons why this can not be standard behavior of free() ? multiple pointers
I can not get to make this comparison in this simple code error ..
there's this interesting problem i can not solve myself. I will be very glad,
I am not professional programmer so i can not be sure about this.How many
I think this is a simple question, but I can not find the answer
This maybe a stupid question, but as I can not easily undo my change

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.