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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:34:07+00:00 2026-05-29T04:34:07+00:00

I wrote this following Objective-C code as part of Stanford’s iTunes U course on

  • 0

I wrote this following Objective-C code as part of Stanford’s iTunes U course on iOS programming. Are there any ways to simplify this code? The requirement is that this method return nil if the set is empty, not an empty NSSet. It’s also required to take an (id), not an NSArray, and that the method doesn’t crash no matter what values are passed into it.

+ (NSSet *)variablesUsedInProgram:(id)program {
    NSMutableArray *stack;
    id variables;
    NSMutableSet *setOfVariables;
    if ([program isKindOfClass:[NSArray class]]) {
        stack = [program mutableCopy];
        int i = stack.count;
        while (i--) {
            if ([[stack objectAtIndex:i] isKindOfClass:[NSString class]]) {
                if ([self isOperation:[stack objectAtIndex:i]]) {
                    [setOfVariables addObject:[stack objectAtIndex:i]];
                }
            }
        }
    }
    if (setOfVariables.count > 0) {
        variables = setOfVariables;
    }
    return variables;
}
  • 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-29T04:34:07+00:00Added an answer on May 29, 2026 at 4:34 am

    variables and setOfVariables are never initialised. In the case where setOfVariables (assuming it was initialised) has a count of 0, the method will return an uninitialised value. I’m not sure why you need a mutable copy of program since you never modify the array. I’m not sure why you need a copy of it at all actually, and you don’t actually release it either (which is OK if you are using ARC or GC, but not if you are using MRC). This is how I would refactor it, the observable behaviour shouldn’t be different to your method.

    + (NSSet *)variablesUsedInProgram:(id)program
    {
        if (![program isKindOfClass:[NSArray class]])
            return nil;
    
        NSMutableSet *setOfVariables = [NSMutableSet set];
    
        foreach (id object in program)
            if ([self isOperation:object])
                [setOfVariables addObject:object];
    
        if (setOfVariables.count > 0)
            return setOfVariables;
        else
            return nil;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this following java code to format the date and time in specific
I wrote this snippet of code and I assume len is tail-recursive, but a
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this function in C++ as part of a bigger program: Object Single_list<Object>::pop_front()
I wrote the following lines of code: private static List<Int32> GetRandomList_Serial() { List<Int32> returnValue
I just created an Xcode project and wrote the following code: #define foo(x) x
I wrote this code in c# to check if a file is out of
I see lots of TDD practitioners following this cycle: 1) Write your test as
Following on from this question what would be the best way to write a
I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .

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.