1

i have seen this block of code in this Stackoverflow as a answer of some ones question,i tried to impliment that in my code but i am not getting what kind of function is this and how can i call that

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];
}

i have tried to call this like [self ReplaceFirstNewLine(@"\nstirng\nstring")]; but its giving syntax error,can any one help me out

Ravi
  • 1,759
  • 5
  • 20
  • 38

3 Answers3

8

First of all, it's not a method, it's a C function, similar to NSLog, so use it as such:

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

C-style-functions have advantages and disadvantages, and I'll try to list some of them here:

Advantages:

  • Speed. A C function is almost ALWAYS faster than an equivalent objective-c method, because there is no dynamic dispatch needed to call the function

  • Pointers. It is much easier to get a pointer to a C-style function than it is to an Objective-C method, which makes it much better suited for C API callbacks

Disadvantages:

  • iVars. In a C function, you cannot access the private variables of an object (even with a reference to it), without using runtime wizardry, at which point, it really isn't worth it.

  • No concept of self. You cannot use the self (or _cmd, for that matter) variables inside a C function, as each C function is independent of other functions inside your product.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • @Richard Could you please tell what kind of function is this. I only aware about methods of kind -(NSString)replaceFirstNewLine{ .... }; It seems to be C style function. Could you please provide some resource that I can refer about this. Thanks in advance. – rakeshNS Jun 20 '12 at 12:22
  • @Ravi you are welcome. Do not forget to up-vote and accept the answer, though! It helps others find this easier. – Richard J. Ross III Jun 20 '12 at 12:22
  • @Ravi isn't my answer enough? If you don't understand what a C function is, I would pick up a book on C, keeping objective-c elements in the back of your head. It is a far too large of task to cover in one answer. – Richard J. Ross III Jun 20 '12 at 12:23
  • ya i can accept only after 10 minutes na(stack oveflw rule :-)),, can we call this from other class? – Ravi Jun 20 '12 at 12:28
  • Yes, if the function is not static, you can add a forwarding reference to it. http://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c – Richard J. Ross III Jun 20 '12 at 12:33
0

You would call it like this: ReplaceFirstNewLine(@"");

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0
- (NSString *) ReplaceFirstNewLine(NSString * original){
    //...
}

Now you should be abe to call it like a method:

[self ReplaceFirstNewLine(@"\nstirng\nstring")];
Norbert Bicsi
  • 1,562
  • 2
  • 19
  • 33