目标c重新分配点
由于某种原因,我无法解决这个问题......
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
model.value=newString
更新属性有什么问题?如果你确实想要doSomething:
修改它的字符串参数,你需要传递一个指向变量的指针:并像这样调用它:
但这不是很惯用。仅返回一个值并让调用者决定如何管理结果可能更干净:
What's wrong with updating the property using
model.value=newString
? If you really wantdoSomething:
to modify its string parameter, you need to pass a pointer to the variable:and call it like this:
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:
您需要传入指向 ivar 的指针,而不是 ivar 本身。
并像这样创建您的方法:
因此,当您执行
foo
时,其值是@"new string"
。您正在接受指向该对象的指针,并使其指向另一个对象。通常,该函数获取它自己的指针,并且您为其分配新值在创建它的范围之外不会产生任何影响。
我实际上不确定这的官方名称是什么,但我很确定这就是它的工作原理。任何更精通 C 语言的人都可能会澄清。
当然,这个例子很容易通过简单地返回一个值来清理。这个技巧很有用,但我认为在大多数情况下,如果您避免这种模式,您的代码将更容易理解并且整体更清晰。因此,仅当您有充分的理由时才使用它,例如您有一个返回值的方法,但又想返回更多值,这些值可能重要也可能不重要,调用者可以选择捕获。但如果您可以简单地返回一个值并对其进行赋值,那么您这样做可能会更好。
You need to pass in a pointer to the ivar, and not the ivar itself.
And create your method like so:
So when you do
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.