-1

Is there any way to call or pass a method on a property. I understand how to set and get properties but how do I manipulate them? I'm trying to remove punctuation on string using object oriented programing. The act of removing punctuation from an input string is written as a method.

main.m

TDItem *newItem = [[TDItem alloc] init];

[newItem setItemString:@"Get the mail next Tuesday!"];
NSLog(@"\nCreated Item: %@", [newItem itemString]);

NSString *itemStringWithoutPunctuation = [[NSString alloc] init];
[newItem itemStringWithoutPunctuation:[newItem itemString]];
[newItem setItemString:itemStringWithoutPunctuation];
NSLog(@"\nCreated Item: %@", [newItem itemString]);

TDItem.h

@interface TDItem : NSObject

@property NSString *itemString;


// Formating methods
- (NSString *)itemStringWithoutPunctuation:(NSString *)itemString;

TDItem.m

- (NSString *)itemStringWithoutPunctuation:(NSString *)itemString
{
NSString* itemStringWithoutPunctuation = [[itemString componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@" "];
return itemStringWithoutPunctuation;
}


The debug console prints out a blank for the new itemString value.

Debuger

Created Item: Get the mail next Tuesday!
Created Item: 

If this the completely wrong what to go about changing a property value?

liliaceae
  • 9
  • 1
  • 4

1 Answers1

0

To answer your question:

NSString *itemStringWithoutPunctuation = [[NSString alloc] init]; // note: immutable string
[newItem itemStringWithoutPunctuation:[newItem itemString]]; // does NOT save the result!
[newItem setItemString:itemStringWithoutPunctuation]; // sets itemString to the empty string
NSLog(@"\nCreated Item: %@", [newItem itemString]);

Instead, do this:

NSString* itemStringWithoutPunctuation = [newItem itemStringWithoutPunctuation:[newItem itemString]];
[newItem setItemString:itemStringWithoutPunctuation];
NSLog(@"\nCreated Item: %@", [newItem itemString]);

Note: Properties have a more convenient syntax

Since itemString is a property, you can access it much more cleanly with the . syntax:

newItem.itemString = @"Hello, world" ;
NSLog ( @"The string is: %@" , newItem.itemString ) ;

Note: Alternate place to put the code

Why is itemStringWithoutPunctuation an instance method on your NewItem class? It doesn't make sense, especially requiring you to pass that string in.

You may want to, instead, do this:

@interface NSString (MyCustomAdditions)
- (NSString*) stringByRemovingPunctuation ;
@end

@implementation NSString (MyCustomAdditions)
- (NSString*) stringByRemovingPunctuation {
    return [[self componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@" "];
}
@end

// elsewhere...
NSString* itemStringWithoutPunctuation = [newItem.itemString stringByRemovingPunctuation] ;
newItem.itemString = itemStringWithoutPunctuation ;

Alternatively, you could do this:

@interface TDItem : NSObject
@property NSString* itemString ;
- (NSString*) itemStringWithoutPunctuation ;
@end

// elsewhere
TDItem * item = [ [TDItem alloc] init ] ;
NSLog ( @"The string is: %@" , item.itemString ) ;
NSLog ( @"The string, without punctuation, is: %@" , [item itemStringWithoutPunctuation] ) ;

However, I'd caution you: your code to remove punctuation probably isn't doing what you think it's doing, but you'll discover this soon and can fix it then.

iAdjunct
  • 2,739
  • 1
  • 18
  • 27
  • Why did you declare the `itemStringWithoutPunctuation` variable as `id` instead of `NSString`? – rmaddy Sep 07 '15 at 22:12
  • Less typing. `id` can be used in lieu of `MyClass*` in many circumstances. – iAdjunct Sep 07 '15 at 22:45
  • And using `id` can lead to all kinds of bugs since the compiler can't do any type checking. – rmaddy Sep 07 '15 at 22:46
  • To an extend that is true. However, the more modern compilers can catch many of them for you (which is why I'm a fan of religious use of the static analyzer). In this case, `id` is perfectly fine because the extent of its reach is minimal, but you're right that it is absolutely generally preferable to use types in anything large or more permanent. I'll update my answer, but we should leave these comments here because it's a good point. – iAdjunct Sep 07 '15 at 22:52