• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?


  • Subject: Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
  • From: Alex Zavatone <email@hidden>
  • Date: Fri, 04 Mar 2016 16:14:45 -0500

Great!  It certainly does… but here's where my brain breaks.

The call is originating from the C lib and within the C function.  I am not calling the C function from Objective-C.

I'm looking at somehow passing this as a string (UTF-8, yep) back to an OC class instance, so this implies either a callback or some reference to the OC instance that that cares about the response and a means to get that message to it.

If this is as simple as setting up a callback or a pointer reference, from the c class to the OC instance?  Is it sane programming for the C class to have more than one callback to different OC object instances?

I was thinking one for data and one for method calls for organizational purposes.

Or should there be one layer that serves as a clearly defined API to create a walled garden between the OC world and the C interface to the compiled C lib?

I'm working with PJSIP and PJ's docs clearly state, "we are going to crater unless you do everything SIP related on the main thread."  The code that I am rewriting replacing has nasty try/catch clauses and forces many operations to the main thread just in case they call PJSIP operations - which clearly makes for a sucky user experience and really clunky application architecture.

I'm looking to avoid that nastiness by starting from ground zero so that we can wrap a solidly conceived architecture around a neatly walled off interface layer to PJSIP.


Would it make sense to send a notification from the C method to an Objective-C object to get the value from the C class?  Then I'd need to worry about storing it,  that seems clunky and too involved just to return a string.

Thank you, sir.  Loads for me to learn here.

Alex Zavatone



On Mar 4, 2016, at 3:48 PM, Doug Hill wrote:

> Alex,
>
> I’ve worked on a few wrapper libraries, so I have some experience with this.
>
> In your Obj-C wrapper, you would need to create the NSString yourself. So, if you have a C function:
>
> char* MyCFunc(void);
>
> The Objective-C wrapper method would do something like:
>
> - (void) myObjcMethod
> {
>    char* cStr = MyCFunc();
>    NSString* objcStr = [[NSString alloc] initWithUTF8String:cStr];
>
>    return objCStr;
> }
>
> Depending on the C function implementation, you might have to deal with releasing the C string in your wrapper. Also, I assume UTF-8 encoding, which may or may not be true.
>
> Hopefully this helps you.
>
> Doug Hill
>
>
>> On Mar 4, 2016, at 12:07 PM, Alex Zavatone <email@hidden> wrote:
>>
>> I'm in the middle of some fun where there is a wrapper class to a lib that's written in C and the c function has a char string that I'd like to return back to or somehow pass on to an Cbjective-C class.
>>
>> I'm sure there is an established practice for performing this type of task, but while I have the opportunity to do this, I'd like to start be learning the right way to handle this operation.
>>
>> I've seen really poor use of a catch all delegate for this approach, but am pretty unsure on viable and safe methods to handle this.
>>
>> Any tips to how to handle this?
>>
>> Thanks in advance,
>>
>> Alex Zavatone
>
>


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
      • From: Steve Sisak <email@hidden>
    • Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
      • From: Jonathan Mitchell <email@hidden>
    • Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
      • From: Doug Hill <email@hidden>
References: 
 >Returning a string value from a c function to a Objective-C class method. Is there an approved approach? (From: Alex Zavatone <email@hidden>)
 >Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach? (From: Doug Hill <email@hidden>)

  • Prev by Date: Re: Customizing a UISwitch?
  • Next by Date: Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
  • Previous by thread: Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
  • Next by thread: Re: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
  • Index(es):
    • Date
    • Thread