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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:34:01+00:00 2026-05-23T03:34:01+00:00

I have the following function written to randomly pick a file from a directory.

  • 0

I have the following function written to randomly pick a file from a directory. It works totally fine when I build the project in Xcode for release with the application that automatically opens. However, if I open the application from finder, pressing the button that triggers this function will cause my program to freeze then crash. The only thing I could think of was changing the argument to contentsOfDirectoryAtPath: to not have the ./, but either version has the same exact issue.

Looking at Console tells me that my program exited abnormally with a Floating Point Exception, but I have no idea what’s causing it. Is there something jumping out to you guys that I’m not seeing? I only started learning/using objective-C and cocoa about a week ago, so this is all fairly new to me.

Thanks for taking a look at this…

- (NSMutableString*)setFilePathRandom{
NSArray* files;
NSFileManager* fileManager;
fileManager = [[NSFileManager alloc] init];
files = [fileManager contentsOfDirectoryAtPath:@"./Random Trippy Pics" error:NULL];
NSString* directoryPath = (NSMutableString*)[fileManager currentDirectoryPath];
NSString* fileName;
do{
    fileName = [files objectAtIndex:(arc4random()%[files count])];
}while([fileName isEqualToString:@".DS_Store"]);
filePath = [NSString stringWithFormat:@"%@/Random Trippy Pics/%@",directoryPath,fileName];
[fileManager release];
return filePath;
}
  • 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-23T03:34:01+00:00Added an answer on May 23, 2026 at 3:34 am

    When an OS X application is run from Xcode, its current directory is the path to the build folder. When run “normally”, the current directory is /. So your program is looking for a directory at /Random Trippy Pics, which almost certainly doesn’t exist. Where is that directory normally?


    Edit:

    You could get the directory in which the application is currently stored with this bit of code:

    NSString *currentStoragePath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
    

    However, if the Random Trippy Pics directory is required by the application, you should store it in a known location — preferably the application’s Resource directory. Then you can get the contents with:

    NSArray *files = [[NSBundle mainBundle] pathsForResourceOfType:nil inDirectory:@"Random Trippy Pics"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function that is pulling data from a database. The ajax
So: I have the following function, adapted from a formula found online, which takes
I have written following function which checks whether start_date field is not empty and
i have the following function i wrote to create an XML file using Xerces
I have written the following function.. and executed using WinHugs teneven = [x |
I have written the following function in a bash script but it is not
I have written the following function but it's isn't returning anything when I run
I have the following function that was written by someone else, however I am
I have written the following function to calculate a check digit in R. verhoeffCheck
I'm learning Lisp and have written the following function to collect a list of

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.