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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:42:04+00:00 2026-05-28T19:42:04+00:00

Say I create a class and a protocol like this: @protocol MyProtocol @required –

  • 0

Say I create a class and a protocol like this:

@protocol MyProtocol
@required
- (void) doSomething;
@end

@interface MyClass id<MyProtocol>
@property int aNumber;
@end

@implementation MyClass
@synthesize aNumber = aNumber_;
- (void) doSomething { aNumber_++; }
@end

I can obviously pass instances of MyClass around by treating them as id<MyProtocol>s. I can’t access aNumber though, if I’m dealing with something declared as id<MyProtocol>. That’s expected.

I needed to do this recently. I wanted to keep using the protocol to refer to instances of my class (runtime determined) but I also wanted to access the classes properties too. I found that I could do this:

@protocol MyProtocol
@required
- (void) doSomething;
- (int) aNumber;
@end

And now anywhere I have an instance of an id<MyProtocol> I can also call [instance aNumber], without having to code up accessors directly – the synthesise call effectively implements that part of protocol for me. I could also add the setter as well, if I like.

Have I stumbled upon an intentional feature, or is this sort of thing a Bad Idea which will end up causing me problems down the line?

Thanks

  • 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-28T19:42:05+00:00Added an answer on May 28, 2026 at 7:42 pm

    Completely intentional.

    When you write:

    @property int aNumber;
    

    this is really just shorthand for:

    - (int) aNumber;
    - (void) setaNumber:(int)value;
    

    and the dot notation, object.property, is just shorthand for calling one of these two methods (depending on whether you are reading from, or assigning to, the property).

    The @synthesize statement just writes the two methods for you.

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

Sidebar

Related Questions

Say I've got a domain model created from C# classes like this: public class
lets say I have a class like so. var foodData = Class.create({ initialize: function()
I would like to create a class that effectively does this (mixing a little
I have some interface, and a class implementing this interface, say: interface IMyInterface {
Let's say I create a class with a property: public class User { private
Let's say I create an instance of a class and want to assign some
I want to create a Class, say MyDiv , which inherits from the original
I have a question regarding dependency injection. say i want to create a class
I want to create an object, let's say a Pie. class Pie def initialize(name,
Say I have a structure like: class SomeObject Public Name as String Public Created

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.