Ganesh Ganesh - 2 months ago 6x
Objective-C Question

How to initialise an object which is inside nsmutablearray?

I have 10

instances for downloading 10 files concurrently.

Each instances are named differently (For ex. downloadTask1,downloadTask2,etc..)

I initialised them as follows,

self.downloadTask1 = [self.session downloadTaskWithURL:[NSURL url1]]
self.downloadTask2 = [self.session downloadTaskWithURL:[NSURL url2]]
self.downloadTask3 = [self.session downloadTaskWithURL:[NSURL url3]]
self.downloadTask10 = [self.session downloadTaskWithURL:[NSURL url10]]

This takes 10 lines. Now my question is,

Can i do that in single line by adding each
instances in

This is what i tried

[self.downloadTaskArray objectAtIndex:i] = [self.session downloadTaskWithURL:[NSURL [self.urlArray objectAtIndex:i]]

I know the above method is wrong. The error which i faced is Expression is not Assignable.

I hope you understood my problem. Is is possible to achieve what i am trying ?

Note : I definitely want to use different name for downloadTask
because i need to identify them later in my project


Try the code below:

    [self.downloadTaskArray addObject:[self.session downloadTaskWithURL:[NSURL [self.urlArray objectAtIndex:i]]];