• 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: Doug Hill <email@hidden>
  • Date: Fri, 04 Mar 2016 12:48:05 -0800

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: Alex Zavatone <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>)

  • Prev by Date: Returning a string value from a c function to a Objective-C class method. Is there an approved approach?
  • Next by Date: Re: Customizing a UISwitch?
  • Previous by thread: 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