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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:45:14+00:00 2026-05-12T08:45:14+00:00

This isn’t a style question. Its more about the proper use of the language

  • 0

This isn’t a style question. Its more about the proper use of the language itself. I’m am fairly new to programming and I’m totally new to Objective-C and Cocoa but after reading about the language and looking through some sample code some usage patterns continue to pop up that do not make sense to me. Or rather they seem non optimal in my mind. I am hoping you all can help educate me in the proper uses of some of these language constructs.

interfaces vs protocols

I understand the concepts of interfaces and implementation as they relate to abstraction. However the dominant pattern I see in Objective-C sample code is the following…

@interface Foo : NSObject
{
  some ivars decls
}
some methods decls
@end

And then client code in the form of…

Foo* f = [[Foo alloc] init];

This seems odd to me. Foo seems to be an implementation in my mind (irregardless of the unfortunate naming of the @interface keyword). Interfaces in my mind do not expose instance variables. A client of this class should not be exposed to the details of my implementation.

Objective-C does have the @protocol keyword which in my mind is more of an interface than the @interface keyword. It allows you to define the methods and/or properties and that’s it. No implementation. No instance variables. Just interface.

@protocol Foo <NSObject>
  some methods
  maybe some properties
@end

@interface FooProvider : NSObject
  + (FooProvider*) sharedProvider;
  - (id<Foo>) fooWithBlahBlah;
@end

So the client code would take the form of…

id<Foo> f = [[FooProvider sharedProvider] fooWithBlahBlah];
[f whatever];

This seems (at least to me) to be a more abstract usage of the language and isolates clients from implementation details they aught not to be dependent on. My question is which should I be striving to follow. I understand that there may be situations where one is preferable over the other but in general terms should one be the norm and one be the exception?

Some guidance would be appreciated.

Thanks,
Roman

  • 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-12T08:45:14+00:00Added an answer on May 12, 2026 at 8:45 am

    I can see where you are coming from in regards to the naming of the @interface keyword, but Objective-C uses the two keywords @interface and @implementation to differentiate between the class declaration (interface) and its definition (implementation). The @interface portion is usually placed in the class header file, and the @implementation portion goes in the source file. I agree with you 100% that the internal instance variables really shouldn’t appear in the header file. I’m not sure what led to that decision, but my best guess is that it has to do with object inheritance. When you inherit from a parent class:

    #import "ParentClass.h"
    @interface MyClass : ParentClass
    ...
    

    You also inherit all of the instance variables, which would be very difficult for the compiler to figure out unless those variables are declared in the class header.

    You are also correct about the usage of @protocol, in that it essentially defines an interface that a class must adhere to. Protocols appear to be Objective-C’s answer to multiple inheritance.

    In my experience with Objective-C, protocols are not used very often. They have their place, but the majority of the code uses the basic @interface and @implementation keywords to define a class, and protocols are only used when some extra functionality is required that doesn’t quite belong in the class hierarchy, but still needs to be shared across multiple classes.

    If you’re looking for guidance, I would say:

    1. Accept the naming of the @interface and @implementation keywords, even if it doesn’t quite jive with what you expect. When programming in Objective-C, drink the Kool-Aid and see those keywords the way the language designers intended.
    2. Use protocols when they make sense in your code, but don’t overuse them in an attempt to make Objective-C behave more like another language. It might work, but other people will cringe when they see your code, and it will make collaboration difficult.
    3. Keep asking such good questions. You’ve brought up some very valid points!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This isn't a classic programming language question, but it's keeping me busy so I
This isn't a programming question but it's about the SDKs and the IDE. I've
This isn't strictly a programming question, but I'm asking it here because it's certainly
This isn't the best programming question but lets face it, the server fault guys
This isn't a question on proper coding practice, I'm just working through the semantics.
This isn't exactly a programming question exactly. I just want to know what your
This isn't a direct question, but more a question on how I can improve
This isn't a question that has a black/white yes/no answer, this is more a
This isn't necessarily a programming question, but I've hit a performance bottleneck with disk
This isn't strictly a programming question, but hopefully someone here has the knowledge to

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.