Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 对属性调用方法_Objective C_Methods_Properties_Key Value Observing - Fatal编程技术网

Objective c 对属性调用方法

Objective c 对属性调用方法,objective-c,methods,properties,key-value-observing,Objective C,Methods,Properties,Key Value Observing,是否有任何方法可以调用或传递属性上的方法。我知道如何设置和获取属性,但如何操作它们?我正在尝试使用面向对象编程删除字符串上的标点符号。从输入字符串中删除标点符号的行为是作为一种方法编写的 main.m TDItem *newItem = [[TDItem alloc] init]; [newItem setItemString:@"Get the mail next Tuesday!"]; NSLog(@"\nCreated Item: %@", [newItem itemString]);

是否有任何方法可以调用或传递属性上的方法。我知道如何设置和获取属性,但如何操作它们?我正在尝试使用面向对象编程删除字符串上的标点符号。从输入字符串中删除标点符号的行为是作为一种方法编写的

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]);
- (NSString *)itemStringWithoutPunctuation:(NSString *)itemString
{
NSString* itemStringWithoutPunctuation = [[itemString componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@" "];
return itemStringWithoutPunctuation;
}
t项目h

@interface TDItem : NSObject

@property NSString *itemString;


// Formating methods
- (NSString *)itemStringWithoutPunctuation:(NSString *)itemString;
t项目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]);
- (NSString *)itemStringWithoutPunctuation:(NSString *)itemString
{
NSString* itemStringWithoutPunctuation = [[itemString componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@" "];
return itemStringWithoutPunctuation;
}

调试控制台为新的itemString值打印一个空白

调试程序

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

如果这是完全错误的,那么如何更改属性值?

回答您的问题:

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]);
相反,请执行以下操作:

NSString* itemStringWithoutPunctuation = [newItem itemStringWithoutPunctuation:[newItem itemString]];
[newItem setItemString:itemStringWithoutPunctuation];
NSLog(@"\nCreated Item: %@", [newItem itemString]);
@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 ;
注意:属性具有更方便的语法

由于
itemString
是一个属性,您可以使用
语法更清晰地访问它:

newItem.itemString = @"Hello, world" ;
NSLog ( @"The string is: %@" , newItem.itemString ) ;
注意:放置代码的备用位置

为什么
itemstringwithoutpuncuation
NewItem
类上的一个实例方法?这是没有意义的,尤其是要求你传递字符串

相反,您可能希望执行以下操作:

NSString* itemStringWithoutPunctuation = [newItem itemStringWithoutPunctuation:[newItem itemString]];
[newItem setItemString:itemStringWithoutPunctuation];
NSLog(@"\nCreated Item: %@", [newItem itemString]);
@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 ;
或者,您可以这样做:

@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] ) ;

但是,我要提醒您:您删除标点符号的代码可能并没有达到您认为的效果,但您很快就会发现这一点,然后可以修复它。

为什么将
itemstringwithoutpuncuation
变量声明为
id
,而不是
NSString
?减少键入
id
可以在许多情况下代替
MyClass*
。使用
id
可能会导致各种错误,因为编译器无法执行任何类型检查。在某种程度上,这是正确的。然而,更现代的编译器可以为您捕获其中的许多(这就是为什么我热衷于使用静态分析器的原因)。在这种情况下,
id
是非常好的,因为它的覆盖范围很小,但您是对的,在任何大型或更永久的对象中使用类型都是绝对可取的。我会更新我的答案,但我们应该把这些评论留在这里,因为这是一个很好的观点。