0

I'm trying to learn the basics of Objective-C through reading "Learning Objective-C 2.0" Theres an exercise on categories where your asked to add a method to NSString through use of categories. My simple program is below. it (should) take a string and then reverses the order of the words.

Main

#import <Foundation/Foundation.h>
#import "CatNSString.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

    NSString *test = @"Dog bites Man";
    NSString *test1 = nil;

    test1 = [test1 reverseWords: test];

    NSLog(@"%@ : %@", test, test1);

}
return 0;

}

Interface

#import <Foundation/Foundation.h>

@interface NSString (CatNSString)

- (NSString*) reverseWords:(NSString*)string;

@end

Implementation

#import "CatNSString.h"

@implementation NSString (CatNSString)

- (NSString*) reverseWords: (NSString*) string
{
    NSString *stringReturn = nil;
    NSArray *arrayString = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    stringReturn = [string stringByAppendingString:@"hello"];


    for (NSString *word in arrayString)
    {
        NSString *stringTmp1 = word;
        NSString *stringTmp2 = stringReturn;
        stringReturn = [stringTmp1 stringByAppendingString:stringTmp2];
        NSLog(@"stringTmp1: %@", stringTmp1);

    }

    return stringReturn;
}


@end

It compiles but the program acts as though the method is never called. If I place a NSLog call in the method there is no output to the console. Can anyone see what I'm not doing that I should be doing?

Larme
  • 24,190
  • 6
  • 51
  • 81
hoboBob
  • 832
  • 1
  • 17
  • 37
  • Because `test1` is `nil`. You don't use `self` in that method (or implicitly with one of its instance methods or properties). So it should be a class method: `- (NSString*) reverseWords:(NSString*)string;`=> `+(NSString*) reverseWords:(NSString*)string;`, and `test1 = [test1 reverseWords: test];`should be then `test1 = [NSString reverseWords: test];` – Larme Sep 24 '18 at 20:01
  • Possible duplicate of [Calling a method on an uninitialized object (null pointer)](https://stackoverflow.com/questions/2696891/calling-a-method-on-an-uninitialized-object-null-pointer) – Larme Sep 24 '18 at 20:02
  • Hi Larme, thanks I've turned the method into a class method and then tried again with initialising test1 as something other then nill. The method was called both times. Thanks again for your time. – hoboBob Sep 24 '18 at 20:17

1 Answers1

1

You just did it wrong way.

Here is the proper way to define a Category:

@interface NSString (CatNSString)
-(NSString *)reverse;
@end

@implementation NSString (CatNSString)
-(NSString*)reverse {
    // `Self` keyword will refer to your original string, no need to pass it as a parameter
    NSArray *words = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return [[[words reverseObjectEnumerator] allObjects] componentsJoinedByString:@" "];
}
@end

And in your main code, do the following:

NSString *test = @"Dog bites Man";
NSLog(@"%@", [test reverse]);
RyanB
  • 1,287
  • 1
  • 9
  • 28