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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:25:45+00:00 2026-05-20T07:25:45+00:00

I am having a problem with arrays, I want an object to be picked

  • 0

I am having a problem with arrays, I want an object to be picked randomly from the array,
and then remove it and remove other objects that are specified in the “if” statement.

What I did..

in the .h

NSMutableArray *squares;
int s;
NSString *randomN;

Next, in the .m

Create a new array:

-(void) array{
    squares = [[NSMutableArray alloc] arrayWithObjects: @"a", @"b", @"c", nil];
}

and then choose a random object, if properties of the “if” met, remove the object from the array, do the while loop again.

-(void) start{

    s=5;

    while (s > 0){
     //I even tried it without the next line..
     randomN = nil;

     //Also tried the next line without ' % [squares count'
     randomN = [squares objectAtIndex:arc4random() % [squares count]];

     if (randomN == @"a"){
         [squares removeObject: @"a"];
         [squares removeObject: @"b"];
        s = s - 1;
        }

     if (randomN == @"b"){
         [squares removeObject: @"b"];
         [squares removeObject: @"c"];
        s = s - 1;
        }

     if (randomN == @"c"){
         [squares removeObject: @"c"];
        s = s - 1;
        }

     else {
     s=0;
     }
}
}

When I run the app, the app stops and quits as soon as the loop starts.

Can you please help me?

  • 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-20T07:25:46+00:00Added an answer on May 20, 2026 at 7:25 am

    Their are a few issues that are probably tripping you up:

    You’re initializing an already allocated array with a convenience constructor, you should pick one of the alloc/init pair or the convenience constructor by itself:

    [[NSMutableArray alloc] initWithObjects:...]
    

    or:

    [NSMutableArray arrayWithObjects:...]
    

    Your remove lines are attempting to remove string literals. While your array contains string instances that contain the same values as the strings you’re trying to remove, they’re not the same exact instances of the string. You need to use [stringVar isEqualToString:otherStringVar] to compare the values instead of their references:

    if ([randomN isEqualToString:@"a"])
    

    instead of:

    if (randomN == @"a")
    

    Also, your else statement will fire every time for similar reasons as the second problem. Even if you use the correct string comparisons, your logic is probably off if you’re trying to only execute one of those 4 blocks of code. To accomplish that, each of the ifs after the first needs an else, like so:

    if (/* test 1 */) {
    }
    else if (/* test 2 */) {
    }
    else if (/* test 3 */) {
    }
    else {
        // chained else's make only one block able to execute
    }
    

    instead of:

    if (/* test 1 */) {
    }
    if (/* test 2 */) {
    }
    if (/* test 3 */) {
    }
    else {
        // this else only applies to the LAST if!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem I'm having is that a Vehicle object is not being deleted from
I've been having a really weird problem. I have a mutable array that is
So Im having trouble with a problem. Im givin that a is an array
I have a problem here in JSTL, I have an array object coming from
I want to have singleton class that its object is not statically created. having
Is there a way to pick a random object from an array of objects?
I am having some problems extracting the data I want from a SimpleXMLElement object.
I'm having a bit of a problem using FieldOffset correctly with arrays. The code
Hello I'm having problems sending arrays, structs and arrays of structs from PHP to
I'am having problem in accessing json array elements. Below is the response 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.