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
First of all, it’s not a
method, it’s a Cfunction, similar toNSLog, so use it as such: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 theself(or_cmd, for that matter) variables inside a C function, as each C function is independent of other functions inside your product.