views:

101

answers:

4

When using XCode for writing for the iphone SDK... like a simple function that accepts a text-string URL and visits the website, and returns the HTML... which is better?

  1. Use Objective-C and write a "method" that accepts an NSString and returns an NSString. or
  2. Use C and write a "function" that accepts a string and returns a string.

How do I decide which to use... here... and in any of my code?

A: 

Typically you would write new iPhone code in Objective-C.

gerry3
A: 

Unless performance is a serious concern, you should use whatever is going to make the most sense when you come back to it 6 months later and need to make a change.

Azeem.Butt
A: 

If I recall correctly, even if you write all your code in C, you have to wrap it in Objective-C to work on the iPhone.

+1  A: 

For your particular example, you could probably get away with using [NSString stringWithContentsOfURL:encoding:error]

In general, write according to intended use. If you are going to be calling it from C code, or are interested in cross platform compatibility, write it in C. If you are calling from ObjC, or would like the input/result to be easily used with Apple's ObjC frameworks, write it in ObjC.

The normal pattern is to develop as much as possible in ObjC, and dive down to the C layers only for what's not possible using the higher level ObjC frameworks, so on that basis the recommended approach for all new code would be to use ObjC

cidered
I guess I should have added this comment. (But I'm not sure it matters.) *INSIDE* my code (function or method) I would still use as much Object-C and Apple Frameworks as possible. My question was just really "should I put my Objective-C in a wrapper of C or Objective-C"?Yes, I would still be using stringWithContentsOfURL either way. (I didn't want anyone to think I was going to write my entire "get the HTML code from a URL" all in raw C code.)
Donna