简体   繁体   English

在Objective-C中向数组添加数据时出现问题

[英]Problem in adding data to an array in Objective-C

I am grappling with adding an NSData object to a NSMutable array. 我正在努力将NSData对象添加到NSMutable数组中。 The code is executing fine but it is not adding the objects in the array.The code is as follows: 代码执行正常,但未在数组中添加对象,代码如下:

NSData * imageData = UIImageJPEGRepresentation(img, 1.0);

int i=0;

    do{

        if([[tempArray objectAtIndex:i] isEqual:imageData])
        {
            [tempArray removeObjectAtIndex:i];
        }
        else 
        {
            [tempArray addObject:imageData];
            //NSLog(@"ANURAG %@",[tempArray objectAtIndex:0]);
        }
    }while(i<[tempArray count]) ;

The NSLog statement shows the object added is null however the value of imageData is not null. NSLog语句显示添加的对象为null,但是imageData的值不为null。

I have defined the tempArray as a static memeber of the class in which this code is written. 我已将tempArray定义为编写此代码的类的静态成员。

Is it because of the size of the data object as it is the data of an image? 是因为数据对象的大小,因为它是图像的数据吗?

Do you initialize tempArray before this code section? 您是否在此代码段之前初始化tempArray? You cannot add objects to an uninitialized array. 您不能将对象添加到未初始化的数组。

tempArray = [[NSMutableArray alloc] init];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM