ivar 不会响应 NSMutableString 方法

发布于 2024-12-12 02:43:49 字数 1465 浏览 0 评论 0原文

#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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-12-19 02:43:50

您还没有显示您的代码,但我怀疑您所做的是声明 namemutableName 属性,并实现它们以在后台使用相同的 ivar。在这种情况下,您的 mutableName 属性虽然声明为 NSMutableString,但实际上返回分配给 nameNSString代码>属性。如果这就是您正在做的事情,并且您确实希望它以这种方式工作,那么您需要将 name 访问器更改为如下所示(假设 _name是支持 ivar):

- (NSString *)name {
    return [[_name copy] autorelease]; // take a snapshot of the string
}
- (void)setName:(NSString *)name {
    [_name release];
    _name = [_name mutableCopy];
}

这样字符串在底层保持可变,但在使用 name 访问器时作为不可变字符串返回。

You haven't shown your code, but I suspect what you've done is declared both name and mutableName properties and implemented them to use the same ivar under the hood. In this case, your mutableName property, while declared as NSMutableString, is actually returning the NSString that was assigned to the name property. If this is what you're doing, and you really do want it to work this way, then you need to change your name accessors to look like the following (assuming _name is the backing ivar):

- (NSString *)name {
    return [[_name copy] autorelease]; // take a snapshot of the string
}
- (void)setName:(NSString *)name {
    [_name release];
    _name = [_name mutableCopy];
}

This way the string is kept as mutable under the hood, but returned as an immutable string when using the name accessor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文