iphone - 如何在 objective-c 中调用此方法

标签 iphone objective-c ios function methods

我已经看到这个Stackoverflow中的这段代码是对某些问题的回答,我试图暗含在我的代码中,但是我没有得到这是什么样的功能,我该怎么称呼它

NSString * ReplaceFirstNewLine(NSString * original)
{
    NSMutableString * newString = [NSMutableString stringWithString:original];

    NSRange foundRange = [original rangeOfString:@"\n"];
    if (foundRange.location != NSNotFound)
    {
        [newString replaceCharactersInRange:foundRange
                                 withString:@""];
    }
    NSLog(@"%@",newString);
    return [[newString retain] autorelease];
}

我试图像[self ReplaceFirstNewLine(@"\nstirng\nstring")];这样称呼它
但是它给了语法错误,有人可以帮我吗

最佳答案

首先,它不是method,它是C function,类似于NSLog,因此应这样使用:

NSString *results = ReplaceFirstNewLine(@"\nstirng\nstring");
NSLog(@"%@", results);

C样式函数有优点也有缺点,我将在这里尝试列出其中的一些:

好处:
  • 速度。 C函数几乎总是比等效的Objective-C方法快,因为不需要动态调度即可调用函数
  • 指针。指向C样式函数的指针比指向Objective-C方法要容易得多,这使其更适合C API回调

  • 缺点:
  • iVars。在C函数中,如果不使用运行时向导,则不能访问对象的 private 变量(即使带有对其的引用),这时确实不值得。
  • 没有self的概念。您不能在C函数内使用self(或_cmd)变量,因为每个C函数都独立于产品中的其他函数。
  • 关于iphone - 如何在 objective-c 中调用此方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11119290/

    相关文章:

    ios - MKAnnotationView 标注中的反转文本

    ios - 了解 iOS 中的 MVVM 架构

    iphone - 基于位置的本地通知

    iphone - iOS UIScrollView : Cannot scroll until the end of the content

    ios - 从选项卡栏中的应用程序委托(delegate)推送 View Controller

    ios - Xcode5错误: Warning attempt to present <View> on <OtherView> while presentation is in progress

    ios - 控制NSMallocException

    iphone - 以编程方式填充的 TableView 错误

    xcode - iPad 不读取我的 plists

    ios - 如何在第二个组件的特定行开始之前不在 UIPickerView 的第一个组件中显示数据?