How to create a method in Objective-C that takes an NSString stringWithFormat parameter as a parameter?

I'm not sure the title is clear. I want to make a convenient method that works like the NSLog method and concatenates the lines below?

This is what I have at the moment:

NSString *out = [NSString stringWithFormat:@"something %d,%d",1,2];
[self showLog:out];

What would a similar method look like in a definition?

- (void) showLog:(NSString *) data;

thank

+3
source share
4 answers

In interface

-(void) showLog: (NSString*) formatSpecifier, ...;

In implementation

-(void) showLog: (NSString*) formatSpecifier, ...
{
    va_list formatArgs;
    va_start(formatArgs, formatSpecifier);
    NSString* logMessage = [[NSString alloc] initWithFormat: formatSpecifier arguments: formatArgs];
    va_end(formatArgs);

    // Do want you need to to output the string.

    [logMessage release];
}
+7
source
+2
source

- ?

NSString *out = [NSString stringWithFormat:@"something %d,%d",1,2];
[self showLog:out];

- (void)showLog:(NSString*)data{
     NSLog(@"%@", data);
}

, :)

, , ;)

Kristian

+1

, String , , .

#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
  //Method declaration
- (void) functn:(NSString*)str;
@end

@implementation SampleClass
  //<<<<<<<<<<-- Function to print UserName -->>>>>>>>>>>>>>>
- (void)functn:(NSString*)str
  {
  NSLog(@"Your Name is %@",str);
  }
@end
int main ()
{
 SampleClass *sampleClass = [[SampleClass alloc]init];
 /* calling a method to get max value */
 NSString *yourName=@"Pir fahim shah";
 [sampleClass functn:yourName];
 return 0;
}
0

Source: https://habr.com/ru/post/1779364/


All Articles