i had a problem with leak. In this editingSessionArray
in this line below:
NSMutableArray *sessionsArray = [[NSMutableArray alloc] initWithArray:[BBWorkoutSession fetchSessionDetail1:selectedWorkoutId]];
say that Method returns a objective-c object with a +1 retain count (owing reference):
were did i made the mistake.
Please help me
-(void)editingSessionsArray
{
[[BBAppController sharedInstance] updateLogWithString:[NSString stringWithFormat:#"BEGIN : %s", __PRETTY_FUNCTION__]];
if(selectedWorkoutId) {
// NSLog(#"the section id is : %#",selectedWorkoutId);
NSMutableArray *sessionsArray = [[NSMutableArray alloc] initWithArray:[BBWorkoutSession fetchSessionDetail1:selectedWorkoutId]];
/* condition for checking the selected workout id is existing or not */
if ([workOutSessionData count]) {
for (int i= 0; i< [workOutSessionData count] ; i++) {
if ([[[workOutSessionData objectAtIndex:i] allKeys] containsObject:[NSString stringWithFormat:#"%#",selectedWorkoutId]]) {
[workOutSessionData removeObjectAtIndex:i];
return;
}
}
}
/* If console comes here the workout id is not existed in the main array */
[mainDict setValue:sessionsArray forKey:[NSString stringWithFormat:#"%#",selectedWorkoutId]];
NSMutableDictionary *workoutsList = [[NSMutableDictionary alloc]initWithObjectsAndKeys:sessionsArray,[NSString stringWithFormat:#"%#",selectedWorkoutId],nil] ;
[workOutSessionData addObject:workoutsList];
[workoutsList release];
[sessionsArray release];
}
[[BBAppController sharedInstance] updateLogWithString:[NSString stringWithFormat:#"END : %s", __PRETTY_FUNCTION__]];
}
If your code goes here:
if ([workOutSessionData count]) {
for (int i= 0; i< [workOutSessionData count] ; i++) {
if ([[[workOutSessionData objectAtIndex:i] allKeys] containsObject:[NSString stringWithFormat:#"%#",selectedWorkoutId]]) {
[workOutSessionData removeObjectAtIndex:i];
return;
}
}
}
you return without releasing workoutList and sessionArray.
You should allocate your sessionArray with an autorelease
NSMutableArray *sessionsArray = [[[NSMutableArray alloc] initWithArray:[BBWorkoutSession fetchSessionDetail1:selectedWorkoutId]] autorelease];
Related
I have the array like:
(
{
id=1;
Title="AAAA";
period_id=1;
},
{
id=2;
Title="BBBB";
period_id=2;
},
{
id=3;
Title="CCCC";
period_id=2;
},
{
id=4;
Title="DDDD";
period_id=2;
},
{
id=5;
Title="EEEE";
period_id=3;
},
)
Question: How can i know that Period_id=2 is multiple times in the array?
Help me solve this.
Thank you,
There are lots of ways to do so, Some of them are here ..
A:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"period_id == %#", #"2"];
NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
NSLog(#"%d", [newArray count]);
B:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (id obj in array)
{
if([obj[#"period_id"] isEqualToString:#"2"]){
[newArray addObject:obj];
}
}
NSLog(#"%d", [newArray count]);
C:
NSArray *allIds = [array valueForKey:#"period_id"];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:allIds];
for (id item in set)
{
NSLog(#"period_id=%#, Count=%d", item,[set countForObject:item]);
}
D:
NSArray *allIds = [array valueForKey:#"period_id"];
__block NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSString *valueToCheck = #"2";
[allIds enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isEqualToString:valueToCheck])
[newArray addObject:obj];
}];
NSLog(#"%d", [newArray count]);
E:
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *newArray = [array objectsAtIndexes:indexes];
NSLog(#"%d", [newArray count]);
try like this,
NSIndexSet *indices = [questionSections indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *filtered = [questionSections objectsAtIndexes:indices];
NSLog(#"duplictae:%d\n %#",[indices count],filtered);
O/P:-
duplicate: 3
(
{
name = bbbb;
"period_id" = 2;
},
{
name = ccccc;
"period_id" = 2;
},
{
name = ddddd;
"period_id" = 2;
}
)
if the array is sorted, as it seems at your case, just check if the next item has the same value as this one
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == array[i + 1].id) {
// Duplicate
}
}
if you just want to know about id = 2
int idCount = 0;
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idCount++;
}
}
if you also want to know the location
int idCount = 0;
int idarr[array.size()];
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idarr[idCount++] = i;
}
}
I think this is a JSON response from what I gather. Yes you can get the period_id. Add all the period_id's in an NSMutableArray.
Then simply search for the period_id from within this array for the values of the period_id to be same . You will get the index on which the period_id's are same.
NSSet *uniqueElements = [NSSet setWithArray:myArray];
for(id element in uniqueElements) {
// iterate here
}
You could also use NSPredicate to check duplicate.
Try this Example:
NSPredicate *testPredicate = [NSPredicate predicateWithFormat:#"period_id.intValue == %d",value];
NSMutableArray *data = [[NSMutableArray alloc] init];
NSArray *testArray= [yourArray filteredArrayUsingPredicate:testPredicate];
NSLog(#"duplicate:%d",[testArray count]);
This question already has answers here:
Removing object from NSMutableArray
(5 answers)
Closed 9 years ago.
I have an NSMutableArray of objects which are of AdDetail class that hold a few properties (for eg. adId, adTitle, adPrice... etc). I want to remove only those objects which have adID = 0. How can I do that ?
Perhaps something more elegant would suffice?
[array removeObjectsInArray:[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"adID == 0"]]];
Using predicate
NSArray *filtered=[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(adId == 0)"]];
Using fastEnumeration:
NSMutableArray *newArray=[NSMutableArray new];
for(AdDetail adDetailObj in array){
if(![[adDetailObj adId] isEqualToString:#"0"]){ //if these are strings, if NSInteger then directly compare using ==
newArray[newArray.count]=adDetailObj;
}
}
Now newArray contains all objects other than id=0
Use following code :
int count = array.count;
for(i=0;i<count;i++){
ADetail *adetail = [array objectAtIndex:i];
if(adetail.adID = 0){
[array removeObjectAtIndex:i];
i--;
}
count = array.count;
}
NSMutableArray *newArray = [NSMutableArray arrayWithArray:yourArray];
for (int i = 0; i < yourArray.count; i++)
{
AdDetail *obj = (AdDetail *)[yourArray objectAtIndex:i];
if (obj.adID == 0)
[newArray removeObjectAtIndex:i];
}
yourArray = [newArray mutableCopy];
for(i=0; i < myArray.count; i++)
{
myClass = [myArray objectAtIndex:i];
if([myClass.adID isEqualtoString:"0"])// if it it int/NSInteger the write myClass.adID==0
{
[myArray removeObjectAtIndex:i];
i--;
}
}
predicate = #"adID == 0";
newArray = [theArray filterUsingPredicate:aPredicate]
I want to below effect,but I don't kown how to use NSMutableArray combine NSArray More than two?
1.my code
for (int i=0; i<[DateSortArry2 count]; i++) {
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[Arry addObject:sectiondateStr2];
}
}
[SumArry addObjectsFromArray:Arry];
[Arry removeAllObjects];
}
2.my code Result
SumArry:(
"20130227",
"20130227",
"20130227",
"20130226",
"20130226",
"20130226",
"20130225",
"20130225")
3.I want the results
SumArry:((
"20130227",
"20130227",
"20130227",
),
(
"20130226",
"20130226",
"20130226",
),
(
"20130225",
"20130225"
))
Your code repeatedly fills and empties the same array by adding its elements, but you need to preserve the structure with additional instances of NSArray. So, use a new NSArray for each section.
for (int i=0; i<[DateSortArry2 count]; i++) {
NSMutableArray *section = [NSMutableArray array];
for (int j=0; j<[DateSortArry2Copy count]; j++) {
NSString *sectiondateStr2 = [NSString stringWithFormat:#"%#",[DateSortArry2Copy objectAtIndex:j]];
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:sectiondateStr2]) {
[section addObject:sectiondateStr2];
}
}
[SumArry addObject:section];
}
You can either store a reference to another array (or any type of object) in your array:
[myMutableArray addObject:otherArray];
Or concatinate the arrays.
[myMutableArray addObjectsFromArray:otherArray];
Both of which are documented in the documentation. By the looks of it the first approach is what you want since you want to have NSArray of NSMutableArray.
try this:
please tell me if it works.
thanks
NSString *str = #"";
for (int i=0; i<[DateSortArry2 count]; i++)
{
if (str isEqualToString:[DateSortArry2 objectAtIndex:i])
{
return;
}
else
{
NSMutableArray * Arry = [[NSMutableArray alloc] init];
str = [DateSortArry2 objectAtIndex:i]
for (int j=0; j<[DateSortArry2Copy count]; j++)
{
if ([[DateSortArry2 objectAtIndex:i] isEqualToString:str])
{
[Arry addObject:str];
}
}
[SumArry addObject:Arry];
[Arry removeAllObjects];
}
}
i have this code :
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i <[titleNews count]; ++i) {
TFHppleElement *someText =[titleNews objectAtIndex:i];
NSString *result = [someText content];
[arr addObject:result];
if ([[arr objectAtIndex:i]isEqualToString:#"-- : --"]) {
[arr replaceObjectAtIndex:i withObject:#"x"];
[arr addObject:#"/"];
}
NSLog(#"%#",[arr objectAtIndex:i]);
}
the (replaceObjectAtIndex: withObject:) work fine but the (addObject)method not working
You're presuming that the "result" that you add to the array will always be at index i. But the first time that you fall into your "if" body, you'll add an i+1 element ("/") and from then on everything will be off by one.
i have the following code
inAppKeys = [[MKStoreManager sharedManager] purchasableObjectsDescription ];
NSMutableArray * unremovableArray = [[NSMutableArray alloc] init];
for(int i = 0; i<[inAppKeys count]; i++){
for (int j=0; j< [categories count]; j++) {
NSString * inAppKey = [[categories objectAtIndex:j] valueForKey:#"inAppKey"];
if([inAppKey isEqualToString: [inAppKeys objectAtIndex:i]]){
[unremovableArray addObject:[categories objectAtIndex:j]];
}
}
}
categories = [[NSMutableArray alloc] init];
[categories addObjectsFromArray:unremovableArray];
where categories is nsmutablearray .. the thing is addObjectsFromArray leave the categories empty .. what do i do wrong?
Looks to me like you're referring to [categories count] and [categories objectAtIndex:j] before you even alloc/init categories.
Having re-read your title ("reinitializing") which suggests you've previously inited categories, I'm now assuming that you have a master set of categories that you're trying to reduce to the ones actually purchased. If so, I wouldn't re-use the variable "categories" as that's confusing. (I assume categories was auto-released, or else you've got a leak). How 'bout using unremovableArray instead of leaking it?
I'd also use fast enumerators for clarity and speed...
NSLog(#"categories: %#", categories);
inAppKeys = [[MKStoreManager sharedManager] purchasableObjectsDescription ];
NSLog(#"inAppKeys:%#", inAppKeys);
NSMutableArray * unremovableCategories = [[NSMutableArray alloc] init];
for(NSString* thisAppKey in inAppKeys) {
for (NSDictionary* thisCategory in categories) {
if ([[thisCategory valueForKey:#"inAppKey"] isEqualToString: thisAppKey]){
[unremovableCategories addObject:thisCategory];
break; //having added this category; no reason to continue looking at it
}
}
}
//now use unremovableCategories...