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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:54:10+00:00 2026-05-25T18:54:10+00:00

I am trying to compare objects in Objective-C and was just wandering how, for

  • 0

I am trying to compare objects in Objective-C and was just wandering how, for example, two objects (which are instances of UIView) are compared that hold two NSStrings like so:

    #import <Foundation/Foundation.h>

    @interface Notebook : UIView
    {
        NSString *nameOfBook;
        NSString *colourOfBook;

    }

    @property (nonatomic, retain) NSString *nameOfBook;
    @property (nonatomic, retain) NSString *colourOfBook;

Let’s assume that I have two NSMutableArrays which hold several objects of the type Notebook. One is called reality and the other theory. Reality holds two notebooks with the nameOfBook @"Lectures" and @"Recipies", but colourOfBook are all empty. Theory holds three notebooks with the nameOfBook @"Lectures", @"Recipies", @"Zoo", and colourOfBook @"red", @"yellow", @"green".

I would like to compare the two arrays and adjust theory according to reality. In this case, it would mean to remove @"Zoo". I can’t simply replace theory with reality as I would loose all the colours stored in theory.

This is the code I’ve come up with:

    for (int i=0; i < [theory count]; i++) {

    Notebook *testedNotebook = [Notebook alloc];
    testedNotebook = [theory objectAtIndex:i];

    if ([reality indexOfObject:testedNotebook] == NSNotFound)
    {
        NSLog(@"Book is not found in reality - remove it from theory...");
        [theory removeObject:testedNotebook];
    }

    [testedNotebook release];

}

Now, my big question is how the objects will be compared. Ideally, I’d like to compare ONLY their NAMES regardless of the COLOUR. But I guess this is not how my code works right now. The entire objects are compared and the object in reality which holds @”Lectures” and @”” (no colour) cannot be the same as the object in theory which holds @”Lectures” and @”red”.

How could I achieve to compare objects according to one of their attributes only (in this case the name)?

  • 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-25T18:54:10+00:00Added an answer on May 25, 2026 at 6:54 pm

    If you read the documentation for indexOfObject:, you’d find that NSArray calls isEqual: for each object in the array. So, override isEqual for Notebook and implement your own comparison routine.

    Apropos of nothing, why are you allocating without initializing an instance of Notebook, overwriting it with an autoreleased instance, and subsequently releasing that? (Never mind you might be releasing it in your loop first!) You’re destined for a crash. And why are you removing objects from an array while you’re iterating through it?

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

Sidebar

Related Questions

I am trying to compare two vector objects, and return a single vector containing
So I'm trying to figure out how to compare two jQuery objects, to see
C# I'm trying to compare two different objects (I'm only comparing identical subfields). But
More specifically I want an interface to compare objects which can be only compared
I'm trying to write a function in three classes that will compare two lists
I am trying to compare objects in an object[] that are of a single
I am trying to compare two Document objects and try to make operation according
Im trying to compare these two chars but on win 32 Visual Studio 2008:
I am trying to compare two decimal values in Java script. I have two
I am trying to compare two text files and output the first string in

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.