English 中文(简体)
安保标准问题,在将其分配到职能参数后,就会失去价值。
原标题:problem with NSString, it s losing it s value after assigning it to a function parameter

Objective-C is really wierd, i can t get the hang of it... I have a NSstring that is losing it s value if I try to reassign it... Here s how I use it.. Can anyone tell me what am I doing wrong? it s happening at the assigning of the new value..

@interface PageViewController : UIViewController {

    NSString *mystring;
}

- (void)viewDidLoad {

    mystring=[ [NSString alloc] initWithString:@""];
}

-(void) function_definition:(NSString *) param {
.............
    mystring=param;
.........
}
最佳回答

最常见的情况是,你要把这一财产指定为:

@interface PageViewController : UIViewController {

    NSString *mystring;
}

@property (nonatomic, retain) NSString *mystring;

之后,你执行,

@synthesize mystring;

- (void)dealloc {
    [mystring release];
    [super dealloc];
}

最后,在你执行过程中,无论在什么地方,都利用:

[self setMystring:@"something"];

or

self.mystring = @"somethingelse";

如果你重新分配新的座椅,肯定会释放。 它自动使用该财产。

self.mystring = [[[NSString alloc] initWithString:@"hello"] autorelease];

最后,在你的职责中:

-(void) function_definition:(NSString *) param {
.............
    self.mystring = param;
.........
}
问题回答

这并非完全清楚你通过失去价值意味着什么,但我认为,这里的问题是:记忆管理——你需要

-(void) function_definition:(NSString *) param {
.............
    if (mystring != param) {
        [mystring release];
        mystring = [param retain];
    }
.........
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

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 ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签