Change values ​​in NSArray by dereferencing?

I ran into a problem related to pointers inside arrays in objective-c.

What I'm trying to do is take the pointers inside the NSArray, pass them to the method, and then return the return value to the original pointer (the pointer belonging to the array).

Based on what I know from C and C ++, dereferencing pointers inside an array, I should be able to change the values ​​that they point to ... Here is the code that I use, but it doesn't work (the value of the telephone points never changes based on 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 pass in NSString *:

- (NSString*) removeNonNumbers: (NSString*) string;

As you can see, I repeat through each NSString * in phoneNumbers using a variable phone. I pass the phone to delete NonNumbers:, which returns the modified NSString *. I Then look for the pointer returned with removeNonNumber and assign a value to the phone.

As you can tell, I probably don't understand Objective-C objects very well. I am sure this will work in C ++ or C, but I do not understand why this does not work here! Thanks in advance for your help!

+5
source share
5 answers

Yes, that will not work. You will need 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];
+13
source

Objective-C. , , . , , .

+4

NSArray C/++. Objective-C. NSArray .

Objective-C "" , .

, Fast Enumeration, .

+2

enumerateObjectsUsingBlock:.

NSArray *array = [NSArray array];
__block NSMutableArray *mutableCopyArray = [array mutableCopy];
[mutableCopyArray enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
    [mutableCopyArray replaceObjectAtIndex:idx withObject:[object modifiedObject]];
}];

NSArray?

+1

, , " " . NSArray cocoa , .

, , , . NSArray , , .

, , , api, NSMutableData mutableBytes.

NS (Mutable) Array .

0

All Articles