ivar 不会响应 NSMutableString 方法
#import <Foundation/Foundation.h>
@interface Engine : NSObject {
NSMutableString *mutableName;
}
@property (assign) NSMutableString *mutableName;
@end
为什么我的 ivar, engine.name
不能使用这个简单的 NSMutableString
方法? mutableName
是一个 NSMutableString
,并已使用 @property
和 @synthesize
正确实现。
#import "Engine.h"
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Engine *engine = [[Engine alloc]init];
#import "Engine.h"
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Engine *engine = [[Engine alloc]init];
engine.mutableName = @"Jones";
[engine.mutableName insertString:@"Mrs." atIndex:0];
NSLog(@"Full name is %@", engine.mutableName);
}
[pool drain];
return 0;
}
#import "Engine.h"
@implementation Engine
@synthesize mutableName;
@end
#import "Engine.h"
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Engine *engine = [[Engine alloc]init];
engine.mutableName = @"Jones";
[engine.mutableName insertString:@"Mrs." atIndex:0];
NSLog(@"Full name is %@", name);
[pool drain];
return 0;
}
#import <Foundation/Foundation.h>
@interface Engine : NSObject {
NSMutableString *mutableName;
}
@property (assign) NSMutableString *mutableName;
@end
Why does my ivar, engine.name
not work with this simple NSMutableString
method? mutableName
is an NSMutableString
and has been correctly implemented with @property
and @synthesize
.
#import "Engine.h"
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Engine *engine = [[Engine alloc]init];
#import "Engine.h"
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Engine *engine = [[Engine alloc]init];
engine.mutableName = @"Jones";
[engine.mutableName insertString:@"Mrs." atIndex:0];
NSLog(@"Full name is %@", engine.mutableName);
}
[pool drain];
return 0;
}
#import "Engine.h"
@implementation Engine
@synthesize mutableName;
@end
#import "Engine.h"
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Engine *engine = [[Engine alloc]init];
engine.mutableName = @"Jones";
[engine.mutableName insertString:@"Mrs." atIndex:0];
NSLog(@"Full name is %@", name);
[pool drain];
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有显示您的代码,但我怀疑您所做的是声明
name
和mutableName
属性,并实现它们以在后台使用相同的 ivar。在这种情况下,您的mutableName
属性虽然声明为NSMutableString
,但实际上返回分配给name
的NSString
代码>属性。如果这就是您正在做的事情,并且您确实希望它以这种方式工作,那么您需要将name
访问器更改为如下所示(假设_name
是支持 ivar):这样字符串在底层保持可变,但在使用
name
访问器时作为不可变字符串返回。You haven't shown your code, but I suspect what you've done is declared both
name
andmutableName
properties and implemented them to use the same ivar under the hood. In this case, yourmutableName
property, while declared asNSMutableString
, is actually returning theNSString
that was assigned to thename
property. If this is what you're doing, and you really do want it to work this way, then you need to change yourname
accessors to look like the following (assuming_name
is the backing ivar):This way the string is kept as mutable under the hood, but returned as an immutable string when using the
name
accessor.