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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:52:09+00:00 2026-05-15T06:52:09+00:00

All examples I find on shuffling arrays are for NSMutableArrays. Copying an NSArray to

  • 0

All examples I find on shuffling arrays are for NSMutableArrays. Copying an NSArray to an NSMutable array, shuffling, then copying back always crashes the application. Is it even possible to shuffle an NSArray?

Anything in objective-c similar to Collections.shuffle() (Java)?

Edit

static NSUInteger random_below(NSUInteger n) {
NSUInteger m = 1;

do {
    m <<= 1;
} while(m < n);

NSUInteger ret;

do {
    ret = random() % m;
} while(ret >= n);

return ret;
}
- (NSArray *)loadAllData{
    XYZAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quote" inManagedObjectContext:managedObjectContext]; 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];  
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];   

    [request setSortDescriptors:sortDescriptors];
    [request setEntity: entity]; 

    NSError *myError;   
    NSArray *theResults = [managedObjectContext executeFetchRequest:request error:&myError];

    if (theResults == nil) {
        NSLog(@"Testing: No results found");
    }else {
        NSLog(@"Testing: Results found.");
    }

    [request release];
    [sortDescriptors release];

    for(NSUInteger i = [theResults count]; i > 1; i--) {
        NSUInteger j = random_below(i);
        [theResults exchangeObjectAtIndex:i-1 withObjectAtIndex:j];
    }

    return theResults;
}




warning: 'NSArray' may not respond to '-exchangeObjectAtIndex:withObjectAtIndex:' which crashed the application.     

error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_PFArray exchangeObjectAtIndex:withObjectAtIndex:]: unrecognized selector sent to instance 0x3d35a40
  • 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-15T06:52:10+00:00Added an answer on May 15, 2026 at 6:52 am

    OK. I think I see a problem. Always look at the error message. I thought you said that you were using NSMutableArrays in this function?

    NSArray *theResults = [managedObjectContext executeFetchRequest:request error:&myError];
    

    Change that to

    NSMutableArray *theResults = [[managedObjectContext executeFetchRequest:request error:&myError] mutableCopy];
    

    Tell me if it works, and if it doesn’t, I’ll try and help.

    Some more info:

    -exchangeObjectAtIndex:withObjectAtIndex:
    

    is a method for a NSMutableArray. You were using a NSArray, they’re different classes (NSMutableArray is a subclass (and adds methods that mean you can modify it) of NSArray).

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

Sidebar

Related Questions

in all the examples I find of Zend_Form the view showing the form corresponds
All the examples I could find of flexible box model show stuff expanding either
In all the examples I can find of usages of HttpClient , it is
I am trying to find rectangle buttons with gloss effect. all examples are relevant
All find and execute examples are a variation of this (with /bin/rm ) :
In all examples I can find as well as the automatically generated code i
I'm trying to map my Hashmap in Hibernate. All examples I can find are
I've been searching the site a lot, but all I could find were examples
All the examples I can find using DLLImport to call C++ code from C#
In all the tutorials or examples I find, they show a colon after the

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.