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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:05:01+00:00 2026-05-16T04:05:01+00:00

In languages like C++ and C# when you create a contain such as a

  • 0

In languages like C++ and C# when you create a contain such as a std::vector or a C# list you explicitly declare the container type when you create it:

C++:

std::vector<MyObject>

C#:

List<MyObject> list = new List<MyObject>();

Looking at the code above, I know immediately that these containers can only contain objects of type MyObject and the compiler will complain if I try to add an object that isn’t off this type.

Since Objective-C is a dynamic language, we don’t have the privilege of the compiler warning us about this (because it is a perfectly valid but potentially dangerous thing to do):

Objective-C:

NSDictionary *dict = [[NSDictionary alloc]init];
[dict setValue:[[SomeClass alloc]init] forKey:@"someClass"];
[dict setValue:[[NSMutableString alloc]init] forKey:@"mutableString"];
BOOL classIsSomeClass = [[dict objectForKey:@"someClass"] isKindOfClass:[SomeClass class]];

Instead something like an NSDictionary or NSArray will store and accept objects of any type that inherits from NSObject. I find this in itself very flexible but I cannot really be sure of the object type in the container I can only really know at runtime whereas with c++ or c# I know this at compile time and just by looking at the code.

Should I be validating the contents of the containers when adding, using and removing objects for container classes (NSArray, NSSet, NSDictionary, etc) from Apple’s Foundation Framework? Or is this okay in all circumstances and will verification hurt performance much?:

NSDictionary *dict = [[NSDictionary alloc]init];
[dict objectForKey:@"someKey"];    // return nil?
  • 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-16T04:05:01+00:00Added an answer on May 16, 2026 at 4:05 am

    Objective-C’s dynamic messaging is much more like dynamic languages such as Python or Ruby. In these languages, the standard paradigm is often known as “duck typing”. In other words, if an object instance quacks like a duck (i.e. responds to the message you’re sending), it’s a duck. In Objective-C, methods can be added at run time by a number of mechanisms, outside of the object inheritance hierarchy. So, it’s much more common to ask whether an instance responds to a particular selector:

    if([obj respondsToSelector:@selector(myMethod)]) {
      [obj myMethod];
    }
    

    than to ask whether obj belongs to a certain class’ hierarchy.

    For the most part, Objective-C developers don’t do this check unless they’re getting object instances from “unknown” modules. Instead, we rely heavily on compiler warnings (the Objective-C compiler will warn about sending a message to a type that it isn’t sure can receive that message) and unit testing. In this case, unit test to confirm that the correct objects are going into the collection and that you get the expected types out of the collection would probably go a long way to allaying your fears.

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

Sidebar

Related Questions

I would like to create data visualizations in desktop apps, using frameworks, languages and
Languages like C and even C# (which technically doesn't have a preprocessor) allow you
In languages like Java and C#, strings are immutable and it can be computationally
In OOP languages like C# or VB.NET, if I make the properties or methods
An idiom commonly used in OO languages like Python and Ruby is instantiating an
See: Understanding Pointers In many C flavoured languages, and some older languages like Fortran,
Why don't we see C-like languages that allow for callables with polymorphism in the
This question would probably apply equally as well to other languages with C-like multi-line
I like to study languages outside my comfort zone, but I've had a hard
It seems like in most mainstream programming languages, returning multiple values from a function

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.