0

i have this nsarray having images data in it images setting dynamically in array but i want to filter data if there is any nil nsdata i dont want it inside my nsarray how can i sort this array. here is my code.

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileImg"];
    NSData* imageData1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileImg1"];
    NSData* imageData2 = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileImg2"];
    NSData* imageData3 = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileImg3"];
    NSData* imageData4 = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileImg4"];
    NSData* imageData5 = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileImg5"];




    self.pageImages = [NSArray arrayWithObjects:
                                           [UIImage imageWithData:imageData],
                                           [UIImage imageWithData:imageData1],
                                           [UIImage imageWithData:imageData2],
                                           [UIImage imageWithData:imageData3],
                                           [UIImage imageWithData:imageData4],
                                           [UIImage imageWithData:imageData5],nil];
2
  • There can't be nil objects in an NSArray. If, for example, imageData1 is nil, the array will only contain imageData and no other images. Commented Aug 10, 2016 at 15:45
  • So if i put some other dummy pic instead of nil so will it work then?? I mean how can i apply if statement on is image is dummy then DONOT add is in nsaray! Commented Aug 10, 2016 at 15:51

2 Answers 2

3

I would take a completely different approach. The problem is that arrayWithObjects: stops when it encounters a nil. If, for example, imageData1 is nil, your array will only have one image - imageData.

A better way would be to check each one for nil. Only add the non-nil images.

NSArray *keys = @[ @"profileImg", @"profileImg1", @"profileImg2", @"profileImg3", @"profileImg4", @"profileImg5" ];
NSMutableArray *images = [NSMutableArray array];
for (NSString *key in keys) {
    NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if (imageData) {
        UIImage *image = [UIImage imageWithData:imageData];
        if (image) {
            [images addObject:image];
        }
    }
}

self.pageImages = [images copy];
Sign up to request clarification or add additional context in comments.

1 Comment

triend this one but app crashes and output "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData _isDecompressing]: unrecognized selector sent to instance 0x1567164a0'"
1

You can directly check whether the object at that index is nil.

NSMutableArray * finalArray = [[NSMutableArray alloc] init];
for (int i = 0; i< pageImages.count; i++) {
    if (pageImages[i]){
        UIImage * temp = [pageImages objectAtIndex:i];
        [finalArray addObject:temp];
    }
}

11 Comments

thanks for replying i actually want to store sorted data inside the same self.pageImages
i actually want to exclude all the images with nill data or no images in it out of my nsarray
Yes, that's what I am saying . take the finalArray and store in pageImages
Add after for loop - pageImages = finalArray ;
i did that but still no luck :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.