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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:44:14+00:00 2026-05-30T03:44:14+00:00

I am using objects that conform to an obj c protocol to represent a

  • 0

I am using objects that conform to an obj c protocol to represent a calculation. There are constants [1], variables [x], and operations [a+b]. All of these objects are immutable (the variable contains a key (to a dictionary) and a default value (to be used if the dictionary doesn’t contain a value for the key)).

Since the objects are immutable, I would like to simplify the calculation if possible when an object is initialized. (Note: The variable dictionary is allowed to continuously change, so I am never able to simplify based on the value of a variable, only constants).

For example, a very common use will be to increment a variable by 1: [[x]+1]. When these are nested, I would like to return [[x]+2] from init instead of [[[x]+1]+1] so that that part of the calculation only has to be done once. So far so good.

When both arguments are constants (e.g. [2+2]) it makes sense to just return [4]. But ARC complains about this because [4] is a different class that adheres to the same protocol.

It got me thinking about whether what I want to do is good form. I would normally make them all inherit from a single class (class cluster), but I want to use NSNumber (extended via category) for the constants.

I could move the simplification logic to another method, but that would mean having to allocate extra objects fairly often, and I would have to remember to always call it: [[[MyClass alloc]init]simplifiedCalc].

My options are:

  1. Just silence the compiler (and write a thorough note in the
    documentation)
  2. Create and call a separate “Simplified” method which
    returns another object (creating extra objects)
  3. Do optimizations which return the same object
    in init(e.g. [[[x]+1]+1] -> [[x]+2]), but require a “simplified”
    method for the [2+2]=[4] case.
  4. ???

Any thoughts or advice is appreciated.

  • 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-30T03:44:16+00:00Added an answer on May 30, 2026 at 3:44 am

    Instead of doing this work in init, maybe you should just have a convenience method that sets up the un-simplified expression, then goes through and simplifies it and returns the simplified one. Or you could do this as a method on the object itself, e.g.

    @protocol Calculation
    - (id<Calculation>)simplifiedExpression;
    @end
    

    This could return self if it’s already simplified, or construct a new simplified version if not. That way you can still alloc/init your objects as you’re doing now, and then simplify them afterwards. You could even make a convenience constructor that does both at once:

    + (id<Calculation>)simplifiedCalculationWithInput:(id)input;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I implement objects that I want to compare using the IEquatable<T> interface :
Lets say I have a million people objects that, when evaluated using person1.Matches(person2); return
I have a class that serializes a set of objects (using XML serialization) that
I have a web application that adds contextual information to XmlHttpRequest objects using the
I'm using python 3.1.1. I know that I can create byte objects using the
Where I work, people mostly think that objects are best initialised using C++-style construction
I've discovered recently that when using the Session or Application objects within an ASP.net
I'm on a project using CORBA to read the data of objects that could
When adding objects to an NSArray using initWithObjects can anyone confirm for me that
I'm using a COM object that has a function called GetImage. http://www.pdf-tools.com/asp/products.asp?name=P2IA When I

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.