Write an Objective-C method, and a c function, when writing for the iPhone?
When using XCode for writing for开发者_JS百科 the iphone SDK... like a simple function that accepts a text-string URL and visits the website, and returns the HTML... which is better?
- Use Objective-C and write a "method" that accepts an NSString and returns an NSString. or
- 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?
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
Typically you would write new iPhone code in Objective-C.
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.
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.
精彩评论