我遇到了与 Objective-C 中数组中的指针相关的问题。
我想做的是获取 NSArray 中的指针,将它们传递给一个方法,然后将返回的值分配回原始指针(属于数组的指针)。
根据我从 C 和 C++ 中了解到的信息,通过取消引用数组中的指针,我应该能够更改它们指向的值...这是我正在使用的代码,但它不起作用(值电话根据 NSLog 输出,指向永远不会改变)。
NSArray *phoneNumbers = [phoneEmailDict objectForKey:@"phone"];
for (NSString* phone in phoneNumbers) {
(*phone) = (*[self removeNonNumbers:phone]);
NSLog(@"phone:%@", phone);
}
下面是我将 NSString* 传递给的方法签名:
- (NSString*) removeNonNumbers: (NSString*) string;
如您所见,我使用变量phone 迭代phoneNumbers 中的每个NSString*。我将电话传递给removeNonNumbers:,它返回修改后的NSString*。 I 然后取消引用从removeNonNumber 返回的指针并将值分配给phone。
正如您所知,我可能不太了解 Objective-C 对象。我很确定这在 C++ 或 C 中可以工作,但我不明白为什么它在这里不起作用!预先感谢您的帮助!
I've come across a problem related to pointers within arrays in objective-c.
What I'm trying to do is take the pointers within an NSArray, pass them to a method, and then assign the returned value back to the original pointer(the pointer which belongs to the array).
Based on what I know from C and C++, by dereferencing the pointers within the array, I should be able to change the values they point to... Here is the code I'm using, but it is not working (the value phone points to never changes based on the NSLog output).
NSArray *phoneNumbers = [phoneEmailDict objectForKey:@"phone"];
for (NSString* phone in phoneNumbers) {
(*phone) = (*[self removeNonNumbers:phone]);
NSLog(@"phone:%@", phone);
}
And here is the method signature I am passing the NSString* to:
- (NSString*) removeNonNumbers: (NSString*) string;
As you can see, I am iterating through each NSString* within phoneNumbers with the variable phone. I pass the phone to removeNonNumbers:, which returns the modified NSString*. I Then dereference the pointer returned from removeNonNumber and assign the value to phone.
As you can tell, I probably do not understand Objective-C objects that well. I'm pretty sure this would work in C++ or C, but I can't see why it doesn't work here! Thanks in advance for your help!
发布评论
评论(5)
是的,那是行不通的。您需要一个
NSMutableArray
:Yeah, that's not going to work. You'll need an
NSMutableArray
:您无法取消引用 Objective-C 对象变量。它们始终是指针,但您应该将它们视为原子值。您需要改变数组本身以包含您正在生成的新对象。
You can't dereference Objective-C object variables. They are always pointers, but you should treat them as though they're atomic values. You need to mutate the array itself to contain the new objects you're generating.
NSArray
不是 C/C++ 风格的数组。它是一个 Objective-C 对象。您需要使用NSArray
类的实例方法,用于对其执行操作。在 Objective-C 中,您永远不会“取消引用”对象指针来设置其值。
另外,您正在使用所谓的 快速枚举,不允许突变。
NSArray
is not a C/C++ style array. It's an Objective-C object. You need to use the instance methods of theNSArray
class to perform operations on it.In Objective-C you never "dereference" an object pointer to set its value.
Also, you're using what is called Fast Enumeration, which does not allow mutation.
您还可以使用 enumerateObjectsUsingBlock:。
查看如何迭代 NSArray?
You can also use enumerateObjectsUsingBlock:.
Checkout How do I iterate over an NSArray?
虽然这可能在某种程度上有效,但我还没有测试过它,我会将其归档在“坏主意”下,而不是碰触。 NSArray 和许多其他 cocoa 对象相当复杂,并且作为类簇设计模式的一部分,可以在底层有多种实现。
因此,归根结底,您真的不知道自己在内部处理什么。 NSArray 实际上被设计为不可变的,因此就地编辑甚至是一个糟糕的主意。
旨在让您搞乱内部结构的对象通过 API 方法(如 NSMutableData 的 mutableBytes )公开这些对象。
您最好使用处理后的值构造一个新的 NS(Mutable)Array。
While this may work to some degree, I haven't tested it, I'd file this under 'bad idea' and not touch. NSArray, and many other cocoa objects, a fairly complex and can have a variety of implementations under the hood as part of the class cluster design pattern.
So when it comes down to it you really won't know what you're dealing internally. NSArray is actually designed to be immutable so in place editing is even doubly a bad idea.
Objects that are designed to let you mess around with the internals expose those through api methods like
NSMutableData
'smutableBytes
.You're better off constructing a new NS(Mutable)Array with the processed values.