Objective c 在附加到NSMutableString时遇到问题

Objective c 在附加到NSMutableString时遇到问题,objective-c,nsstring,nsmutablestring,Objective C,Nsstring,Nsmutablestring,大家好, 我只是想知道我哪里做错了。我尝试了这段代码,但“mutableString”没有附加任何值(因为“len”的值为“0”,而NSLog没有为“mutableString”打印任何值),尽管我在网上搜索了解决方案,但人们以同样的方式实现了,但我不知道为什么我的代码不起作用 提前谢谢 MGD通过追加字符串来创建新字符串。改为使用appendString:: [mutableString appendString:@“你好”]哦,天哪,真是一团糟。stringByAppendingString

大家好,

我只是想知道我哪里做错了。我尝试了这段代码,但“mutableString”没有附加任何值(因为“len”的值为“0”,而NSLog没有为“mutableString”打印任何值),尽管我在网上搜索了解决方案,但人们以同样的方式实现了,但我不知道为什么我的代码不起作用

提前谢谢


MGD通过追加字符串来创建新字符串。改为使用
appendString:


[mutableString appendString:@“你好”]

哦,天哪,真是一团糟。
stringByAppendingString
不会更改字符串,而是创建并返回一个新字符串:

@interface MainView : UIView { 
    NSMutableString *mutableString; 
}
@property (nonatomic, retain) NSMutableString *mutableString;
@end

@implementation MainView
@synthesize mutableString;

-(void) InitFunc {  
    self.mutableString=[[NSMutableString alloc] init];
 }

-(void) AppendFunc:(*NString) alpha { 
    [self.mutableString stringByAppendingString:@"hello"];
    NSLog(@"the appended String is: %@",self.mutableString);
    int len=[self.mutableString length];
}
如果要更改可变字符串本身,请使用
appendString
方法:

// Sets str2 to “hello, world”, does not change str1.
NSMutableString *str1 = [NSMutableString stringWithString:@"hello, "];
NSString *str2 = [str1 stringByAppendingString:@"world"];
此外,这是一个漏洞:

// Does not return anything, changes str1 in place.
[str1 appendString:@"world"];
最好是这样写的:

self.mutableString = [[NSMutableString alloc] init];
…因为在
init
dealloc

1)中使用访问器会违反命名约定:请使用小写字母开头

2)
stringByAppendingString
作为结果返回一个新字符串,并且不修改原始字符串。您应该使用
[self.mutableString appendString:@“hello”]取而代之


3) 您的init方法正在泄漏。您应该使用
mutableString=[[NSMutableString alloc]init]retain
ed(并且
release
将丢失)。

谢谢兄弟,问题解决了!,事实上,我是一个新手,我需要像你这样有经验的人给我很好的指导。。。我很抱歉我发布了这么一个愚蠢的问题,顺便说一句,谢谢你的帮助。这不是一个愚蠢的问题,写一个乱七八糟的代码也没什么错,我们都经历过。(见鬼,我仍然在写乱七八糟的代码。)只要一直努力变得更好,就这样。非常感谢。。。我会把你的宝贵建议留在身边:)
mutableString = [[NSMutableString alloc] init];