目标c重新分配点

发布于 2024-08-22 07:00:04 字数 629 浏览 12 评论 0原文

由于某种原因,我无法解决这个问题......

我有一个带有 ivar (值)的类(模型)。我想将值传递给方法并将值 ivar 更改为指向其他内容。这会将 aValue 更改为指向 newString。那么,如何让 model.value 指向 newString 而无需说 model.value = newString 呢?

(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
 Model * model = [[Model alloc] init];//model.value gets set in init
 [self doSomething:model.value];
 NSLog(@"changed?  %@", model.value);
}

- (void)doSomething:(NSString *)aValue
{
 NSString * newString = [[NSString alloc] initWithString:@"new string"];
 aValue = newString;
 NSLog(@"changed? %@", aValue);
}

另外,我该如何让这个该死的代码块正常工作?

I cant wrap my head around this for some reason...

I have a class (Model) with an ivar (value). I want to pass the value to a method and change the value ivar to point to something else. This changes the aValue to point to newString. So, how do I get the model.value to point to newString with out saying model.value = newString?

(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
 Model * model = [[Model alloc] init];//model.value gets set in init
 [self doSomething:model.value];
 NSLog(@"changed?  %@", model.value);
}

- (void)doSomething:(NSString *)aValue
{
 NSString * newString = [[NSString alloc] initWithString:@"new string"];
 aValue = newString;
 NSLog(@"changed? %@", aValue);
}

Also, how do I get this darn code block to work right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(2

轮廓§ 2024-08-29 07:00:04

使用 model.value=newString 更新属性有什么问题?如果你确实想要 doSomething: 修改它的字符串参数,你需要传递一个指向变量的指针:

- (void)doSomething:(NSString **)aValue
{
    [*aValue release];

    NSString * newString = [[NSString alloc] initWithString:@"new string"];
    *aValue = newString;
    NSLog(@"changed? %@", *aValue);
}

并像这样调用它:

[self doSomething:&value];

但这不是很惯用。仅返回一个值并让调用者决定如何管理结果可能更干净:

- (NSString*)doSomething
{
    NSString * newString = [[NSString alloc] initWithString:@"new string"];
    NSLog(@"changed? %@", *aValue);
    return [newString autorelease];
}

What's wrong with updating the property using model.value=newString? If you really want doSomething: to modify its string parameter, you need to pass a pointer to the variable:

- (void)doSomething:(NSString **)aValue
{
    [*aValue release];

    NSString * newString = [[NSString alloc] initWithString:@"new string"];
    *aValue = newString;
    NSLog(@"changed? %@", *aValue);
}

and call it like this:

[self doSomething:&value];

But this is not very idiomatic. It is probably cleaner to just return a value and let the caller decide how to manage the result:

- (NSString*)doSomething
{
    NSString * newString = [[NSString alloc] initWithString:@"new string"];
    NSLog(@"changed? %@", *aValue);
    return [newString autorelease];
}
十六岁半 2024-08-29 07:00:04

您需要传入指向 ivar 的指针,而不是 ivar 本身。

[self doSomething:&model.value];

并像这样创建您的方法:

- (void)doSomething:(NSString**)aValue {
    *aValue = @"new string";
}

因此,当您执行

NSString *foo = @"old string";
[self doSomething:&foo];
NSLog(@"foo is: %@", foo);

foo 时,其值是 @"new string"

您正在接受指向该对象的指针,并使其指向另一个对象。通常,该函数获取它自己的指针,并且您为其分配新值在创建它的范围之外不会产生任何影响。

我实际上不确定这的官方名称是什么,但我很确定这就是它的工作原理。任何更精通 C 语言的人都可能会澄清。

当然,这个例子很容易通过简单地返回一个值来清理。这个技巧很有用,但我认为在大多数情况下,如果您避免这种模式,您的代码将更容易理解并且整体更清晰。因此,仅当您有充分的理由时才使用它,例如您有一个返回值的方法,但又想返回更多值,这些值可能重要也可能不重要,调用者可以选择捕获。但如果您可以简单地返回一个值并对其进行赋值,那么您这样做可能会更好。

You need to pass in a pointer to the ivar, and not the ivar itself.

[self doSomething:&model.value];

And create your method like so:

- (void)doSomething:(NSString**)aValue {
    *aValue = @"new string";
}

So when you do

NSString *foo = @"old string";
[self doSomething:&foo];
NSLog(@"foo is: %@", foo);

foo's value is @"new string".

You are accepting the pointer to the object, and making it point to a different object. Normally, the function gets it's own pointer and you assigning a new value to it has no effect outside of the scope that created it.

I'm actually not sure what this is officially called, but I am pretty sure this is how it works. Anyone more well versed in C can likely clarify.

And, of course, this example is easily cleaned by simply returning a value. This trick is useful, but I think in most cases your code will be easier to understand and overall cleaner if you avoid this pattern. So only use it if you have a good reason, like you have a method returning a value, but also want to return more values that may or may not be important that the caller can optionally capture. But if you can simply return a value and assign it, it's probably better that you do.

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