0

my requirement is to convert the NSMutualArray of NSData into NSMutualArray of NSString, Is there any method or function which directly works for above condition, or we have to deal with individual element of each array?

My code:

for (int i = 0; i < [newTutorials count]; i++) { // mutableArray[i] = [[NSString alloc] initWithData:newTutorials[i] encoding:NSUTF8StringEncoding]; NSLog(@"url: %@: ",newTutorials[i]); } NSLog(@"%@",mutableArray);

  • This is not related to Xcode. By the way, a `for-in` loop with a call to `initWithData:encoding:` should not be so hard to write. –  Jun 08 '13 at 16:06

2 Answers2

0

I am not aware of anyway besides visiting each element, like:

NSMutableArray *mutableArray; // your array of objects
for (int i = 0; i < [mutableArray count]; i++)
    mutableArray[i] = [[NSString alloc] initWithData:mutableArray[i] encoding:NSUTF8StringEncoding];
Firo
  • 15,448
  • 3
  • 54
  • 74
  • I'd suggest using `NSUTF8StringEncoding` instead of ASCII. –  Jun 08 '13 at 16:57
  • thanks for response, i used ur suggested code but it terminate saying "Terminating app due to uncaught exception 'NSInvalidArgumentException'", –  Jun 08 '13 at 17:01
  • @rowin does it give you any more information about the exception? – Firo Jun 10 '13 at 14:15
-1

Answer by Firo is correct. Only change required is the syntax used to set/replaced object in NSMutableArray.

Correct syntax is :

[yourMutableArray replaceObjectAtIndex:i withObject:
           [[NSString alloc] initWithData:[yourMutableArray objectAtIndex:i] encoding:NSASCIIStringEncoding];
Geek
  • 8,280
  • 17
  • 73
  • 137
  • Wrong. You can use the `[]` syntax in modern Objective-C to access elements of `NSArray` and `NSDictionary`. –  Jun 08 '13 at 16:56
  • [Stuff to read](http://stackoverflow.com/questions/8106375/accessing-nsarrays-items-with-subscript) –  Jun 08 '13 at 18:04