0

I would define some helper functions, and i prefer write them in C instead of objective-C.

Which is the "best practice" to add header and implementation C file in a Objective C project ?

If i need to use Objective-c definition in my functions, what i need to include in my header C file (UIKit?) ?

Example: i'd like to create a shortcut to NSLog for NSString , can i create in my C file something similar to this code ? :

void MYLog (NSString *string){
  NSLog(@"%@",string);
}
3
  • Why create a shortcut for NSLog() to take a single NSString when it already take a string, and optionaly more parameters? Commented Dec 15, 2010 at 12:47
  • Every time I've thought I'll just create a C API here, I've regretted it... An Objective-C version isn't that much more work, rarely incurs a significant amount of overhead vs. straight C and is considerably more versatile. Commented Dec 15, 2010 at 16:32
  • ehm... Peylow you are right is completely useless ...let's say this is an example to help me formulate the question :P -.- Commented Dec 23, 2010 at 20:58

2 Answers 2

2

Yes sure you can use objective-c functions in your c code (and vice versa) if your source file is compiled as c/obj-c code (has .m extension or type set manually in xcode)

To make core functions work I think importing <Foundation/Foundation.h> and <UIKit/UIKit.h> should be sufficient. These headers may be already imported in your precompiled header (*.pch file) so you may not even need to import them.

If you want to extend some UIKit classes functionality consider also implementing custom class category instead of using plain c functions.

Sign up to request clarification or add additional context in comments.

1 Comment

And put your C code in a .m file so things like @"foo" are understood by the compiler
0

You can do this quite easily. You shouldn’t have to import anything, as you have UIKit and Foundation already imported by the prefix header. The logger function would look like this:

// Logger.h
void MyLog(NSString *str);

// Logger.m
#import "Logger.h"
void MyLog(NSString *str) {…}

2 Comments

Thank you zoul, in your opinion, can i work with .c extension or i need to use .m ?
You should use .m, so that the compiler understands Objective-C in those files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.