Ganesh Ganesh - 4 months ago 9
Objective-C Question

How to initialise an object which is inside nsmutablearray?

I have 10

NSURLSessionDownloadTask
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
NSURLSessionDownloadTask
instances in
NSMutableArray
.

This is what i tried

for(i=0;i<10;i++){
[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

Answer

Try the code below:

for(i=0;i<10;i++){
    [self.downloadTaskArray addObject:[self.session downloadTaskWithURL:[NSURL [self.urlArray objectAtIndex:i]]];
}