English 中文(简体)
ivar将不采用NSMutablering方法
原标题:ivar will not respond to NSMutableString method
#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;
}
问题回答

你没有显示你的代码,但我怀疑你所做的事情是:<条码><>>>和<条码>可更改的Name ,并且为了在世系使用相同的ivar。 在此情况下,您的<代码>可变性Name 财产,在被宣布为NSMutableString时,实际上将分配给<代码><> 姓名>/代码>的财产退回。 如果是你重新做的,而且你真的想这样做,那么你需要修改你的<条码>。 浏览器如以下(假设_name):背书:

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

这样一来,护str就被保留在hood子下,但在使用<条码>名称/代码>时,作为不可改变的扼杀。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签