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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:04:37+00:00 2026-05-27T21:04:37+00:00

As I cannot create a synthesized property in a Category in Objective-C, I do

  • 0

As I cannot create a synthesized property in a Category in Objective-C, I do not know how to optimize the following code:

@interface MyClass (Variant)
@property (nonatomic, strong) NSString *test;
@end

@implementation MyClass (Variant)

@dynamic test;

- (NSString *)test {
    NSString *res;
    //do a lot of stuff
    return res;
}

@end

The test-method is called multiple times on runtime and I’m doing a lot of stuff to calculate the result. Normally using a synthesized property I store the value in a IVar _test the first time the method is called, and just returning this IVar next time. How can I optimized the above code?

  • 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-27T21:04:37+00:00Added an answer on May 27, 2026 at 9:04 pm

    @lorean’s method will work (note: answer is now deleted), but you’d only have a single storage slot. So if you wanted to use this on multiple instances and have each instance compute a distinct value, it wouldn’t work.

    Fortunately, the Objective-C runtime has this thing called Associated Objects that can do exactly what you’re wanting:

    #import <objc/runtime.h>
    
    static void *MyClassResultKey;
    @implementation MyClass
    
    - (NSString *)test {
      NSString *result = objc_getAssociatedObject(self, &MyClassResultKey);
      if (result == nil) {
        // do a lot of stuff
        result = ...;
        objc_setAssociatedObject(self, &MyClassResultKey, result, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
      }
      return result;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a piece of code but cannot get it working. The
I want to create an allocator which provides memory with the following attributes: cannot
I am receiving the following error: The buffer manager cannot create a temporary storage
We cannot create an instance of an interface. But why does Arrays.asList(Object[] a) in
As we know you cannot create a file download via ajax. you can however
I cannot create my database when I close it (check code below). If I
I cannot create a 2D array from 2 variables (eg int arr[i][j] not allowed)
As we know that we CANNOT create the instance of abstract class . I
Drupal cannot create the files folder. I tried to upload a php script with
I just want to understand why I cannot create a protected enum on C#?

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.