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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:23:09+00:00 2026-05-17T22:23:09+00:00

I was wondering what would be the best way to implement a morse code

  • 0

I was wondering what would be the best way to implement a morse code generator. The way it would work is the user types in a word or phrase and then that NSString would be passed into my method as an argument for processing. The way I want to process is to loop through each character in the string and then play the correct sequence of tones as I go. I’ve already implemented the mechanism in a separate class.

I’ve already tried so many different methods but they all seem to introduce some sort of problem. Things i’ve tried so far are: creating an NS Timer, using delaying method calls (performSelector:withObject:afterDelay).

Below is an example of the method i’m using to delay, but the problem is that if the user types in say “aaaa” only the first “a” will play because the entire loop will have been completed before the morse sequence is even done generating. It would be more ideal if the loop could wait for the switch case to finish before continuing.

Anyways this is the way that i thought it should be implemented but it doesn’t seem to be working, so maybe there is a better way of implementing the morse code generator or i’m just missing a step so if anyone can please help me figure this out i’d really appreciate it.

- (NSString *)convertTextToMorse:(NSString *)phrase {

    for (int i = 0; i < [phrase length]; i++) {

        unichar ch;
        ch = [phrase characterAtIndex:i];
        NSLog(@"Processing charachter %c",ch);

        //Dot = 100ms
        //Dash = 300ms
        //Pause = 200ms

        switch (ch) {
            case 'a':
                //Morse "a"
                [self playDotSound];
                [self performSelector:@selector(playDashSound) withObject:nil afterDelay:0.2];
                break;
            case 'b':
                //Morse "b"
                break;
            default:
                break;
        }
    }

    return phrase;

}
  • 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-17T22:23:10+00:00Added an answer on May 17, 2026 at 10:23 pm

    Another option might be to run the entire thing on a separate thread:

    - (NSString *) convertTextToMorse:(NSString *)phrase {
        [self performSelectorInBackground:@selector(morseInternal:) withObject:phrase];
        return phrase;
    }
    
    - (NSString *) morseInternal:(NSString *)phrase {
        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        for (int i = 0; i < [phrase length]; i++) {
            if (stopMorse) {
                break;
            }
            unichar ch = [phrase characterAtIndex:i];
            NSLog(@"Processing charachter %c",ch);
            switch (ch) {
                case 'a':
                    [self playDotSound];
                    [self pause];
                    [self playDashSound];
                break;
                case 'b':
                    //Morse "b"
                    break;
                default:
                    break;
            }
        }
        [pool release];
    }
    
    - (void) pause {
        [NSThread sleepForTimeInterval:0.2];
    }
    

    Notice I added a stopMorse boolean instance variable that you can set to true if you want to stop the morse code generation in the middle of the string.

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

Sidebar

Related Questions

i was wondering what would be the best way to code a browser plugin
I was wondering if InnoDB would be the best way to format the table?
I was wondering what would be the best strategy to implement a badges system
I'm wondering what would be the best way to group contacts by their company.
I'm wondering what would be the best prectice regarding mainataining connections to the database
I was looking at WF and was wondering if it would be best to
As a hobby project, I had like to implement a Morse code encoder and
I'm struggling with the following bit of code(/challenge) and I was wondering what would
What's the best way (in a WPF app, C#) of implementing control behaviour that
I was wondering what is the best way to change the font colour 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.