Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the values within NSArray by dereferencing?

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!

like image 486
Matt.M Avatar asked Nov 23 '09 19:11

Matt.M


People also ask

How do you dereference an array?

You cannot dereference an array, only a pointer. What's happening here is that an expression of array type, in most contexts, is implicitly converted to ("decays" to) a pointer to the first element of the array object. So ar "decays" to &ar[0] ; dereferencing that gives you the value of ar[0] , which is an int .

What is dereference operator in c++?

The . * operator is used to dereference pointers to class members. The first operand must be of class type. If the type of the first operand is class type T , or is a class that has been derived from class type T , the second operand must be a pointer to a member of a class type T .

How to dereference a pointer in Cpp?

The unary operator * is used to declare a pointer and the unary operator & is used to dereference the pointer.. In both cases, the operator is “unary” because it acts upon a single operand to produce a new value.

How do you declare NSArray in Objective-C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.


3 Answers

Yeah, that's not going to work. You'll need an NSMutableArray:

NSMutableArray * phoneNumbers = [[phoneEmailDict objectForKey:@"phone"] mutableCopy];
for (NSUInteger i = 0; i < [phoneNumber count]; ++i) {
  NSString * phone = [phoneNumbers objectAtIndex:i];
  phone = [self removeNonNumbers:phone];
  [phoneNumbers replaceObjectAtIndex:i withObject:phone];
}
[phoneEmailDict setObject:phoneNumbers forKey:@"phone"];
[phoneNumbers release];
like image 181
Dave DeLong Avatar answered Nov 15 '22 05:11

Dave DeLong


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.

like image 38
Chuck Avatar answered Nov 15 '22 04:11

Chuck


NSArray is not a C/C++ style array. It's an Objective-C object. You need to use the instance methods of the NSArray 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.

like image 42
Ben S Avatar answered Nov 15 '22 05:11

Ben S