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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:08:42+00:00 2026-05-27T09:08:42+00:00

I have a class method returning a CGSize and I’d like to call it

  • 0

I have a class method returning a CGSize and I’d like to call it via the Objective-C runtime functions because I’m given the class and method names as string values.

I’m compiling with ARC flags in XCode 4.2.

Method signature:

+(CGSize)contentSize:(NSString *)text;

The first thing I tried was to invoke it with objc_msgSend like this:

Class clazz = NSClassFromString(@"someClassName);
SEL method = NSSelectorFromString(@"contentSize:");

id result = objc_msgSend(clazz, method, text);

This crashed with “EXC_BAD_ACCESS” and without a stack trace. I used this first because the documentation for objc_msgSend says,

When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend, objc_msgSend_stret, objc_msgSendSuper, or objc_msgSendSuper_stret. […] Methods that have data structures as return values are sent using objc_msgSendSuper_stret and objc_msgSend_stret.

Next, I used objc_msgSend_stret like this:

Class clazz = NSClassFromString(@"someClassName);
SEL method = NSSelectorFromString(@"contentSize:");

CGSize size = CGSizeMake(0, 0);
objc_msgSend_stret(&size, clazz, method, text);

Using the above signature gives me the following two compiler errors and two warnings:

error: Automatic Reference Counting Issue: Implicit conversion of a non-Objective-C pointer type ‘CGSize *’ (aka ‘struct CGSize *’) to ‘id’ is disallowed with ARC

warning: Semantic Issue: Incompatible pointer types passing ‘CGSize *’ (aka ‘struct CGSize *’) to parameter of type ‘id’

error: Automatic Reference Counting Issue: Implicit conversion of an Objective-C pointer to ‘SEL’ is disallowed with ARC

warning: Semantic Issue: Incompatible pointer types passing
‘__unsafe_unretained Class’ to parameter of type ‘SEL’

If I look at the declaration of the method, it is:

OBJC_EXPORT void objc_msgSend_stret(id self, SEL op, ...)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

which is identical to objc_msgSend:

OBJC_EXPORT id objc_msgSend(id self, SEL op, ...)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

This explains the compiler errors to me but what do I use at the run-time level to invoke a class and its static method at run-time and return a struct value?

  • 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-27T09:08:43+00:00Added an answer on May 27, 2026 at 9:08 am

    You need to cast objc_msgSend_stret to the correct function pointer type. It’s defined as void objc_msgSend_stret(id, SEL, ...), which is an inappropriate type to actually call. You’ll want to use something like

    CGSize size = ((CGSize(*)(id, SEL, NSString*))objc_msgSend_stret)(clazz, @selector(contentSize:), text);
    

    Here we’re just casting objc_msgSend_stret to a function of type (CGSize (*)(id, SEL, NSString*)), which is the actual type of the IMP that implements +contentSize:.

    Note, we’re also using @selector(contentSize:) because there’s no reason to use NSSelectorFromString() for selectors known at compile-time.

    Also note that casting the function pointer is required even for regular invocations of objc_msgSend(). Even if calling objc_msgSend() directly works in your particular case, it’s relying on the assumption that the varargs method invocation ABI is the same as the ABI for calling a non-varargs method, which may not be true on all platforms.

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

Sidebar

Related Questions

i have a class method in which i am creating and returning the class
I have a class with a static method that looks roughly like: class X
Whats recommended in an abstract class? Have a virtual method returning null, or have
I have a c++ class which exposes collections by providing functions returning ranges, using
Say I have the following class with a method returning a list: class C:
I have class method that returns a list of employees that I can iterate
Is it possible in Delphi to have a class method invoke an inherited instance
I have a C# class method that return a xml document not file. How
I have a class with a custom each-method: class CurseArray < Array def each_safe
I have a class with some method that depend by one parameter. What is

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.