Search code examples
objective-cprintfnslog

Why isn't this print method working?


It works when I use NSLog, but not printf. I want to use printf because then all the elements in setA will be displayed in one line.

#import <Foundation/Foundation.h>

#define INTOBJ(v) [NSNumber numberWithInteger: v]

@interface NSSet (Printing) 
-(void) print;
@end

@implementation NSSet (Printing)
-(void) print {
    for (NSNumber *element in self) 
        printf("%li", (long) [element integerValue]);
}

@end

int main (int argc, char *argv[]) {
    @autoreleasepool {
        NSMutableSet *setA = [NSMutableSet setWithObjects:INTOBJ(3), INTOBJ(10), INTOBJ(1), INTOBJ(5), nil];

        [setA print];
    }
    return 0;
}

Solution

  • NSLog is essentially a different version of printf that allows for Objective C stuff, while printf can't handle Objective C material.

    If your printf method isn't working, this is probably the problem.