简体   繁体   中英

Write an Objective-C method, and a c function, when writing for the iPhone?

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?

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

通常,您将在Objective-C中编写新的 iPhone代码。

除非性能是一个严重的问题,否则六个月后重新使用它时,您应该使用最有意义的方法,并且需要进行更改。

如果我没记错的话,即使您用C编写了所有代码,也必须将其包装在Objective-C中才能在iPhone上工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM